如何在pester

时间:2018-03-22 17:55:21

标签: powershell powershell-v3.0 powershell-v4.0 pester

这是我的代码: 的 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
    }

    }

这里我想模拟一个带有强制参数的powershell函数,它不会向用户请求$ PSFilePath变量值但是使用任何虚拟值检查mock函数。 每当我运行 Testcase.Tests.ps1 时,它会提示$ PSFilePath值并运行source powershell脚本( Testcase.ps1 ) 我坚持这一点,任何建议都会有很大的帮助。

1 个答案:

答案 0 :(得分:1)

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 
    }
}