Skip to content

Commit

Permalink
adding new Assert-MockCalled feature: exciting stuff!
Browse files Browse the repository at this point in the history
  • Loading branch information
mwrock committed Oct 22, 2012
1 parent 7faa656 commit dd7dca2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
17 changes: 16 additions & 1 deletion Functions/Mock.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ Describe "When Creaing a Verifiable Mock that is called" {
$result=$_
}

It "Should not throw" {
It "Assert-VerifiableMocks Should not throw" {
$result.should.be("")
}
}
Expand All @@ -176,3 +176,18 @@ Describe "When Creaing a Verifiable Mock with a filter that does not return a bo
$result.should.be("The Parameter Filter must return a boolean")
}
}

Describe "When Calling a filterless mock once with params" {
Mock FunctionUnderTest {}
FunctionUnderTest "one"

try {
Assert-MockCalled FunctionUnderTest 0
} Catch {
$result=$_
}

It "Assert-MockCalled 0 Should throw" {
$result.Exception.Message.should.be("Expected FunctionUnderTest to be called 0 times")
}
}
13 changes: 13 additions & 0 deletions Functions/Mock.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
$script:mockTable = @{}
$script:callHistory = @()

function Mock {

Expand Down Expand Up @@ -170,14 +171,26 @@ This will not throw an exception because the mock was invoked.
}
}

function Assert-MockCalled {
param(
[string]$commandName,
[switch]$Exactly,
[int]$times=1,
[ScriptBlock]$parameterFilter = {$True}
)

}

function Clear-Mocks {
$mockTable.Keys | % { Microsoft.PowerShell.Management\Remove-Item function:\$_ }
$mockTable.Clear()
$callHistory.Clear()
Get-ChildItem Function: | ? { $_.Name.StartsWith("PesterIsMocking_") } | % {Rename-Item Function:\$_ "script:$($_.Name.Replace('PesterIsMocking_', ''))"}
}

function MockPrototype {
$functionName = $MyInvocation.MyCommand.Name
$callHistory += @{CommandName=$functionName;Params=$PSBoundParameters}
$mock=$mockTable.$functionName
$idx=$mock.blocks.Length
while(--$idx -ge 0) {
Expand Down

0 comments on commit dd7dca2

Please sign in to comment.