Skip to content

Commit 072d521

Browse files
committed
Improved unit tests to detect exception type
1 parent 3c033fa commit 072d521

5 files changed

Lines changed: 50 additions & 30 deletions

Tests/Unit/MSFT_xComputer.Tests.ps1

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
$script:DSCModuleName = 'xComputerManagement'
22
$script:DSCResourceName = 'MSFT_xComputer'
33

4+
Import-Module -Name (Join-Path -Path (Join-Path -Path (Split-Path $PSScriptRoot -Parent) -ChildPath 'TestHelpers') -ChildPath 'CommonTestHelper.psm1') -Global
5+
46
# Unit Test Template Version: 1.2.0
57
$script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
68
if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or `
@@ -48,22 +50,29 @@ try
4850
}
4951

5052
It 'Throws if both DomainName and WorkGroupName are specified' {
53+
$errorRecord = Get-InvalidOperationRecord `
54+
-Message ($LocalizedData.DomainNameAndWorkgroupNameError)
55+
5156
{
5257
Test-TargetResource `
5358
-Name $Env:ComputerName `
5459
-DomainName 'contoso.com' `
5560
-WorkGroupName 'workgroup' `
5661
-Verbose
57-
} | Should Throw
62+
} | Should Throw $errorRecord
5863
}
5964

6065
It 'Throws if Domain is specified without Credentials' {
66+
$errorRecord = Get-InvalidArgumentRecord `
67+
-Message ($LocalizedData.CredentialsNotSpecifiedError) `
68+
-ArgumentName 'Credentials'
69+
6170
{
6271
Test-TargetResource `
6372
-Name $Env:ComputerName `
6473
-DomainName 'contoso.com' `
6574
-Verbose
66-
} | Should Throw
75+
} | Should Throw $errorRecord
6776
}
6877

6978
It 'Should return True if Domain name is same as specified' {
@@ -474,25 +483,32 @@ try
474483
Mock -CommandName Set-CimInstance
475484

476485
It 'Throws if both DomainName and WorkGroupName are specified' {
486+
$errorRecord = Get-InvalidOperationRecord `
487+
-Message ($LocalizedData.DomainNameAndWorkgroupNameError)
488+
477489
{
478490
Set-TargetResource `
479491
-Name $Env:ComputerName `
480492
-DomainName 'contoso.com' `
481493
-WorkGroupName 'workgroup' `
482494
-Verbose
483-
} | Should Throw
495+
} | Should Throw $errorRecord
484496

485497
Assert-MockCalled -CommandName Rename-Computer -Exactly -Times 0 -Scope It
486498
Assert-MockCalled -CommandName Add-Computer -Exactly -Times 0 -Scope It
487499
}
488500

489501
It 'Throws if Domain is specified without Credentials' {
502+
$errorRecord = Get-InvalidArgumentRecord `
503+
-Message ($LocalizedData.CredentialsNotSpecifiedError) `
504+
-ArgumentName 'Credentials'
505+
490506
{
491507
Set-TargetResource `
492508
-Name $Env:ComputerName `
493509
-DomainName 'contoso.com' `
494510
-Verbose
495-
} | Should Throw
511+
} | Should Throw $errorRecord
496512

497513
Assert-MockCalled -CommandName Rename-Computer -Exactly -Times 0 -Scope It
498514
Assert-MockCalled -CommandName Add-Computer -Exactly -Times 0 -Scope It

Tests/Unit/MSFT_xOfflineDomainJoin.tests.ps1

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,38 @@
1-
$Global:DSCModuleName = 'xComputerManagement'
2-
$Global:DSCResourceName = 'MSFT_xOfflineDomainJoin'
1+
$script:DSCModuleName = 'xComputerManagement'
2+
$script:DSCResourceName = 'MSFT_xOfflineDomainJoin'
33

4-
#region HEADER
5-
[String] $moduleRoot = Split-Path -Parent (Split-Path -Parent (Split-Path -Parent $Script:MyInvocation.MyCommand.Path))
6-
if ( (-not (Test-Path -Path (Join-Path -Path $moduleRoot -ChildPath 'DSCResource.Tests'))) -or `
7-
(-not (Test-Path -Path (Join-Path -Path $moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) )
8-
{
9-
& git @('clone','https://github.com/PowerShell/DscResource.Tests.git',(Join-Path -Path $moduleRoot -ChildPath '\DSCResource.Tests\'))
10-
}
11-
else
4+
Import-Module -Name (Join-Path -Path (Join-Path -Path (Split-Path $PSScriptRoot -Parent) -ChildPath 'TestHelpers') -ChildPath 'CommonTestHelper.psm1') -Global
5+
6+
# Unit Test Template Version: 1.2.0
7+
$script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
8+
if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or `
9+
(-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) )
1210
{
13-
& git @('-C',(Join-Path -Path $moduleRoot -ChildPath '\DSCResource.Tests\'),'pull')
11+
& git @('clone','https://github.com/PowerShell/DscResource.Tests.git',(Join-Path -Path $script:moduleRoot -ChildPath '\DSCResource.Tests\'))
1412
}
15-
Import-Module (Join-Path -Path $moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force
13+
14+
Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force
15+
1616
$TestEnvironment = Initialize-TestEnvironment `
17-
-DSCModuleName $Global:DSCModuleName `
18-
-DSCResourceName $Global:DSCResourceName `
19-
-TestType Unit
20-
#endregion
17+
-DSCModuleName $script:DSCModuleName `
18+
-DSCResourceName $script:DSCResourceName `
19+
-TestType Unit
20+
#endregion HEADER
2121

2222
# Begin Testing
2323
try
2424
{
2525
#region Pester Tests
2626

27-
InModuleScope $Global:DSCResourceName {
27+
InModuleScope $script:DSCResourceName {
28+
$script:DSCResourceName = 'MSFT_xOfflineDomainJoin'
2829

2930
$TestOfflineDomainJoin = @{
3031
IsSingleInstance = 'Yes'
3132
RequestFile = 'C:\ODJRequest.txt'
3233
}
3334

34-
Describe "$($Global:DSCResourceName)\Get-TargetResource" {
35+
Describe "$($script:DSCResourceName)\Get-TargetResource" {
3536

3637
It 'should return the correct values' {
3738
$Result = Get-TargetResource `
@@ -42,7 +43,7 @@ try
4243
}
4344
}
4445

45-
Describe "$($Global:DSCResourceName)\Set-TargetResource" {
46+
Describe "$($script:DSCResourceName)\Set-TargetResource" {
4647
Mock Test-Path -MockWith { return $True }
4748
Mock Join-Domain
4849

@@ -77,8 +78,8 @@ try
7778
}
7879
}
7980
}
80-
81-
Describe "$($Global:DSCResourceName)\Test-TargetResource" {
81+
82+
Describe "$($script:DSCResourceName)\Test-TargetResource" {
8283
Mock Test-Path -MockWith { return $True }
8384
Mock Get-DomainName -MockWith { return $null }
8485

@@ -126,8 +127,8 @@ try
126127
}
127128
}
128129

129-
Describe "$($Global:DSCResourceName)\Join-Domain" {
130-
Mock djoin.exe -MockWith { $Global:LASTEXITCODE = 0; return "OK" }
130+
Describe "$($script:DSCResourceName)\Join-Domain" {
131+
Mock djoin.exe -MockWith { $script:LASTEXITCODE = 0; return "OK" }
131132

132133
Context 'Domain Join successful' {
133134
It 'should not throw' {
@@ -138,7 +139,7 @@ try
138139
}
139140
}
140141

141-
Mock djoin.exe -MockWith { $Global:LASTEXITCODE = 99; return "ERROR" }
142+
Mock djoin.exe -MockWith { $script:LASTEXITCODE = 99; return "ERROR" }
142143

143144
Context 'Domain Join successful' {
144145
$errorId = 'DjoinError'

Tests/Unit/MSFT_xPowerPlan.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
$script:DSCModuleName = 'xComputerManagement'
22
$script:DSCResourceName = 'MSFT_xPowerPlan'
33

4-
#region HEADER
4+
Import-Module -Name (Join-Path -Path (Join-Path -Path (Split-Path $PSScriptRoot -Parent) -ChildPath 'TestHelpers') -ChildPath 'CommonTestHelper.psm1') -Global
55

66
# Unit Test Template Version: 1.2.0
77
$script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)

Tests/Unit/MSFT_xScheduledTask.Tests.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ param(
55
$script:DSCModuleName = 'xComputerManagement'
66
$script:DSCResourceName = 'MSFT_xScheduledTask'
77

8+
Import-Module -Name (Join-Path -Path (Join-Path -Path (Split-Path $PSScriptRoot -Parent) -ChildPath 'TestHelpers') -ChildPath 'CommonTestHelper.psm1') -Global
9+
810
# Unit Test Template Version: 1.2.0
911
$script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
1012
if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or `

Tests/Unit/MSFT_xVirtualMemory.Tests.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
$script:DSCModuleName = 'xComputerManagement'
33
$script:DSCResourceName = 'MSFT_xVirtualMemory'
44

5+
Import-Module -Name (Join-Path -Path (Join-Path -Path (Split-Path $PSScriptRoot -Parent) -ChildPath 'TestHelpers') -ChildPath 'CommonTestHelper.psm1') -Global
6+
57
# Unit Test Template Version: 1.2.0
68
$script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
79
if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or `
@@ -15,7 +17,6 @@ $TestEnvironment = Initialize-TestEnvironment `
1517
-DSCModuleName $script:DSCModuleName `
1618
-DSCResourceName $script:DSCResourceName `
1719
-TestType Unit
18-
1920
#endregion HEADER
2021

2122
function Invoke-TestSetup {

0 commit comments

Comments
 (0)