def test_allows_stubs_on_existing_class_methods(self): with no_builtin_verification(): date = InstanceDouble('datetime.date') allow(date).today.with_args() assert date.today() is None
def test_allows_stubs_on_existing_methods(self): with no_builtin_verification(): date = InstanceDouble('datetime.date') allow(date).ctime assert date.ctime() is None
def test_get_vehicles(): owner = 'owner' owners_vehicles = 'owners_vehicles' vehicles_repo = InstanceDouble( 'app.sample.repositories.vehicles_repository.VehiclesRepository') allow(vehicles_repo).get_vehicles.with_args(owner).and_return( owners_vehicles) assert vehicles_repo.get_vehicles(owner) == owners_vehicles
def test_get_user(): username = '******' password = '******' user = '******' users_repo = InstanceDouble( 'app.sample.repositories.users_repository.UsersRepository') allow(users_repo).get_user.with_args(username, password).and_return(user) assert users_repo.get_user(username, password) == user
def test_mocking__enter__and__exit__works(self): user = InstanceDouble('doubles.testing.User') allow(user).__enter__.and_return('bob barker') allow(user).__exit__ with user as u: assert u == 'bob barker'
def test_dictionary_read_error(): some_rw = InstanceDouble('tchannel.rw.ReadWriter') allow(some_rw).read.and_raise(ReadError('great sadness')) dict_rw = rw.dictionary(('foo', some_rw)) with pytest.raises(ReadError): dict_rw.read(BytesIO())
def test_instance_exception(): some_rw = InstanceDouble('tchannel.rw.ReadWriter') c_rw = rw.instance(ClassWithArgs, ('x', some_rw), ('y', rw.number(4))) allow(some_rw).read.and_raise(ReadError("great sadness")) with pytest.raises(ReadError): c_rw.read(bio([1, 2, 3, 4]))
def test_read(self): some_rw = InstanceDouble('tchannel.rw.ReadWriter') delegated_rw = self._mk_rw(some_rw) stream = BytesIO() expect(some_rw).read.with_args(stream) delegated_rw.read(stream)
def test_write(self): some_rw = InstanceDouble('tchannel.rw.ReadWriter') delegated_rw = self._mk_rw(some_rw) stream = BytesIO() expect(some_rw).write.with_args("foo", stream) delegated_rw.write("foo", stream)
def git(): repo = ClassDouble('accountable.accountable.Repo') git = InstanceDouble('accountable.accountable.Repo') git.git = support.MockRepo() allow_constructor(repo).and_return(git) accountable.Repo = repo
def vcr_client(): return InstanceDouble('tchannel.testing.vcr.config.VCRProxyClient')
def test_raises_when_stubbing_noncallable_attributes(self): date = InstanceDouble('datetime.date') with raises(VerifyingDoubleError): allow(date).year
def cassette(): cass = InstanceDouble('tchannel.testing.vcr.cassette.Cassette') cass.write_protected = False return cass
def test_width(self): some_rw = InstanceDouble('tchannel.rw.ReadWriter') delegated_rw = self._mk_rw(some_rw) expect(some_rw).width.with_no_args() delegated_rw.width()
def test_raises_when_stubbing_nonexistent_methods(self): date = InstanceDouble('datetime.date') with raises(VerifyingDoubleError): allow(date).nonexistent_method
def test_mocking__call__works(self): user = InstanceDouble('doubles.testing.User') allow(user).__call__.and_return('bob barker') assert user() == 'bob barker'
def test_raises_when_class_does_not_exist_in_module(self): with raises(VerifyingDoubleImportError) as e: InstanceDouble('unittest.foo') assert str(e.value) == 'No object at path: unittest.foo.'
def test_raises_when_importing_a_non_class_object(self): with raises(VerifyingDoubleImportError) as e: InstanceDouble('unittest.signals') assert str( e.value) == 'Path does not point to a class: unittest.signals.'
def test_raises_on_import_error(self): with raises(VerifyingDoubleImportError) as e: InstanceDouble('foo.bar') assert str(e.value) == 'Cannot import object from path: foo.bar.'
def test_raises_when_target_is_a_top_level_module(self): with raises(VerifyingDoubleImportError) as e: InstanceDouble('foo') assert str(e.value) == 'Invalid import path: foo.'
def test_passing_kwargs_assings_them_as_attrs(self): user = InstanceDouble('doubles.testing.User', name='Bob Barker') assert user.name == 'Bob Barker'
def test_class_with__getattr__(self): test_obj = InstanceDouble('doubles.testing.ClassWithGetAttr') allow(test_obj).method.and_return('bob barker') assert test_obj.method() == 'bob barker'
def test_creating_instance_double_after_patching(self): patch('doubles.testing.User', InstanceDouble('doubles.testing.User')) assert InstanceDouble('doubles.testing.User')
def test_raises_when_argspec_does_not_match(self): date = InstanceDouble('datetime.date') with raises(VerifyingDoubleArgumentError): allow(date).ctime.with_args('foo')