def should_verify_specified_collaborations(self): ''' after start_collaborating, collaborations should be verified ''' spec = Spec(MockSpec(name='a')) spec.when(spec.start_collaborating()) spec.then(spec.foo()) msg = 'should not be collaborating with a.foo()' spec.should_raise(UnmetSpecification(msg)) spec = Spec(MockSpec(name='b')) spec.when(spec.foo(), spec.start_collaborating()) spec.then(spec.bar()) msg = 'should be collaborating with b.foo(), not b.bar()' spec.should_raise(UnmetSpecification(msg)) spec = Spec(MockSpec(name='c')) spec.when(spec.foo(), spec.bar(), spec.start_collaborating()) spec.then(spec.foo()).should_not_raise(UnmetSpecification) msg = 'should be collaborating with c.bar(), not c.baz()' spec.then(spec.baz()).should_raise(UnmetSpecification(msg)) mock = MockSpec(name='d') mock.foo().times(2).will_return('camelot') spec = Spec(mock) spec.when(spec.start_collaborating()) spec.then(spec.foo()).should_not_raise(UnmetSpecification) spec.then(spec.foo()).should_not_raise(UnmetSpecification) msg = 'should not be collaborating with d.foo()' spec.then(spec.foo()).should_raise(UnmetSpecification(msg))
def should_trap_incorrect_return(self): ''' Specified and_result="bar" but was "baz": UnmetSpecification. Note: and_result refers to the value returned from the callable invoked in verify(), not the return value from the mock. See the Hungarian gentleman in the examples for a clearer picture... ''' mock_spec = MockSpec() spec = Spec(CollaborateWith(mock_spec.foo().will_return('baz'), and_result='bar')) spec.verify(lambda: mock_spec.foo()).should_raise(UnmetSpecification)
def should_trap_incorrect_args(self): ''' Specified foo(2) & bar(), and foo(1) called: UnmetSpecification''' mock_spec = MockSpec() collaborations = (mock_spec.foo(2), mock_spec.bar()) descriptions = [collaboration.description() for collaboration in collaborations] spec = Spec(CollaborateWith(*collaborations)) spec.describe_constraint().should_be(','.join(descriptions)) spec.verify(lambda: mock_spec.foo(1)).should_raise(UnmetSpecification)
def should_trap_incorrect_return(self): ''' Specified and_result="bar" but was "baz": UnmetSpecification. Note: and_result refers to the value returned from the callable invoked in verify(), not the return value from the mock. See the Hungarian gentleman in the examples for a clearer picture... ''' mock_spec = MockSpec() spec = Spec( CollaborateWith(mock_spec.foo().will_return('baz'), and_result='bar')) spec.verify(lambda: mock_spec.foo()).should_raise(UnmetSpecification)
def should_trap_incorrect_args(self): ''' Specified foo(2) & bar(), and foo(1) called: UnmetSpecification''' mock_spec = MockSpec() collaborations = (mock_spec.foo(2), mock_spec.bar()) descriptions = [ collaboration.description() for collaboration in collaborations ] spec = Spec(CollaborateWith(*collaborations)) spec.describe_constraint().should_be(','.join(descriptions)) spec.verify(lambda: mock_spec.foo(1)).should_raise(UnmetSpecification)
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 should_notify_mock_spec(self): ''' start_collaborating should pass message on to mock_spec ''' mock_spec = MockSpec() mock_call = mock_spec.foo().once() Spec(mock_call).start_collaborating().should_be(mock_spec) Spec(mock_spec).bar().should_raise(UnmetSpecification) Spec(mock_spec).foo().should_not_raise(UnmetSpecification)
def correct_call_should_be_ok(self): ''' Specified foo() and foo() called: met specification ''' mock_spec = MockSpec() spec = Spec(CollaborateWith(mock_spec.foo())) spec.verify(lambda: mock_spec.foo()) spec.should_not_raise(UnmetSpecification)
def should_trap_incorrect_call(self): ''' Specified foo() but bar() called: UnmetSpecification ''' mock_spec = MockSpec() spec = Spec(CollaborateWith(mock_spec.foo())) spec.describe_constraint().should_be(mock_spec.foo().description()) spec.verify(lambda: mock_spec.bar()).should_raise(UnmetSpecification)