def test_skip_builtin_verification_does_not_affect_non_builtins(self): with no_builtin_verification(): subject = InstanceDouble('doubles.testing.User') allow(subject).instance_method with raises(VerifyingDoubleArgumentError): subject.instance_method('bar')
class TestAsync(object): def setup(self): self.subject = InstanceDouble('doubles.testing.User') def test_and_return_future(self): allow(self.subject).instance_method.and_return_future('Bob Barker') result = self.subject.instance_method() assert result.result() == 'Bob Barker' def test_and_return_future_multiple_values(self): allow(self.subject).instance_method.and_return_future('Bob Barker', 'Drew Carey') result1 = self.subject.instance_method() result2 = self.subject.instance_method() assert result1.result() == 'Bob Barker' assert result2.result() == 'Drew Carey' def test_and_raise_future(self): exception = Exception('Bob Barker') allow(self.subject).instance_method.and_raise_future(exception) result = self.subject.instance_method() with raises(Exception) as e: result.result() assert e.value == exception
def test_passes_when_called_twice(self): subject = InstanceDouble('doubles.testing.User') expect(subject).instance_method.twice() subject.instance_method() subject.instance_method()
def test_passes_when_called_exactly_at_least_times(self): subject = InstanceDouble('doubles.testing.User') allow(subject).instance_method.at_least(1).times subject.instance_method() subject.instance_method()
def test_passes_when_called_more_than_at_least_times(self): subject = InstanceDouble('doubles.testing.User') expect(subject).instance_method.at_least(1).times subject.instance_method() subject.instance_method()
def test_calls_are_chainable(self): subject = InstanceDouble('doubles.testing.User') allow(subject).instance_method.exactly(1).time.exactly(2).times subject.instance_method() subject.instance_method()
def test_calls_are_chainable(self): subject = InstanceDouble('doubles.testing.User') expect(subject).instance_method.at_most(1).times.at_most(2).times subject.instance_method() subject.instance_method()
def test_matches_most_specific_allowance(self): subject = InstanceDouble('doubles.testing.User') allow(subject).method_with_varargs.and_return('bar') allow(subject).method_with_varargs.with_args('baz').and_return('blah') assert subject.method_with_varargs('baz') == 'blah'
def test_with_args_validator(self): subject = InstanceDouble('doubles.testing.User') expect(subject).method_with_varargs.with_args_validator( lambda *args: args[0] == 'Bob Barker' ) subject.method_with_varargs('Bob Barker', 'Drew Carey')
def test_takes_precendence_over_previous_allowance(self): subject = InstanceDouble('doubles.testing.User') allow(subject).instance_method.and_return('foo') expect(subject).instance_method assert subject.instance_method() is None
def test_returns_result_of_a_callable_with_varkwargs(self, stubber): subject = InstanceDouble('doubles.testing.User') stubber(subject).method_with_varkwargs.and_return_result_of( lambda **kwargs: kwargs['bob']) assert subject.method_with_varkwargs(bob='barker') == 'barker'
def test_returns_result_of_a_callable_with_positional_vargs(self, stubber): subject = InstanceDouble('doubles.testing.User') stubber(subject).method_with_varargs.and_return_result_of(lambda *x: x) result = subject.method_with_varargs('bob', 'barker') assert result == ('bob', 'barker')
def test_raises_provided_exception(self, stubber): subject = InstanceDouble('doubles.testing.User') stubber(subject).instance_method.and_raise(UserDefinedException) with raises(UserDefinedException): subject.instance_method()
def test_returns_result_of_a_callable_with_positional_arg(self, stubber): subject = InstanceDouble('doubles.testing.User') stubber(subject).method_with_positional_arguments.and_return_result_of( lambda x: x) assert subject.method_with_positional_arguments('bar') == 'bar'
def test_returns_specified_values_in_order(self, stubber): subject = InstanceDouble('doubles.testing.User') stubber(subject).instance_method.and_return('bar', 'bazz') assert subject.instance_method() == 'bar' assert subject.instance_method() == 'bazz'
def test_subsequent_allowances_override_previous_ones(self, stubber): subject = InstanceDouble('doubles.testing.User') stubber(subject).instance_method.never().and_return('bar') stubber(subject).instance_method.and_return('baz') assert subject.instance_method() == 'baz'
class TestAsync(object): def setup(self): self.subject = InstanceDouble('doubles.testing.User') def test_and_return_future(self): allow(self.subject).instance_method.and_return_future('Bob Barker') result = self.subject.instance_method() assert result.result() == 'Bob Barker' def test_and_return_future_multiple_values(self): allow(self.subject).instance_method.and_return_future( 'Bob Barker', 'Drew Carey') result1 = self.subject.instance_method() result2 = self.subject.instance_method() assert result1.result() == 'Bob Barker' assert result2.result() == 'Drew Carey' def test_and_raise_future(self): exception = Exception('Bob Barker') allow(self.subject).instance_method.and_raise_future(exception) result = self.subject.instance_method() with raises(Exception) as e: result.result() assert e.value == exception
def test_allows_any_arguments_if_none_are_specified(self): subject = InstanceDouble('doubles.testing.User') allow(subject).method_with_positional_arguments.and_return('bar') assert subject.method_with_positional_arguments( 'unspecified argument') == 'bar'
def test_returns_the_last_specified_value_multiple_times(self, stubber): subject = InstanceDouble('doubles.testing.User') stubber(subject).instance_method.and_return('bar', 'bazz') assert subject.instance_method() == 'bar' assert subject.instance_method() == 'bazz' assert subject.instance_method() == 'bazz'
def test_raises_provided_exception_with_complex_signature(self, stubber): subject = InstanceDouble('doubles.testing.User') stubber(subject).instance_method.and_raise( UserDefinedExceptionWithArgs, 'msg', 'arg1', arg2='arg2') with raises(UserDefinedExceptionWithArgs): subject.instance_method()
def test_chaining_result_methods_gives_the_last_one_precedence(self, stubber): subject = InstanceDouble('doubles.testing.User') stubber(subject).instance_method.and_return('bar').and_return_result_of( lambda: 'baz' ).and_raise(UserDefinedException).and_return('final') assert subject.instance_method() == 'final'
def test_chaining_result_methods_gives_the_last_one_precedence( self, stubber): subject = InstanceDouble('doubles.testing.User') stubber(subject).instance_method.and_return( 'bar').and_return_result_of(lambda: 'baz').and_raise( UserDefinedException).and_return('final') assert subject.instance_method() == 'final'
def test_raises_provided_exception_with_complex_signature(self, stubber): subject = InstanceDouble('doubles.testing.User') stubber(subject).instance_method.and_raise( UserDefinedExceptionWithArgs('msg', 'arg1', arg2='arg2'), ) with raises(UserDefinedExceptionWithArgs): subject.instance_method()
def test_called_with_zero(self): subject = InstanceDouble('doubles.testing.User') allow(subject).instance_method.exactly(0).times with raises(MockExpectationError) as e: subject.instance_method() teardown() assert re.match( r"Allowed 'instance_method' to be called 0 times instead of 1 " r"time on <InstanceDouble of <class 'doubles.testing.User'> " r"object at .+> with any args, but was not." r" \(.*doubles/test/allow_test.py:\d+\)", str(e.value))
def test_raises_if_called_with_args(self): subject = InstanceDouble('doubles.testing.User') allow(subject).instance_method.with_no_args() with raises(UnallowedMethodCallError) as e: subject.instance_method('bar') assert re.match( r"Received unexpected call to 'instance_method' on " r"<InstanceDouble of <class '?doubles.testing.User'?" r"(?: at 0x[0-9a-f]{9})?> object at .+>\." r" The supplied arguments \('bar'\)" r" do not match any available allowances.", str(e.value))
def test_raises_if_arguments_were_specified_but_wrong_kwarg_used_when_called( self): subject = InstanceDouble('doubles.testing.User') allow(subject).method_with_default_args.with_args('one', bar='two') with raises(UnallowedMethodCallError) as e: subject.method_with_default_args('one', bob='barker') assert re.match( r"Received unexpected call to 'method_with_default_args' on " r"<InstanceDouble of <class '?doubles.testing.User'?" r"(?: at 0x[0-9a-f]{9})?> object at .+>\." r" The supplied arguments \('one', bob='barker'\)" r" do not match any available allowances.", str(e.value))
def test_fails_when_called_less_than_at_least_times(self): subject = InstanceDouble('doubles.testing.User') expect(subject).instance_method.at_least(2).times subject.instance_method() with raises(MockExpectationError) as e: verify() teardown() assert re.match( r"Expected 'instance_method' to be called at least 2 times instead of 1 time on " r"<InstanceDouble of <class 'doubles.testing.User'> object at .+> " r"with any args, but was not." r" \(.*doubles/test/expect_test.py:\d+\)", str(e.value))
def test_called_with_zero(self): subject = InstanceDouble('doubles.testing.User') allow(subject).instance_method.exactly(0).times with raises(MockExpectationError) as e: subject.instance_method() teardown() assert re.match( r"Allowed 'instance_method' to be called 0 times but was called 1 " r"time on <InstanceDouble of <class 'doubles.testing.User'> " r"object at .+> with any args, but was not." r" \(.*doubles/test/allow_test.py:\d+\)", str(e.value) )
def test_raises_if_arguments_were_specified_but_not_provided_when_called(self): subject = InstanceDouble('doubles.testing.User') allow(subject).method_with_default_args.with_args('one', bar='two') with raises(UnallowedMethodCallError) as e: subject.method_with_default_args() assert re.match( r"Received unexpected call to 'method_with_default_args' on " r"<InstanceDouble of <class '?doubles.testing.User'?" r"(?: at 0x[0-9a-f]{9})?> object at .+>\." r" The supplied arguments \(\)" r" do not match any available allowances.", str(e.value) )
def test_raises_if_called_with_args(self): subject = InstanceDouble('doubles.testing.User') allow(subject).instance_method.with_no_args() with raises(UnallowedMethodCallError) as e: subject.instance_method('bar') assert re.match( r"Received unexpected call to 'instance_method' on " r"<InstanceDouble of <class '?doubles.testing.User'?" r"(?: at 0x[0-9a-f]{9})?> object at .+>\." r" The supplied arguments \('bar'\)" r" do not match any available allowances.", str(e.value) )
def test_fails_when_called_once_times(self): subject = InstanceDouble('doubles.testing.User') allow(subject).instance_method.never() with raises(MockExpectationError) as e: subject.instance_method() teardown() assert re.match( r"Allowed 'instance_method' to be called 0 times instead of 1 " r"time on <InstanceDouble of <class 'doubles.testing.User'> " r"object at .+> with any args, but was not." r" \(.*doubles/test/allow_test.py:\d+\)", str(e.value) )
def test_proxies_docstring(self): subject = InstanceDouble('doubles.testing.User') allow(subject).method_with_doc assert subject.method_with_doc.__doc__ == ( """A basic method of OldStyleUser to illustrate existence of a docstring""" )
def test_fails_when_called_once(self): subject = InstanceDouble('doubles.testing.User') expect(subject).instance_method.twice() subject.instance_method() with raises(MockExpectationError) as e: verify() teardown() assert re.match( r"Expected 'instance_method' to be called 2 times instead of 1 time on " r"<InstanceDouble of <class 'doubles.testing.User'> object at .+> " r"with any args, but was not." r" \(.*doubles/test/expect_test.py:\d+\)", str(e.value) )
def test_fails_when_called_more_than_at_most_times(self): subject = InstanceDouble('doubles.testing.User') expect(subject).instance_method.at_most(1).times subject.instance_method() with raises(MockExpectationError) as e: subject.instance_method() teardown() assert re.match( r"Expected 'instance_method' to be called at most 1 time instead of 2 times on " r"<InstanceDouble of <class 'doubles.testing.User'> object at .+> " r"with any args, but was not." r" \(.*doubles/test/expect_test.py:\d+\)", str(e.value) )
def test_fails_when_called_more_than_expected_times(self): subject = InstanceDouble('doubles.testing.User') allow(subject).instance_method.exactly(1).times subject.instance_method() with raises(MockExpectationError) as e: subject.instance_method() teardown() assert re.match( r"Allowed 'instance_method' to be called 1 time but was called 2 times on " r"<InstanceDouble of <class 'doubles.testing.User'> object at .+> " r"with any args, but was not." r" \(.*doubles/test/allow_test.py:\d+\)", str(e.value) )
def test_raises_if_no_arguments_supplied(self, stubber): subject = InstanceDouble('doubles.testing.User') with raises(TypeError) as e: stubber(subject).instance_method.and_return() assert str(e.value) == 'and_return() expected at least 1 return value' teardown()
def test_satisfied_expectation(self): subject = InstanceDouble('doubles.testing.User') allow(subject).__enter__ expect(subject).__exit__.once() with subject: pass
def test_raises_if_called_with_negative_value(self): subject = InstanceDouble('doubles.testing.User') with raises(TypeError) as e: allow(subject).instance_method.at_most(-1).times teardown() assert re.match(r"at_most requires one positive integer argument", str(e.value))
def test_takes_precedence_over_subsequent_allowances(self): subject = InstanceDouble('doubles.testing.User') expect(subject).instance_method allow(subject).instance_method.and_return('foo') with raises(MockExpectationError): verify() teardown()
def test_unsatisfied_expectation(self): subject = InstanceDouble('doubles.testing.User') expect(subject).__exit__.once() with raises(MockExpectationError) as e: verify() teardown() assert re.match( r"Expected '__exit__' to be called 1 time instead of 0 times on " r"<InstanceDouble of <class 'doubles.testing.User'> object at .+> " r"with any args, but was not." r" \(.*doubles/test/expect_test.py:\d+\)", str(e.value))
def test_returns_result_of_a_callable_with_varkwargs(self, stubber): subject = InstanceDouble('doubles.testing.User') stubber(subject).method_with_varkwargs.and_return_result_of(lambda **kwargs: kwargs['bob']) assert subject.method_with_varkwargs(bob='barker') == 'barker'
def test_passes_when_called_exactly_expected_times(self): subject = InstanceDouble('doubles.testing.User') expect(subject).instance_method.exactly(1).times subject.instance_method()
def test_returns_result_of_a_callable_with_positional_arg(self, stubber): subject = InstanceDouble('doubles.testing.User') stubber(subject).method_with_positional_arguments.and_return_result_of(lambda x: x) assert subject.method_with_positional_arguments('bar') == 'bar'
def test_returns_result_of_a_callable(self, stubber): subject = InstanceDouble('doubles.testing.User') stubber(subject).instance_method.and_return_result_of(lambda: 'bar') assert subject.instance_method() == 'bar'
def test_passes_if_an_expected_method_call_is_made(self): subject = InstanceDouble('doubles.testing.User') expect(subject).instance_method subject.instance_method()
def test_passes_if_method_is_called_with_specified_arguments(self): subject = InstanceDouble('doubles.testing.User') expect(subject).method_with_default_args.with_args('one', bar='two') assert subject.method_with_default_args('one', bar='two') is None