の機能は、ばらばらなセットのAssert-VerifiableMocks
サブセットではありません。多くのモックが同時に呼び出されていることを確認できますが、既に述べたように、モックが呼び出されていないことを確認することはできませんAssert-MockCalled
。Assert-VerifiableMocks
「Get-ValueSum」の例をテストするこれら 2 つの違いをより完全に説明するために、以下に例を示します。
気がついたら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
}
}