tests/Add-Assembly.tests.ps1
param( [string] $targetFilePath = "$PSScriptRoot\..\functions\Add-Assembly.ps1" ) Describe "Validate Add-Assembly functionality"{ Context "Mock for add assemblies" { BeforeAll{ #Dot source or target file . $targetFilePath #Define functions target file calls here function Resolve-AssemblyFilePath{ param( [string] $assemblyFileName ) } [string] $testPath = "" [bool] $validateFileResult [bool] $validateLoadedAssembly #Mock it so we can get the verifiable part Mock Resolve-AssemblyFilePath {return $testPath} -Verifiable Mock Test-Path { return $validateFileResult} Mock Add-Type {} -Verifiable Mock Test-AssemblyLoaded { return $validateLoadedAssembly } } #Arrange - this should follow "happy path" of assembly found but not already loaded $validateFileResult = $true $validateLoadedAssembly = $false It "Calls Add-Type the correct number of times when assembly not loaded" { #Act Add-Assembly -Assemblies "test.dll" #Assert Assert-MockCalled Add-Type -Times 1 -Scope It } #Arrange - this should follow the path where the aseembly is found, but is already loaded $validateLoadedAssembly = $true It "Doesn't call Add-Type if loaded"{ #Act - try with an array Add-Assembly -Assemblies @("test.dll") #Assert Assert-MockCalled Add-Type -Times 0 -Scope It } #Arrange - this should follow the the path where the DLL isn't found $validateFileResult = $false $testPath = "" It "Calls ResolveAssembly if file not found and fails to call Add-Type if resolve-path doesn't work" { #Act Add-Assembly -Assemblies "test.dll" -ErrorVariable err -ErrorAction SilentlyContinue $err.Count | Should Not Be 0 #Assert Assert-MockCalled Resolve-AssemblyFilePath -Times 1 -Scope It Assert-MockCalled Add-Type -Times 0 -Scope It } } } |