diff --git a/framework/src/source/BaseTestSuite.bs b/framework/src/source/BaseTestSuite.bs index a9d17cca..bb25dba3 100644 --- a/framework/src/source/BaseTestSuite.bs +++ b/framework/src/source/BaseTestSuite.bs @@ -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) @@ -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, @@ -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 @@ -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 diff --git a/tests/src/source/NewExpectSyntax.spec.bs b/tests/src/source/NewExpectSyntax.spec.bs index 7148c7b3..3e81f8bf 100644 --- a/tests/src/source/NewExpectSyntax.spec.bs +++ b/tests/src/source/NewExpectSyntax.spec.bs @@ -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 '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ @@ -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 \ No newline at end of file