Reputation: 117
Here is my code: Testcase.ps1
function Verify_AuthenticodeSignature{
Param(
[Parameter(Mandatory=$true)]
[string]$PSFilePath
)
Write-Host $PSFilePath
if (-Not (Test-Path $PSFilePath)){
echo "file does not exist"
exit 1
}
$Status=(Get-AuthenticodeSignature -FilePath $PSFilePath).Status
if($Status -ne "Valid"){
echo "Script is not signed"
}
}
Verify_AuthenticodeSignature
Testcase.Tests.ps1:
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
. "$here\$sut"
Describe "Testing" {
It "Test Path"{
Mock Test-Path{$false}
Mock echo{}
Verify_AuthenticodeSignature
Assert-MockCalled echo -Times 1
#Verify_AuthenticodeSignature -PSFilePath 'D:\abc\xyz.ps1' |should be "file does not exist"
}
It "Authentication"{
Mock Get-AuthenticodeSignature{ return "valid" }
Mock echo{}
Verify_AuthenticodeSignature
Assert-MockCalled Get-AuthenticodeSignature -Times 1
Assert-MockCalled echo -Times 1
}
}
Here I want to mock a powershell function with mandatory parameters in such a way that it doesn't ask for $PSFilePath variable value from user but check the mock function using any dummy value. Whenever i am running Testcase.Tests.ps1 it is prompting for $PSFilePath value and running the source powershell script(Testcase.ps1) I am stuck on this , any suggestion would be of great help.
Upvotes: 0
Views: 1150
Reputation: 3063
Create a temporary file at TestDrive:
Describe 'Describing test' {
it 'dummy test fail' {
Test-Path TestDrive:\Test.log | Should Be $True
}
it 'dummy test pass' {
'some file' | Out-File TestDrive:\Test.log
Test-Path TestDrive:\Test.log | Should Be $True
}
}
Upvotes: 1