簡體   English   中英

Pester如何模擬“測試存在(未找到)-創建-再次測試以確認創建”模式中的“測試”功能?

[英]How can Pester mock the “test” function in the pattern “test existence (not found) - create - test again to confirm creation”?

這是一些偽代碼,顯示要測試的功能:

function Set-Something
{
    if (Test-Something)
    {
        return $True
    }

    # Not found so do something to create it.
    Do-Something

    # Check it's been created successfully.
    if (Test-Something)
    {
        return $True
    }

    return $False
}

這必須是一個相當普遍的模式:“測試是否存在-如果找不到,請創建-再次測試以驗證創建”。 測試大多數分支非常簡單,但是如何在第一次調用Test-Something失敗然后第二次調用成功的地方測試分支呢?

到目前為止,這是我的測試代碼:

Describe 'Set-Something' {

    Context 'already exists' {
        Mock Test-Something { return $True }
        Mock Do-Something

        It 'returns True' {
            { Set-Something } | Should -Be $True
        }

        It 'does not call Do-Something' {
            Set-Something
            Assert-MockCalled Do-Something -Times 0 -Exactly
        }
    }

    Context 'does not already exist and creation fails' {
        Mock Test-Something { return $False }
        Mock Do-Something

        It 'calls Do-Something' {
            Set-Something
            Assert-MockCalled Do-Something -Times 1 -Exactly
        }

        It 'calls Test-Something twice' {
            Set-Something
            Assert-MockCalled Test-Something -Times 2 -Exactly
        }

        It 'returns False' {
            { Set-Something } | Should -Be $False
        }
    }

    Context 'does not already exist and creation succeeds' {
        Mock Test-Something { ?? }
        Mock Do-Something

        It 'calls Do-Something' {
            Set-Something
            Assert-MockCalled Do-Something -Times 1 -Exactly
        }

        It 'calls Test-Something twice' {
            Set-Something
            Assert-MockCalled Test-Something -Times 2 -Exactly
        }

        It 'returns True' {
            { Set-Something } | Should -Be $True
        }
    }
}

問題是“尚不存在,創建成功”。 需要對Test-Something進行模擬,以便它在第一次調用時失敗,而在第二次調用時成功。 傳遞給Test-Something的參數在每個調用中將是相同的,因此我不能使用ParameterFilter創建具有不同行為的Test-Something的兩個模擬。

我發現了兩種模擬方法:

1)使用“靜態”(即腳本作用域)變量記錄狀態

Context 'does not already exist and creation succeeds' {

    BeforeEach {
        $script:exists = $False
    }

    AfterAll {
        Remove-Variable exists -Scope Script
    }

    Mock Test-Something { 
        return $script:exists
    }

    Mock Do-Something {
        $script:exists = $True
    }

    It 'calls Do-Something' {
        Set-Something
        Assert-MockCalled Do-Something -Times 1 -Exactly
    }

    It 'calls Test-Something twice' {
        Set-Something
        Assert-MockCalled Test-Something -Times 2 -Exactly
    }

    It 'returns True' {
        { Set-Something } | Should -Be $True
    }
}

2)使用哈希表記錄狀態

Context 'does not already exist and creation succeeds' {

    BeforeEach {
        $mockState = @{
                        ItExists = $False    
                    }
    }

    Mock Test-Something { 
        return $mockState.ItExists
    }

    Mock Do-Something {
        $mockState.ItExists = $True
    }

    It 'calls Do-Something' {
        Set-Something
        Assert-MockCalled Do-Something -Times 1 -Exactly
    }

    It 'calls Test-Something twice' {
        Set-Something
        Assert-MockCalled Test-Something -Times 2 -Exactly
    }

    It 'returns True' {
        { Set-Something } | Should -Be $True
    }
}   

我個人喜歡哈希表,因為$mockState. ... $mockState. ...在我看來似乎比$script:...更能說明變量的用途。 同樣,如果測試並行化並且另一個Describe塊修改了相同的變量,則腳本范圍的變量可能會導致競爭。

暫無
暫無

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

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