unit-testing - Pester 中的 Assert-MockCalled 与 Assert-VerABLEMocks

标签 unit-testing powershell pester

Assert-VerabilizedMocks 与 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')"
}

使用断言验证模拟:

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

最佳答案

Assert-VerifyingMocks 的功能不是 Assert-MockCalled 的子集,它是一个不相交的集合。使用 Assert-VerifyingMocks ,您可以验证同时调用许多模拟,但正如已经提到的,您无法验证没有调用任何模拟。我在下面有一个示例,可以更全面地解释测试“Get-ValueSum”示例的这两个差异。

如果您注意到使用 Assert-verABLEMocks,您可以验证测试是否已在单个断言中调用了所有预期的模拟。

断言可验证示例

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

断言模拟调用示例

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

关于unit-testing - Pester 中的 Assert-MockCalled 与 Assert-VerABLEMocks,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37031322/

相关文章:

powershell - 为什么 (return) 和 return 不同?

regex - 允许数字和星号的正则表达式模式

powershell - 使用 Pester 测试强制参数

class - Powershell 5 类的 Pester 模拟方法

powershell - "Path cannot be found"Pester测试结果

javascript - 需要一些类似访客的设计模式

angular - 如何对自定义组件进行 Angular 单元测试(实现 ControlValueAccessor 和继承的父表单控件)

java - 如何加载大量数据并将其用于多个测试用例?

ruby - 如何在 rspec 示例中删除全局日志记录功能

powershell - 如何使用 Set-StrictMode 忽略空引用异常?