def test_mocks_can_expect_calls_with_args(mocks): return_value = "Hello!" mock = mocks.mock(name='save') expects_call(mock).with_args("positional").returns(return_value) assert_raises(UnexpectedInvocationError, mock) assert_raises(UnexpectedInvocationError, lambda: mock("positional", key="word")) assert mock("positional") is return_value
def test_can_expect_calls_with_the_same_syntax_that_it_will_be_called_with(mocks): to_print = "Hello, hello, hello, what's going on here then?" to_return = "There are four lights!" printer = mocks.mock() expects_call(printer)(to_print).returns(to_return) assert printer(to_print) is to_return
def test_can_mock_methods_used_internally_by_mock(mocks): mock = mocks.mock() expects(mock)._mocked_calls expects(mock).save() allows(mock)._mocked_calls allows(mock).save() expects_call(mock) allows_call(mock) mock._mocked_calls() mock.save() mock() class UserRepository(object): def _base(self): pass based_mock = mocks.mock(UserRepository) allows(based_mock)._base
def test_unexpected_calls_on_mocks_display_mock_name_and_parameters(mocks): return_value = "Hello!" mock = mocks.mock(name='save') assert_raises_str(UnexpectedInvocationError, """Unexpected invocation: save() The following expectations on save did not match: No expectations set.""", mock) expects_call(mock).returns(return_value) mock() assert_raises_str(UnexpectedInvocationError, """Unexpected invocation: save() The following expectations on save did not match: save [expectation has already been satisfied]""", mock) assert_raises_str(UnexpectedInvocationError, """Unexpected invocation: save('positional', key='word') The following expectations on save did not match: save [expectation has already been satisfied]""", lambda: mock("positional", key="word"))
def function(mocks): mock = mocks.mock() expects_call(mock).with_args("positional", key="word")
def function(mocks): mock = mocks.mock() expects_call(mock)