def test_allowing_with_args(self, test_class): TestClass = ClassDouble(test_class) allow_constructor(TestClass).with_args( *VALID_ARGS[test_class]).and_return('Bob Barker') assert TestClass(*VALID_ARGS[test_class]) == 'Bob Barker'
def test_called_with_wrong_args(self, test_class): TestClass = ClassDouble(test_class) allow_constructor(TestClass).with_args(*VALID_ARGS[test_class]).and_return('Bob Barker') with raises(UnallowedMethodCallError): TestClass('Bob', 101)
def test_using_the_patched_class(self): allow_constructor(patch_class('doubles.testing.User')).and_return('result_1') allow_constructor(patch_class('doubles.testing.OldStyleUser')).and_return('result_2') result_1, result_2 = doubles.testing.top_level_function_that_creates_an_instance() assert result_1 == 'result_1' assert result_2 == 'result_2'
def test_called_with_wrong_args(self, test_class): TestClass = ClassDouble(test_class) allow_constructor(TestClass).with_args( *VALID_ARGS[test_class]).and_return('Bob Barker') with raises(UnallowedMethodCallError): TestClass('Bob', 101)
def test_using_the_patched_class(self): allow_constructor( patch_class('doubles.testing.User')).and_return('result_1') allow_constructor( patch_class('doubles.testing.OldStyleUser')).and_return('result_2') result_1, result_2 = doubles.testing.top_level_function_that_creates_an_instance( ) assert result_1 == 'result_1' assert result_2 == 'result_2'
def test_allowing_with_args(self, test_class): TestClass = ClassDouble(test_class) allow_constructor(TestClass).with_args(*VALID_ARGS[test_class]).and_return('Bob Barker') assert TestClass(*VALID_ARGS[test_class]) == 'Bob Barker'
def test_fails_with_positional_args_and_kwargs(self, type_): double = ClassDouble('doubles.testing.{}SubClass'.format(type_)) with raises(VerifyingDoubleArgumentError): allow_constructor(double).with_args(1, 2, foo=1)
def test_fails_with_kwargs(self, type_): double = ClassDouble('doubles.testing.{}SubClass'.format(type_)) with raises(VerifyingDoubleArgumentError): allow_constructor(double).with_args(bob='Barker')
def test_passes_with_positional_args(self, type_): double = ClassDouble('doubles.testing.{}SubClass'.format(type_)) allow_constructor(double).with_args(1, 2)
def test_passes_with_kwargs(self, type_): double = ClassDouble('doubles.testing.{}SubClass'.format(type_)) allow_constructor(double).with_args(bob='Barker')
def test_raises_if_you_allow_constructor(self, test_class): with raises(ConstructorDoubleError): allow_constructor(test_class)
def gen_mock_service(): class_name = 'bootcamp.handlers.title.TitleService' mock_service = TitleService() service_class = patch_class(class_name) allow_constructor(service_class).and_return(mock_service) return mock_service
def test_with_no_args(self, test_class): TestClass = ClassDouble(test_class) allow_constructor(TestClass) assert TestClass(*VALID_ARGS[test_class]) is None
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 test_with_invalid_args(self, test_class): TestClass = ClassDouble(test_class) with raises(VerifyingDoubleArgumentError): allow_constructor(TestClass).with_args(10)
def gen_mock_service(): class_name = 'bootcamp.handlers.add_user.UserService' mock_service = UserService() service_class = patch_class(class_name) allow_constructor(service_class).and_return(mock_service) return mock_service