def test_stub_raising_exception(self): when(self.spy.hello).then_raise(SomeException()) try: self.spy.hello() self.fail("not raised") except SomeException: pass
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_is_matcher(self): class Customer: pass customer = Customer() when(self.spy.one_arg_method).with_args( is_(customer)).then_return(1000) self.assertEqual(1000, self.spy.one_arg_method(customer))
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_do_not_call_callable_object_if_wasnt_generated_by_the_framework( self): class CallableObj(): just_testing = True def __call__(self, *args, **kwargs): raise Exception('should not happen') obj = CallableObj() when(self.spy.one_arg_method).then_return(obj) self.assertEqual(obj, self.spy.one_arg_method(1), "Wrong returned object")
def test_str_not_containing_stubs_anything_else(self): when(self.spy.one_arg_method).with_args( str_not_containing("abc")).then_return(1000) self.assertEqual(1000, self.spy.one_arg_method("xxx"))
def test_str_not_containing(self): when(self.spy.one_arg_method).with_args( str_not_containing("abc")).then_return(1000) self.assertNotEqual(1000, self.spy.one_arg_method("abc"))
def test_str_containing_but_matcher_not_used(self): when(self.spy.one_arg_method).with_args("abc").then_return(1000) self.assertNotEqual(1000, self.spy.one_arg_method("XabcX"))
def test_str_containing_with_substr_unicode(self): when(self.spy.one_arg_method).with_args( str_containing("abc")).then_return(1000) self.assertEqual(1000, self.spy.one_arg_method(u"XabcñX"))
def test_stub_with_kwargs(self): when(self.spy.kwarg_method).with_args(key_param=2).then_return(3) self.assertEqual(3, self.spy.kwarg_method(key_param=2)) self.assertEqual(6, self.spy.kwarg_method(key_param=6))
def test_use_in_stub_method(self): when(self.spy.one_arg_method).with_args( starts_with('tt')).then_return(1000) self.assertEqual(1000, self.spy.one_arg_method('ttxe'))
def test_matchers_when_passed_arg_is_none(self): when(self.spy.one_arg_method).with_args( str_length(5)).then_return(1000) self.assertTrue(self.spy.one_arg_method(None) is None)
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_any_arg_matcher(self): when(self.spy.two_args_method).with_args(1, ANY_ARG).then_return(1000) self.assertEqual(1000, self.spy.two_args_method(1, 2)) self.assertEqual(1000, self.spy.two_args_method(1, 5))
def test_be_able_to_return_objects(self): when(self.spy.one_arg_method).then_return(Collaborator()) collaborator = self.spy.one_arg_method(1) self.assertEqual(1, collaborator.one_arg_method(1))
def test_stub_returning_what_receives_when_no_params(self): when(self.spy.hello).then_return_input() with self.assertRaises(ApiMismatch): self.spy.hello()
def test_stub_returning_what_receives(self): when(self.spy.method_one).then_return_input() self.assertEqual(20, self.spy.method_one(20))
def test_stub_out_method_with_several_inputs(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.assertEqual(3, self.spy.one_arg_method(2)) self.assertEqual(4, self.spy.one_arg_method(3))
def test_several_matchers(self): when(self.spy.two_args_method).with_args( str_containing("abc"), str_containing("xxx")).then_return(1000) self.assertNotEqual(1000, self.spy.two_args_method("abc", "yyy"))
def test_str_length_matcher(self): when(self.spy.one_arg_method).with_args( str_length(5)).then_return(1000) self.assertEqual(1000, self.spy.one_arg_method("abcde"))
def test_instance_of_matcher(self): when(self.spy.one_arg_method).with_args( instance_of(int)).then_return(1000) self.assertEqual(1000, self.spy.one_arg_method(5))
def test_restricted_stub(self): my_stub = stub(Collaborator()) when(my_stub.something).then_return(10) self.assertEqual(10, my_stub.something())
def test_has_length_matcher(self): list = [10, 20, 30] when(self.spy.one_arg_method).with_args( has_length(3)).then_return(1000) self.assertEqual(1000, self.spy.one_arg_method(list))
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_matching_stub_definition_is_used(self): when(self.spy.one_arg_method).then_return(1000) when(self.spy.one_arg_method).with_args(2).then_return(3) self.assertEqual(3, self.spy.one_arg_method(2)) self.assertEqual(1000, self.spy.one_arg_method(8))
def test_all_of_matcher(self): text = 'hello' when(self.spy.one_arg_method).with_args( all_of(starts_with('h'), equal_to(text))).then_return(1000) self.assertEqual(1000, self.spy.one_arg_method(text))
def test_str_cotaining_with_exact_match(self): when(self.spy.one_arg_method).with_args( str_containing("abc")).then_return(1000) self.assertEqual(1000, self.spy.one_arg_method("abc"))
def test_has_entry_matcher(self): list = {'one': 1, 'two': 2} when(self.spy.one_arg_method).with_args(has_entry(equal_to('two'), 2)).then_return(1000) self.assertEqual(1000, self.spy.one_arg_method(list))
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)