예제 #1
0
 def test_stub_raising_exception(self):
     when(self.spy.hello).then_raise(SomeException())
     try:
         self.spy.hello()
         self.fail("not raised")
     except SomeException:
         pass
예제 #2
0
    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)
예제 #3
0
    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))
예제 #4
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)
예제 #5
0
    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")
예제 #6
0
    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"))
예제 #7
0
    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"))
예제 #8
0
    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"))
예제 #9
0
    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"))
예제 #10
0
    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))
예제 #11
0
 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'))
예제 #12
0
 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)
예제 #13
0
    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)
예제 #14
0
    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)
예제 #15
0
    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))
예제 #16
0
    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))
예제 #17
0
    def test_stub_returning_what_receives_when_no_params(self):
        when(self.spy.hello).then_return_input()

        with self.assertRaises(ApiMismatch):
            self.spy.hello()
예제 #18
0
    def test_stub_returning_what_receives(self):
        when(self.spy.method_one).then_return_input()

        self.assertEqual(20, self.spy.method_one(20))
예제 #19
0
    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))
예제 #20
0
    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"))
예제 #21
0
    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"))
예제 #22
0
 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))
예제 #23
0
    def test_restricted_stub(self):
        my_stub = stub(Collaborator())
        when(my_stub.something).then_return(10)

        self.assertEqual(10, my_stub.something())
예제 #24
0
 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))
예제 #25
0
    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)
예제 #26
0
    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))
예제 #27
0
 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))
예제 #28
0
    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"))
예제 #29
0
 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))
예제 #30
0
    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)