def test_recorded_calls_work_on_several_stubs(self): when(self.spy.one_arg_method).with_args(2).then_return(3) when(self.spy.one_arg_method).with_args(3).then_return(4) self.spy.one_arg_method(2) self.spy.one_arg_method(3) assert_that_was_called(self.spy.one_arg_method).with_args(2) assert_that_was_called(self.spy.one_arg_method).with_args(3)
def test_if_doesnt_match_message_is_human_redable(self): self.spy.one_arg_method("XabcX") try: assert_that_was_called(self.spy.one_arg_method).with_args( str_containing("xxx")) except ArgsDontMatch as e: self.assertTrue("xxx" in str(e.args[0]), str(e.args[0])) self.assertTrue("string containing" in str(e.args[0]))
def test_stub_methods_can_be_handled_separately(self): when(self.spy.one_arg_method).with_args(1).then_return(1000) when(self.spy.two_args_method).with_args(5, 5).then_return(2000) handle1 = self.spy.one_arg_method handle2 = self.spy.two_args_method self.assertEqual(1000, handle1(1)) self.assertEqual(2000, handle2(5, 5)) assert_that_was_called(handle1).with_args(1) assert_that_was_called(handle2).with_args(5, 5)
def test_fail_missing_fluent_method(self): try: self.spy.one_arg_method(1) assert_that_was_called(self.spy.one_arg_method).with_params( 2) # should be with_args self.fail("TypeError should be raised") except AttributeError as e: expected = "'assert_that_method' object has no attribute 'with_params'" hamcrest.assert_that(str(e), hamcrest.contains_string(expected))
def test_stub_works_with_alias_method(self): when(self.spy.one_arg_method).with_args(1).then_return(1000) self.spy.alias_method(1) assert_that_was_called(self.spy.one_arg_method).with_args(1)
def test_any_arg_matcher_was_called(self): when(self.spy.two_args_method).with_args(1, 2).then_return(1000) self.spy.two_args_method(1, 2) assert_that_was_called(self.spy.two_args_method).with_args(1, ANY_ARG)
def test_one_method_called_other_wasnt(self): self.spy.something() with self.assertRaises(UnexpectedBehavior): assert_that_was_called(self.spy.hello)
def test_assert_was_called_when_wasnt(self): with self.assertRaises(UnexpectedBehavior): assert_that_was_called(self.spy.hello)
def test_str_not_containing_was_called(self): self.spy.one_arg_method("abc") assert_that_was_called(self.spy.one_arg_method).with_args( str_not_containing("xxx"))
def test_match_call_with_unicode_and_non_ascii_chars(self): non_ascii = u'EspaƱa' self.spy.one_arg_method(non_ascii) assert_that_was_called(self.spy.one_arg_method).with_args(non_ascii)
def test_was_called_with_keyed_args_matching(self): self.spy.kwarg_method(key_param="foo") assert_that_was_called( self.spy.kwarg_method).with_args(key_param="foo")
def test_was_called_with_keyed_args_not_matching(self): self.spy.kwarg_method(key_param="foo") args_checker = assert_that_was_called(self.spy.kwarg_method) with self.assertRaises(ArgsDontMatch): args_checker.with_args(key_param="bar")
def test_was_called_with_parameters_not_matching(self): self.spy.one_arg_method(1) args_checker = assert_that_was_called(self.spy.one_arg_method) with self.assertRaises(ArgsDontMatch): args_checker.with_args("2")
def test_was_called_with_several_parameters(self): self.spy.two_args_method(1, 2) args_checker = assert_that_was_called(self.spy.two_args_method) args_checker.with_args(1, 2)
def test_was_called_with_same_params_but_no_params_accepted(self): self.spy.hello() args_checker = assert_that_was_called(self.spy.hello) with self.assertRaises(TypeError): args_checker.with_args("something")
def test_was_called_with_same_parameters_in_variables(self): arg1 = 1 self.spy.one_arg_method(arg1) assert_that_was_called(self.spy.one_arg_method).with_args(1)
def test_was_called_with_same_parameters(self): self.spy.one_arg_method(1) assert_that_was_called(self.spy.one_arg_method).with_args(1)
def test_override_original_method_and_is_called(self): self.spy.hello() assert_that_was_called(self.spy.hello)
def test_spy_can_work_from_empty_and_is_called(self): self.spy.hello() assert_that_was_called(self.spy.hello)
def test_recorded_call_params_are_displayed(self): self.spy.kwarg_method(key_param="foo") try: assert_that_was_called(self.spy.kwarg_method).with_args("bar") except ArgsDontMatch as e: self.assertTrue(str(e).find("foo") != -1, str(e))
def test_stub_method_returning_list_was_called(self): when(self.spy.one_arg_method).then_return([1, 2, 3]) self.spy.one_arg_method(5) assert_that_was_called(self.spy.one_arg_method).with_args(5)
def test_assert_was_called(self): self.spy.hello() assert_that_was_called(self.spy.hello)
def test_was_called_and_substr_matcher(self): self.spy.one_arg_method("XabcX") assert_that_was_called(self.spy.one_arg_method).with_args( str_containing("abc"))
def test_use_in_spy_call(self): self.spy.one_arg_method('ttxe') assert_that_was_called(self.spy.one_arg_method).with_args( starts_with('tt'))
def test_stub_method_with_args_was_called(self): when(self.spy.one_arg_method).with_args(2).then_return(3) self.spy.one_arg_method(2) assert_that_was_called(self.spy.one_arg_method).with_args(2)
def test_two_methods_called_assert_on_the_first(self): self.spy.hello() self.spy.something() assert_that_was_called(self.spy.hello)
def test_assert_was_called_on_any_method(self): self.spy.something() assert_that_was_called(self.spy.something)
def test_stub_out_method_with_args_calls_actual(self): when(self.spy.one_arg_method).with_args(2).then_return(3) self.assertEqual(4, self.spy.one_arg_method(4)) assert_that_was_called(self.spy.one_arg_method).with_args(4)
def test_equal_to_ignoring_case_matcher(self): self.spy.one_arg_method('hello') assert_that_was_called(self.spy.one_arg_method).with_args( equal_to_ignoring_case('HEllO'))
def test_assert_needs_always_a_method_from_a_double_not_the_original(self): with self.assertRaises(WrongApiUsage): assert_that_was_called(Collaborator().hello)