Skip to content

Commit

Permalink
adds expectLastCallToThrowError so we can throw errors from fakes (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
georgejecook authored May 9, 2022
1 parent b2a8f79 commit 690826e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
17 changes: 14 additions & 3 deletions framework/src/source/BaseTestSuite.bs
Original file line number Diff line number Diff line change
Expand Up @@ -1603,14 +1603,17 @@ namespace rooibos
return fake
end function

function expectCalled(invocation as dynamic, returnValue = invalid as dynamic) as object
function expectCalled(invocation as dynamic, returnValue = invalid as dynamic, thrownError = invalid as dynamic) as object
'mock function body - the plugin replaces this
return invalid
end function

function _expectCalled(target, methodName, expectedArgs = invalid, returnValue = invalid as dynamic) as object
function _expectCalled(target, methodName, expectedArgs = invalid, returnValue = invalid as dynamic, thrownError = invalid as dynamic) as object
try
return m.mock(target, methodName, 1, expectedArgs, returnValue, true)
mock = m.mock(target, methodName, 1, expectedArgs, returnValue, true)
if thrownError <> invalid
mock.toThrow(thrownError)
end if
catch error
'bs:disable-next-line
m.currentResult.fail("Setting up mock failed: " + error.message, m.currentAssertLineNumber)
Expand Down Expand Up @@ -1884,9 +1887,11 @@ namespace rooibos
expectedArgsValues.push(defaultValue)
end if
end for
'todo - make into a class
fake = {
id: id,
target: target,
errorToThrow: invalid,
methodName: methodName,
returnValue: returnValue,
lineNumbers: lineNumbers,
Expand All @@ -1911,6 +1916,9 @@ namespace rooibos
'bs:disable-next-line
m.invocations++

if m.errorToThrow <> invalid
throw m.errorToThrow
end if
'bs:disable-next-line
if type(m.returnValue) = "roAssociativeArray" and m.returnValue.doesExist("multiResult")
'bs:disable-next-line
Expand All @@ -1935,6 +1943,9 @@ namespace rooibos
return m.returnValue
end if
end function
toThrow: function(error)
m.errorToThrow = error
end function
}
return fake
end function
Expand Down
43 changes: 40 additions & 3 deletions tests/src/source/NewExpectSyntax.spec.bs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,27 @@ namespace tests
@it("supports function pointer - therefore ignoring the params")
function _()
item = { "id": "node" }
m.expectCalled(item.getText, "test")

m.assertEqual(item.getText("any text"), "test")
'don't care about the args
m.expectCalled(item.getText, "some return value I want")

m.assertRunningTestIsPassed()
'I expect this arg only
m.expectCalled(item.getText("expected"), "some return value I want")

'callfunc with no args
m.expectCalled(item@.getText())
m.expectCalled(item@.getText("expected"))
m.expectCalled(item@.getText("expected"), "some return value I want")


'don't expect this to be called
m.expectNotCalled(item.getText())

item@.getText()

m.assertEqual(item.getText("any text"), "some return value I want")

m.assertRunningTestIsFailed()
end function

'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Expand Down Expand Up @@ -282,6 +298,27 @@ namespace tests
m.assertRunningTestIsFailed()
end function

'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
@describe("mocks with exceptions")
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

@it("throws exceptions")
function _()
thing = { "id": "thing" }

error = {message: "error"}
m.expectCalled(thing.thrownAnError())
m.expectLastCallToThrowError(error)

try
thing.thrownAnError()
catch error
m.assertEqual(error.message, "error")
end try

end function


end class

end namespace

0 comments on commit 690826e

Please sign in to comment.