Describe "EnsureRemotePath" -Tags UnitTest {
$Session = New-PSSession -computername localhost
Context "If the Path exists on the Remote Node" {
# Arrange
Mock -CommandName Invoke-Command -ParameterFilter { $ScriptBlock.ToString().Equals('Test-Path -Path $using:Path')} -MockWith {$True} -Verifiable
Mock -CommandName Invoke-Command -ParameterFilter { $ScriptBlock.ToString().Contains('New-Item')} -MockWith {}
# Act
EnsureRemotePath -Session $session -path 'C:\temp'
# Assert
It 'Should call Invoke-Command to check if the path on RemotelyNode exists' {
Assert-MockCalled -CommandName Invoke-Command -ParameterFilter { $ScriptBlock.ToString().Equals('Test-Path -Path $using:Path')} -Times 1 -Exactly
Assert-VerifiableMocks
}
It 'Should not create the path as it already exists' {
Assert-MockCalled -CommandName Invoke-Command -ParameterFilter { $ScriptBlock.ToString().Contains('New-Item')} -Times 0 -Exactly
}
}
Context "If the Path does NOT exist on the Remote Node, it gets created" {
# Arrange
Mock -CommandName Invoke-Command -ParameterFilter { $ScriptBlock.ToString().Equals('Test-Path -Path $using:Path')} -MockWith {$false} -Verifiable
Mock -CommandName Invoke-Command -ParameterFilter { $ScriptBlock.ToString().Contains('New-Item')} -MockWith {} -Verifiable
# Act
EnsureRemotePath -Session $session -path 'C:\temp'
# Assert
It 'Should create the path.' {
Assert-MockCalled -CommandName Invoke-Command -ParameterFilter { $ScriptBlock.ToString().Contains('New-Item')} -Times 1 -Exactly
Assert-VerifiableMocks
}
}
}