コード例 #1
0
 def test_was_never_called_is_false(self):
     self.spy.one_arg_method(1)
     try:
         assert_that_method(self.spy.one_arg_method).was_never_called()
         self.fail("it was called indeed!")
     except UnexpectedBehavior:
         pass
コード例 #2
0
 def test_matcher_error_is_human_readable(self):
     self.spy.one_arg_method('xe')
     try:
         assert_that_method(self.spy.one_arg_method).was_called().with_args(
             starts_with('tt'))
     except ArgsDontMatch as e:
         self.assertTrue("tt" in str(e.args[0]))
         self.assertTrue("string starting" in str(e.args[0]))
コード例 #3
0
 def test_obj_with_field_defends_agains_wrong_usage(self):
     self.spy.one_arg_method(Collaborator())
     try:
         assert_that_method(self.spy.one_arg_method).was_called().with_args(
             obj_with_fields('id = 20'))
         self.fail('Wrong assertion, argument should be a dictionary')
     except WrongApiUsage:
         pass
コード例 #4
0
    def test_expect_several_times(self):
        self.spy.one_arg_method(1)

        try:
            assert_that_method(self.spy.one_arg_method).was_called().times(2)
            self.fail("Should have been called 2 times")
        except UnexpectedBehavior:
            pass
コード例 #5
0
 def test_args_match_but_not_number_of_times(self):
     self.spy.one_arg_method(1)
     self.spy.one_arg_method(2)
     try:
         assert_that_method(
             self.spy.one_arg_method).was_called().with_args(1).times(2)
         self.fail("Wrong assertion")
     except UnexpectedBehavior:
         pass
コード例 #6
0
    def test_expect_several_times_with_incorrect_args(self):
        self.spy.one_arg_method(1)
        self.spy.one_arg_method(1)

        try:
            assert_that_method(
                self.spy.one_arg_method).was_called().with_args(2).times(2)
            self.fail("Must have 1 as an argument")
        except ArgsDontMatch:
            pass
コード例 #7
0
    def test_fail_incorrect_times_msg_is_human_readable(self):
        self.spy.one_arg_method(1)

        try:
            assert_that_method(self.spy.one_arg_method).was_called().times(5)
            self.fail("Should have been called 2 times")
        except UnexpectedBehavior as e:
            for arg in e.args:
                if re.search("5", str(arg)) and re.search(
                        "one_arg_method", str(arg)):
                    return
            self.fail("No enough readable exception message")
コード例 #8
0
 def test_obj_with_several_fields_matcher(self):
     obj = Collaborator()
     obj.id = 21
     self.spy.one_arg_method(obj)
     try:
         assert_that_method(self.spy.one_arg_method).was_called().with_args(
             obj_with_fields({
                 'id': 20,
                 'test_field': 'OK'
             }))
         self.fail('Wrong assertion, id field is different')
     except ArgsDontMatch:
         pass
コード例 #9
0
    def test_compare_objects_is_not_possible_without_eq_operator(self):
        class SomeObject():
            field1 = field2 = None

        obj = SomeObject()
        obj2 = SomeObject()
        self.spy.one_arg_method(obj)

        try:
            assert_that_method(
                self.spy.one_arg_method).was_called().with_args(obj2)
            self.fail('they should not match')
        except ArgsDontMatch:
            pass
コード例 #10
0
    def test_other_way_of_assert_called(self):
        self.spy.hello()

        assert_that_method(self.spy.hello).was_called()
コード例 #11
0
 def test_obj_with_field_matcher(self):
     obj = Collaborator()
     obj.id = 20
     self.spy.one_arg_method(obj)
     assert_that_method(self.spy.one_arg_method).was_called().with_args(
         obj_with_fields({'id': 20}))
コード例 #12
0
 def test_expect_several_times_with_args_definition(self):
     self.spy.one_arg_method(1)
     self.spy.one_arg_method(1)
     assert_that_method(
         self.spy.one_arg_method).was_called().with_args(1).times(2)
コード例 #13
0
 def test_expect_several_times_matches_exactly(self):
     self.spy.one_arg_method(1)
     self.spy.one_arg_method(1)
     assert_that_method(self.spy.one_arg_method).was_called().times(2)
コード例 #14
0
 def test_was_never_called(self):
     assert_that_method(self.spy.one_arg_method).was_never_called()