def test_spy(self):

        playername = "Kawhi Leonard"
        year = 2017
        salary = "20m"
        #使用with关键字和Mock()创建mock object
        #假设替代salaryService对象
        #定义mock需要调用的方法及其参数,此方法与被替代的salaryService中的方法相同
        with Mock() as mock:
            mock.set_salary("20m")
        #在SUT playerservice中调用这个mock
        #之前定义的mock.set_salary("20m")会被SUT调用
        player_service_mock_2017 = pls.playerService(playername, year, ds.dataService(playername), pos.profileService(playername), bs.bodyService(), mock)
        player_service_mock_2017.set_new_salary(salary)
        #verify()验证定义的mock期望是否正确被实现
        assert_that(mock, verify())

        #假设替代dataService对象
        #mock可以设置返回值
        with Mock() as mock_order:
            mock_order.get_score().returns("22")
            mock_order.get_assist().returns("3")
            mock_order.get_rebound().returns("6")
            mock_order.get_match_number(year).returns("77")
        #在SUT playerservice中调用这个mock
        player_service_mock_2017_order = pls.playerService(playername, year, mock_order, pos.profileService(playername), bs.bodyService(), ss.salaryService())
        player_service_mock_2017_order.get_player_info()
        # verify()验证定义的mock期望是否正确被实现,且方法调用顺序必须完全一致
        assert_that(mock_order, verify())

        #假设替代dataService对象,注意mock定义中期望的顺序和之前不一样,也会和执行顺序不一致
        with Mock() as mock_any_order:
            mock_any_order.get_score().returns("22")
            mock_any_order.get_rebound().returns("6")
            mock_any_order.get_match_number(year).returns("77")
            mock_any_order.get_assist().returns("3")
        #在SUT playerservice中调用这个mock
        player_service_mock_2017_any_order = pls.playerService(playername, year, mock_any_order, pos.profileService(playername),
                                                         bs.bodyService(), ss.salaryService())
        player_service_mock_2017_any_order.get_player_info()
        #any_order_verify()验证定义的mock期望是否正确被实现,且方法调用顺序不要求完全一致
        assert_that(mock_any_order, any_order_verify())
Example #2
0
    def test_reads_a_line_and_prints_it(self):
        line = 'foo'
        stdin = FakeIO(line)
        with doublex.Mock() as log:
            log.log(line)

        sut = colorize.Printer(stdin, {}, log.log)

        sut.process()

        doublex.assert_that(log, doublex.verify())
    def test_prompt_renders_a_questions(self):
        question1 = dbx.Stub()
        question1.name = 'foo'
        result1 = object()
        with dbx.Mock() as render:
            render.render(question1, dbx.ANY_ARG).returns(result1)

        result = prompt([question1], render=render)

        self.assertEquals({'foo': result1}, result)
        dbx.assert_that(render, dbx.verify())
Example #4
0
    def test_reads_two_lines_and_prints_them(self):
        line = '1\n2'
        stdin = FakeIO(line)
        with doublex.Mock() as log:
            log.log('1')
            log.log('2')

        sut = colorize.Printer(stdin, {}, log.log)

        sut.process()

        doublex.assert_that(log, doublex.verify())
Example #5
0
    def test_replacement(self):
        line = 'foo'
        stdin = FakeIO(line)
        expected = 'ok'
        with doublex.Mock() as log:
            log.log(expected)
        regexps = {'foo': expected}

        sut = colorize.Printer(stdin, regexps, log.log)

        sut.process()

        doublex.assert_that(log, doublex.verify())
Example #6
0
    def test_account_behaviour_with_mock(self):
        with Stub(PasswordService) as password_service:
            password_service.generate().returns('some')

        with Mock(AccountStore) as store:
            store.has_user('John')
            store.save('John', 'some')
            store.has_user('Peter')
            store.save('Peter', 'some')

        service = AccountService(store, password_service)

        service.create_group('team', ['John', 'Peter'])

        assert_that(store, verify())
Example #7
0
    def test_account_behaviour_with_mock(self):
        with Stub(PasswordService) as password_service:
            password_service.generate().returns('some')

        with Mock(AccountStore) as store:
            store.has_user('John')
            store.save('John', 'some')
            store.has_user('Peter')
            store.save('Peter', 'some')

        service = AccountService(store, password_service)

        service.create_group('team', ['John', 'Peter'])

        assert_that(store, verify())
 def test_inline_mock(self):
     playername = "Kawhi Leonard"
     year = 2017
     #使用Mock()创建mock
     inline_mock = Mock()
     #使用expect_all()去设置期望值
     expect_call(inline_mock).get_score().returns("33")
     expect_call(inline_mock).get_assist().returns("6")
     expect_call(inline_mock).get_rebound().returns("7")
     expect_call(inline_mock).get_match_number(year).returns("no injury")
     #在SUT playerservice中调用这个mock
     player_service_mock_2017_order = pls.playerService(playername, year, inline_mock, pos.profileService(playername), bs.bodyService(), ss.salaryService())
     player_service_mock_2017_order.get_player_info()
     # verify()验证定义的mock期望是否正确被实现,且方法调用顺序必须完全一致
     assert_that(inline_mock, verify())
 def assert_expectation_error(self, expected_message):
     self.assert_with_message(self.mock, doublex.verify(),
                              expected_message)
Example #10
0
 def assert_expectation_error(self, expected_message):
     self.assert_with_message(self.mock, doublex.verify(),
                              expected_message)
Example #11
0
 def assert_that_is_satisfied(self):
     hamcrest.assert_that(self, doublex.verify())