def should_mimic_specification(self): ''' result_of should be callable and return specified value or raise specified exception ''' mock_call = MockSpec().foo() mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) spec.__call__().should_be(None) mock_call = MockSpec().foo().will_return(1) mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) spec.__call__().should_be(1) mock_call = MockSpec().foo().will_return((2, 3)) mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) spec.__call__().should_be((2, 3)) mock_call = MockSpec().foo().will_raise(StopIteration) mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) spec.__call__().should_raise(StopIteration) value_error = ValueError("that's no ordinary rabbit") mock_spec = MockSpec() mock_call = mock_spec.foo().will_raise(value_error) mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) spec.__call__().should_raise(value_error) # check that after exception raised the collaboration is 'over' Spec(mock_spec).verify().should_not_raise(UnmetSpecification)
def verify_exceptions_with_comparator(self): ''' ExceptionValue should be used to verify Exception args ''' mock_call = MockSpec().your_mother(TypeError('hamster')) mock_call_result = mock_call.result_of('your_mother') spec = Spec(mock_call_result) spec.__call__(TypeError('hamster')) spec.should_not_raise(UnmetSpecification) mock_call = MockSpec().your_father(smelt_of=TypeError('elderberries')) mock_call_result = mock_call.result_of('your_father') spec = Spec(mock_call_result) spec.__call__(smelt_of=TypeError('elderberries')) spec.should_not_raise(UnmetSpecification)
def result_of_successive_times(): ''' result_of should "iterate" over will_return value(s) and provide a meaningful error message if the specification is unmet''' mock_call = MockSpec(name='x').foo().times(2).will_return(3, 4) spec = Spec(mock_call.result_of('foo')) spec.__call__().should_be(3) spec = Spec(mock_call.result_of('foo')) spec.__call__().should_be(4) spec = Spec(mock_call.result_of('foo')) msg = 'should be collaborating with x.foo() only 2 successive times' spec.__call__().should_raise(UnmetSpecification(msg)) mock_call = MockSpec(name='y').bar().times(3).will_return(5) spec = Spec(mock_call.result_of('bar')) spec.__call__().should_be(5) spec = Spec(mock_call.result_of('bar')) spec.__call__().should_be(5) spec = Spec(mock_call.result_of('bar')) spec.__call__().should_be(5) spec = Spec(mock_call.result_of('bar')) msg = 'should be collaborating with y.bar() only 3 successive times' spec.__call__().should_raise(UnmetSpecification(msg))
def should_verify_args_specification(self): ''' result_of should verify the args specification and supply a meaningful message if specification is unmet ''' mock_call = MockSpec(name='a').foo() mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) msg = 'should be collaborating with a.foo(), not a.foo(1)' spec.__call__(1).should_raise(UnmetSpecification(msg)) mock_call = MockSpec(name='b').foo(1) mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) msg = "should be collaborating with b.foo(1), not b.foo('1')" spec.__call__('1').should_raise(UnmetSpecification(msg)) mock_call = MockSpec(name='c').foo(1) mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) msg = 'should be collaborating with c.foo(1), not c.foo()' spec.__call__().should_raise(UnmetSpecification(msg)) mock_call = MockSpec(name='d').foo(1) mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) msg = 'should be collaborating with d.foo(1), not d.foo(2)' spec.__call__(2).should_raise(UnmetSpecification(msg)) mock_call = MockSpec(name='e').foo(1) mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) spec.__call__(1).should_not_raise(UnmetSpecification) mock_call = MockSpec(name='f').foo(1).will_return(2) mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) spec.__call__(1).should_be(2) mock_call = MockSpec(name='g').bar(keyword='named argument') mock_call_result = mock_call.result_of('bar') spec = Spec(mock_call_result) msg = "should be collaborating with g.bar(keyword='named argument'), "\ + "not g.bar(keyword='wrong argument')" spec.__call__(keyword='wrong argument').should_raise( UnmetSpecification(msg)) mock_call = MockSpec(name='h').bar(keyword='named argument') mock_call_result = mock_call.result_of('bar') spec = Spec(mock_call_result) msg = "should be collaborating with h.bar(keyword='named argument'), "\ + "not h.bar(bad_keyword='named argument')" spec.__call__(bad_keyword='named argument').should_raise( UnmetSpecification(msg)) mock_call = MockSpec(name='i').bar(keyword='named argument') mock_call_result = mock_call.result_of('bar') spec = Spec(mock_call_result) spec.__call__(keyword='named argument').should_not_raise( UnmetSpecification) mock_call = MockSpec(name='j').bar( keyword='named argument').will_return('monty') mock_call_result = mock_call.result_of('bar') spec = Spec(mock_call_result) spec.__call__(keyword='named argument').should_be('monty')
def verify_floats_with_comparator(self): ''' FloatValue should be used to verify float args ''' mock_call = MockSpec().kernigget(3.14) mock_call_result = mock_call.result_of('kernigget') spec = Spec(mock_call_result) spec.__call__(3.141).should_not_raise(UnmetSpecification)
def should_verify_args_specification(self): ''' result_of should verify the args specification and supply a meaningful message if specification is unmet ''' mock_call = MockSpec(name='a').foo() mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) msg = 'should be collaborating with a.foo(), not a.foo(1)' spec.__call__(1).should_raise(UnmetSpecification(msg)) mock_call = MockSpec(name='b').foo(1) mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) msg = "should be collaborating with b.foo(1), not b.foo('1')" spec.__call__('1').should_raise(UnmetSpecification(msg)) mock_call = MockSpec(name='c').foo(1) mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) msg = 'should be collaborating with c.foo(1), not c.foo()' spec.__call__().should_raise(UnmetSpecification(msg)) mock_call = MockSpec(name='d').foo(1) mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) msg = 'should be collaborating with d.foo(1), not d.foo(2)' spec.__call__(2).should_raise(UnmetSpecification(msg)) mock_call = MockSpec(name='e').foo(1) mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) spec.__call__(1).should_not_raise(UnmetSpecification) mock_call = MockSpec(name='f').foo(1).will_return(2) mock_call_result = mock_call.result_of('foo') spec = Spec(mock_call_result) spec.__call__(1).should_be(2) mock_call = MockSpec(name='g').bar(keyword='named argument') mock_call_result = mock_call.result_of('bar') spec = Spec(mock_call_result) msg = "should be collaborating with g.bar(keyword='named argument'), "\ + "not g.bar(keyword='wrong argument')" spec.__call__(keyword='wrong argument').should_raise( UnmetSpecification(msg)) mock_call = MockSpec(name='h').bar(keyword='named argument') mock_call_result = mock_call.result_of('bar') spec = Spec(mock_call_result) msg = "should be collaborating with h.bar(keyword='named argument'), "\ + "not h.bar(bad_keyword='named argument')" spec.__call__(bad_keyword='named argument').should_raise( UnmetSpecification(msg)) mock_call = MockSpec(name='i').bar(keyword='named argument') mock_call_result = mock_call.result_of('bar') spec = Spec(mock_call_result) spec.__call__( keyword='named argument').should_not_raise(UnmetSpecification) mock_call = MockSpec(name='j').bar( keyword='named argument').will_return('monty') mock_call_result = mock_call.result_of('bar') spec = Spec(mock_call_result) spec.__call__(keyword='named argument').should_be('monty')