簡體   English   中英

Pester 中的 Assert-MockCalled 與 Assert-VerifiableMocks

[英]Assert-MockCalled vs Assert-VerifiableMocks in Pester

是什么讓 Assert-VerifiableMocks 與 Pester 中的 Assert-MockCalled 不同? 我一直在閱讀

但仍然想知道:以下代碼段是否等效並可互換?

使用 Assert-MockCalled:

Mock Invoke-MongoCommmand {}
Set-TargetResource -UserName $test_username -Ensure "Absent"
Assert-MockCalled Invoke-MongoCommand -ParameterFilter {
    $Expression -eq "db.dropUser('$test_username')"
}

使用 Assert-VerifiableMocks:

Mock Invoke-MongoCommand {} -Verifiable -ParameterFilter {
    $Expression -eq "db.dropUser('$test_username')"
}
Set-TargetResource -UserName $test_username -Ensure "Absent"
Assert-VerifiableMocks

的功能Assert-VerifiableMocks不是一個子集Assert-MockCalled這是一個不相交集。 使用Assert-VerifiableMocks您可以驗證同時調用多個Assert-VerifiableMocks ,但如前所述,您無法驗證未調用任何模擬。 我在下面有一個示例來更全面地解釋這兩個差異,測試示例“Get-ValueSum”。

如果您注意到Assert-verifiableMocks您可以驗證測試是否在單個斷言中調用了所有預期的模擬。

斷言可驗證示例

describe 'Assert-MockCalled vs Assert-VerifiableMocks in Pester' {
    function Get-Value1{ 1}
    function Get-Value2{ 2}
    function Get-Value3{ 3}
    
    # Should never be called by Get-ValueSum
    function Get-Value4{ 4}

    # Sums Value 1, 2 & 3, but 4 only if $include4 is specified
    function Get-ValueSum
    {
        param([switch] $inclued4 )
        if($inclued4)
        {
            return (Get-Value1) + (Get-Value2) + (Get-Value3) + (Get-Value4)
        }
        else
        {
            return (Get-Value1) + (Get-Value2) + (Get-Value3)
        }
    }

    context 'assert verifiable' {
        # Mark the first 3 mocks as verifiable 
        # because they should not be called 
        mock -CommandName Get-Value1 -MockWith { 2} -Verifiable
        mock -CommandName Get-Value2 -MockWith { 3} -Verifiable
        mock -CommandName Get-Value3 -MockWith { 4} -Verifiable
        # Add this so we can verify it is not called
        mock -CommandName Get-Value4 -MockWith { 99} 
        
        $result = Get-ValueSum

        it 'Should call the 3 expected value calls' {
            Assert-VerifiableMock
        }

        it 'should not call get-value 4' {
            Assert-MockCalled -CommandName Get-Value4 -Times 0
        }

        it 'should have gotten a sum of 9' {
            $result | should be 9
        }
        # add test for #$include4
    }
}

Assert-MockCalled 示例

describe 'Assert-MockCalled vs Assert-VerifiableMocks in Pester' {
    function Get-Value1{ 1}
    function Get-Value2{ 2}
    function Get-Value3{ 3}
    
    # Should never be called by Get-ValueSum
    function Get-Value4{ 4}

    # Sums Value 1, 2 & 3, but 4 only if $include4 is specified
    function Get-ValueSum
    {
        param([switch] $inclued4 )
        if($inclued4)
        {
            return (Get-Value1) + (Get-Value2) + (Get-Value3) + (Get-Value4)
        }
        else
        {
            return (Get-Value1) + (Get-Value2) + (Get-Value3)
        }
    }

    context 'assert mock called method' {
        # Add all mocks so we can verify 
        # if they were called or not individually
        mock -CommandName Get-Value1 -MockWith { 3} 
        mock -CommandName Get-Value2 -MockWith { 4} 
        mock -CommandName Get-Value3 -MockWith { 5} 
        mock -CommandName Get-Value4 -MockWith { 99} 

        $result = Get-ValueSum

        it 'Should call the 3 expected value calls' {
            Assert-MockCalled -CommandName Get-Value1 -Times 1
            Assert-MockCalled -CommandName Get-Value2 -Times 1
            Assert-MockCalled -CommandName Get-Value3 -Times 1
        }

        it 'should not call get-value 4' {
            Assert-MockCalled -CommandName Get-Value4 -Times 0
        }

        it 'should have gotten a sum of 12' {
            $result | should be 12
        }
        # add test for #$include4
    }
}

在您的示例中,它們是相同的,但它是關於您要檢查的方式和內容。 例如,您想檢查是否未調用某些內容。 你可能會使用:

Assert-MockCalled xxx -Times 0 -ParameterFilter {$x -eq "foo"}

這對於 Assert-VerifiableMocks 是不可能的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM