是什么让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
答案 0 :(得分:0)
在你的样本中它们是相同的,但它是关于你想要检查的方式和内容。例如,您要检查是否未调用某些内容。您可能会使用:
Assert-MockCalled xxx -Times 0 -ParameterFilter {$x -eq "foo"}
使用Assert-VerifiableMocks
这是不可能的答案 1 :(得分:0)
Assert-VerifiableMocks
的功能不是Assert-MockCalled
的一个子集,它是一个不相交的集合。使用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)
}
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-VerifiableMocks
}
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)
}
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
}
}