Functions/It.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
function It {
<#
.SYNOPSIS
Validates the results of a test inside of a Describe block.

.DESCRIPTION
The It command is intended to be used inside of a Describe or Context Block.
If you are familiar with the AAA pattern (Arrange-Act-Assert), the body of
the It block is the appropriate location for an assert. The convention is to
assert a single expectation for each It block. The code inside of the It block
should throw a terminating error if the expectation of the test is not met and
thus cause the test to fail. The name of the It block should expressively state
the expectation of the test.

In addition to using your own logic to test expectations and throw exceptions,
you may also use Pester's Should command to perform assertions in plain language.

.PARAMETER Name
An expressive phsae describing the expected test outcome.

.PARAMETER Test
The script block that should throw an exception if the
expectation of the test is not met.If you are following the
AAA pattern (Arrange-Act-Assert), this typically holds the
Assert.

.EXAMPLE
function Add-Numbers($a, $b) {
    return $a + $b
}

Describe "Add-Numbers" {
    It "adds positive numbers" {
        $sum = Add-Numbers 2 3
        $sum | Should Be 5
    }

    It "adds negative numbers" {
        $sum = Add-Numbers (-2) (-2)
        $sum | Should Be (-4)
    }

    It "adds one negative number to positive number" {
        $sum = Add-Numbers (-2) 2
        $sum | Should Be 0
    }

    It "concatenates strings if given strings" {
        $sum = Add-Numbers two three
        $sum | Should Be "twothree"
    }
}

.LINK
Describe
Context
about_should
#>

    param(
        [Parameter(Mandatory = $true)]
        [string]$name,
        [ScriptBlock] $test = $(Throw "No test script block is provided. (Have you put the open curly brace on the next line?)")
    )

    Assert-DescribeInProgress -CommandName It

    $Pester.EnterTest($name)
    Invoke-SetupBlocks

    $PesterException = $null
    try{
        $null = & $test
    } catch {
        $PesterException = $_
    }

    $Result = Get-PesterResult -Test $Test -Exception $PesterException
    $Pester.AddTestResult($Result.name, $Result.Success, $null, $result.failuremessage, $result.StackTrace )
    $Pester.testresult[-1] | Write-PesterResult

    Invoke-TeardownBlocks
    Exit-MockScope
    $Pester.LeaveTest()
}

function Get-PesterResult {
    param([ScriptBlock] $Test, $Time, $Exception)
    $testResult = @{
        name = $name
        time = $time
        failureMessage = ""
        stackTrace = ""
        success = $false
    };

    if(-not $exception)
    {
        $testResult.success = $true
    }
    else
    {
        if ($exception.FullyQualifiedErrorID -eq 'PesterAssertionFailed')
        {
            $failureMessage = $exception.exception.message
            $file = $test.File
            $line = if ( $exception.ErrorDetails.message -match "\d+$" )  { $matches[0] }
        }
        else {
            $failureMessage = $exception.ToString()
            $file = $Exception.InvocationInfo.ScriptName
            $line = $Exception.InvocationInfo.ScriptLineNumber
        }

        $testResult.failureMessage = $failureMessage -replace "Exception calling", "Assert failed on"
        $testResult.stackTrace = "at line: $line in $file"
    }
    return $testResult
}