Skip to content

Commit

Permalink
Issue-dsccommunity#333 Assert-MockCalled in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob-S committed Jun 19, 2020
1 parent b130b70 commit f0b1d7b
Showing 1 changed file with 37 additions and 62 deletions.
99 changes: 37 additions & 62 deletions tests/Unit/DSC_ScheduledTask.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -114,31 +114,7 @@ try

$scheduledTask = $NULL
Mock -CommandName Get-ScheduledTask -MockWith { return $scheduledTask }
Mock -CommandName Register-ScheduledTask -MockWith {
$scheduledTask = @{
TaskName = $testParameters.TaskName
TaskPath = $testParameters.TaskPath
Actions = @(@{
Execute = $testParameters.ActionExecutable
})
Triggers = @(@{
Repetition = @{
Duration = "PT$([System.TimeSpan]::Parse($testParameters.RepetitionDuration).TotalMinutes)M"
Interval = "PT$([System.TimeSpan]::Parse($testParameters.RepeatInterval).TotalMinutes)M"
}
CimClass = @{
CimClassName = 'MSFT_TaskTimeTrigger'
}
})
Settings = [pscustomobject] @{
Enabled = $true
MultipleInstances = 'IgnoreNew'
}
Principal = @{
UserId = 'SYSTEM'
}
}
}
Mock -CommandName Register-ScheduledTask

It 'Should return the correct values from Get-TargetResource' {
$result = Get-TargetResource @getTargetResourceParameters
Expand All @@ -151,53 +127,25 @@ try

It 'Should create the scheduled task in the set method' {
Set-TargetResource @testParameters
Assert-MockCalled Register-ScheduledTask
}

It 'Should return the correct values from Get-TargetResource' {
$result = Get-TargetResource @getTargetResourceParameters
$result.Ensure | Should -Be 'Present'
Assert-MockCalled Register-ScheduledTask -Exactly -Times 1
}

}

Context 'No scheduled task exists, but it should, with MultipleInstances = StopExisting' {
Context 'No scheduled task exists, but it should, with MultipleInstances = Queue' {
$testParameters = $getTargetResourceParameters + @{
ActionExecutable = 'C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe'
ScheduleType = 'Once'
RepeatInterval = (New-TimeSpan -Minutes 15).ToString()
RepetitionDuration = (New-TimeSpan -Minutes 150).ToString()
MultipleInstances = 'StopExisting'
MultipleInstances = 'Queue'
Verbose = $true
}

$scheduledTask = $NULL
Mock -CommandName Get-ScheduledTask -MockWith { return $scheduledTask }
Mock -CommandName Register-ScheduledTask -MockWith {
$scheduledTask = @{
TaskName = $testParameters.TaskName
TaskPath = $testParameters.TaskPath
Actions = @(@{
Execute = $testParameters.ActionExecutable
})
Triggers = @(@{
Repetition = @{
Duration = "PT$([System.TimeSpan]::Parse($testParameters.RepetitionDuration).TotalMinutes)M"
Interval = "PT$([System.TimeSpan]::Parse($testParameters.RepeatInterval).TotalMinutes)M"
}
CimClass = @{
CimClassName = 'MSFT_TaskTimeTrigger'
}
})
Settings = [pscustomobject] @{
Enabled = $true
MultipleInstances = $testParameters.MultipleInstances
}
Principal = @{
UserId = 'SYSTEM'
}
}
}
Mock -CommandName New-ScheduledTaskSettingsSet
Mock -CommandName Register-ScheduledTask

It 'Should return the correct values from Get-TargetResource for initial state' {
$result = Get-TargetResource @getTargetResourceParameters
Expand All @@ -210,15 +158,42 @@ try

It 'Should create the scheduled task in the set method' {
Set-TargetResource @testParameters
Assert-MockCalled -CommandName Register-ScheduledTask -ParameterFilter {
Assert-MockCalled -CommandName New-ScheduledTaskSettingsSet -Exactly -Times 1 -ParameterFilter {
$MultipleInstances -eq $testParameters.MultipleInstances
}
Assert-MockCalled -CommandName Register-ScheduledTask -Exactly -Times 1
}

It 'Should return the correct values from Get-TargetResource' {
}

Context 'No scheduled task exists, but it should, with MultipleInstances = StopExisting' {
$testParameters = $getTargetResourceParameters + @{
ActionExecutable = 'C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe'
ScheduleType = 'Once'
RepeatInterval = (New-TimeSpan -Minutes 15).ToString()
RepetitionDuration = (New-TimeSpan -Minutes 150).ToString()
MultipleInstances = 'StopExisting'
Verbose = $true
}

$scheduledTask = $NULL
Mock -CommandName Get-ScheduledTask -MockWith { return $scheduledTask }
Mock -CommandName Register-ScheduledTask

It 'Should return the correct values from Get-TargetResource for initial state' {
$result = Get-TargetResource @getTargetResourceParameters
$result.Ensure | Should -Be 'Present'
$result.MultipleInstances | Should -Be $testParameters.MultipleInstances
$result.Ensure | Should -Be 'Absent'
}

It 'Should return false from the test method' {
Test-TargetResource @testParameters | Should -BeFalse
}

It 'Should create the scheduled task in the set method' {
Set-TargetResource @testParameters
Assert-MockCalled -CommandName New-ScheduledTaskSettingsSet -Exactly -Times 1
# currently does not have ParameterFilter to ensure MultipleInstances is being set, like 'Queue' above
Assert-MockCalled -CommandName Register-ScheduledTask -Exactly -Times 1
}

}
Expand Down

0 comments on commit f0b1d7b

Please sign in to comment.