Tests/Unit/MSFT_xWindowsUpdate.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
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
<#
    .SYNOPSIS
        Unit tests for xWindowsUpdate
    .DESCRIPTION
        Unit tests for xWindowsUpdate
 
    .NOTES
        Code in HEADER and FOOTER regions are standard and may be moved into DSCResource.Tools in
        Future and therefore should not be altered if possible.
#>



$Script:DSCModuleName      = 'xWindowsUpdate' # Example xNetworking
$Script:DSCResourceName    = 'MSFT_xWindowsUpdate' # Example MSFT_xFirewall

#region HEADER
[String] $moduleRoot = Split-Path -Parent (Split-Path -Parent (Split-Path -Parent $Script:MyInvocation.MyCommand.Path))
if ( (-not (Test-Path -Path (Join-Path -Path $moduleRoot -ChildPath 'DSCResource.Tests'))) -or `
     (-not (Test-Path -Path (Join-Path -Path $moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) )
{
    & git @('clone','https://github.com/PowerShell/DscResource.Tests.git',(Join-Path -Path $moduleRoot -ChildPath '\DSCResource.Tests\'))
}
else
{
    & git @('-C',(Join-Path -Path $moduleRoot -ChildPath '\DSCResource.Tests\'),'pull')
}
Import-Module (Join-Path -Path $moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force
$TestEnvironment = Initialize-TestEnvironment `
    -DSCModuleName $Script:DSCModuleName `
    -DSCResourceName $Script:DSCResourceName `
    -TestType Unit
#endregion

# Begin Testing
try
{

    #region Pester Tests

    # The InModuleScope command allows you to perform white-box unit testing on the internal
    # (non-exported) code of a Script Module.
    InModuleScope $Script:DSCResourceName {

        #region Function Get-TargetResource
        Describe "$($Script:DSCResourceName)\Get-TargetResource" {
            Mock Get-HotFix -MockWith { return [PSCustomObject]@{HotFixId='KB123456'}} -Verifiable
            Context 'Get hotfix' {

                $getResult = (Get-TargetResource -Path 'C:\test.msu' -Id 'KB123457' )

                it 'should have called get-hotfix'{
                    Assert-VerifiableMock
                }

                it 'should return id="KB123456"'{
                    $getResult.id | should be 'KB123456'
                }

                it 'should return path=""'{
                    $getResult.path | should be ([String]::Empty)
                }

                it 'should return log=""'{
                    $getResult.log | should be ([String]::Empty)
                }
            }
        }
        Describe "$($Script:DSCResourceName)\Test-TargetResource" {
            Context 'Hot fix exists' {
                Mock Get-HotFix -MockWith { return [PSCustomObject]@{HotFixId='KB123456'}} -Verifiable

                $getResult = (Test-TargetResource -Path 'C:\test.msu' -Id 'KB123456' )

                it 'should have called get-hotfix'{
                    Assert-VerifiableMock
                }

                it 'should return $true'{
                    $getResult | should be $true
                }
            }

            Context 'Hot fix does not exists' {
                Mock Get-HotFix -MockWith { return [PSCustomObject]@{HotFixId='KB123456'}} -Verifiable

                $getResult = (Test-TargetResource -Path 'C:\test.msu' -Id 'KB123457' )

                it 'should have called get-hotfix'{
                    Assert-VerifiableMock
                }

                it 'should return $true'{
                    $getResult | should be $true
                }
            }
        }
    }
    #endregion
}
finally
{
    #region FOOTER
    Restore-TestEnvironment -TestEnvironment $TestEnvironment
    #endregion
}