Examples/Validator/Validator.Tests.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

function MyValidator($thing_to_validate) {
    return $thing_to_validate.StartsWith("s")
}

function Invoke-SomethingThatUsesMyValidator {
    param(
        [ValidateScript({MyValidator $_})]
        $some_param
    )
}

Describe "Testing a validator" {

    It "calls MyValidator" {
        Mock MyValidator -MockWith { return $true }
        Invoke-SomethingThatUsesMyValidator "test"
        $was_called_once = 1
        Assert-MockCalled MyValidator $was_called_once
    }

}

Describe "MyValidator" {

    It "passes things that start with the letter S" {
        $result = MyValidator "summer"
        $result | Should Be $true
    }

    It "does not pass a param that does not start with S" {
        $result = MyValidator "bummer"
        $result | Should Be $false
    }
}