def test_should_notify_listeners(self): # given when(self.sensor1).measure().thenReturn([mock(), mock(), mock()]) when(self.sensor2).measure().thenReturn([mock(), mock(), mock()]) # when self.service.measure() # then verify(self.listener1).notify( arg_that(lambda dto: len(dto.values) == 6)) verify(self.listener2).notify( arg_that(lambda dto: len(dto.values) == 6))
def test_having_fun_with_mocking(self): when(job_factory).generate_sum_job(1, ANY).thenReturn( sum_job.SumJob(-1, 0)) when(job_factory).\ generate_sum_job(arg_that(lambda val: val % 2 == 0), arg_that(lambda val: val % 2 == 0)).\ thenReturn(sum_job.SumJob(-2, 0)) result = job_factory.generate_sum_job(1, 3).execute() self.assertEqual(-1, result) print(result) result = job_factory.generate_sum_job(4, 8).execute() self.assertEqual(-2, result) print(result)
def test_nested_resources_registering_cleanup_on_create(self, session): class UserResources(RestResources): class Resource(RestResources.Resource): articles = resources('/articles') users = UserResources(session, '/users') when(session).request('/users/user1/articles', method='POST', data=m.any())\ .thenReturn({'id': 'article1'}) users['user1'].articles.create({'name': 'John Doe'}) func_captor = m.captor(m.arg_that(callable)) verify(session).add_cleanup('/users/user1/articles/article1', func_captor) when(session).raw_request(m.any(), method='DELETE')\ .thenReturn(Response(204, {}, '')) (func_captor.value)() verify(session).raw_request('/users/user1/articles/article1', method='DELETE')
def test_should_save_result_to_repository(self): # given when(self.sensor1).measure().thenReturn([mock(), mock(), mock()]) when(self.sensor2).measure().thenReturn([mock(), mock(), mock()]) # when self.service.measure() # then verify(self.repo).save(arg_that(lambda dto: len(dto.values) == 6))
def test_registering_cleanup_on_create(self, session): when(session).request('/users', method='POST', data=m.any())\ .thenReturn({'id': 'john'}) RestResources(session, '/users').create({'name': 'John Doe'}) func_captor = m.captor(m.arg_that(callable)) verify(session).add_cleanup('/users/john', func_captor) when(session).raw_request(m.any(), method='DELETE')\ .thenReturn(Response(204, {}, '')) (func_captor.value)() verify(session).raw_request('/users/john', method='DELETE')