示例#1
0
    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))
示例#2
0
 def verify(self, callable_result):
     ''' Invoke callable_result() and it raises an exception that
     meets the constraint '''
     try:
         self._invoke(callable_result)
     except self._specified_type as raised_exception:
         if self.verify_value(raised_exception):
             return
         msg = '%s, not %r' % (self.describe_constraint(), raised_exception)
         raise UnmetSpecification(msg)
     raise UnmetSpecification(self.describe_constraint())
示例#3
0
def not_behaviour():
    ''' Not should raise exception iff underlying check succeeds '''
    spec = Spec(Not(Constraint(EqualsEquals(2))))
    spec.verify(number_one).should_not_raise(UnmetSpecification)

    spec = Spec(Not(Constraint(EqualsEquals(1))))
    msg = 'should not be == 1'
    spec.describe_constraint().should_be(msg)
    spec.verify(number_one).should_raise(UnmetSpecification(msg))

    spec = Spec(Not(Not(Constraint(EqualsEquals(2)))))
    msg = 'should be == 2'
    spec.describe_constraint().should_be(msg)
    spec.verify(number_one).should_raise(UnmetSpecification(msg))
示例#4
0
    def should_have_meaningful_msg(self):
        ''' Raise should produce meaningful UnmetSpecification messages'''
        spec = Spec(Raise(IndexError))
        msg = "should raise IndexError"
        spec.describe_constraint().should_be(msg)
        spec.verify(dont_raise_index_error)
        spec.should_raise(UnmetSpecification(msg))

        spec = Spec(Raise(IndexError('with some message')))
        msg = "should raise IndexError('with some message',)"
        spec.describe_constraint().should_be(msg)
        unmet_msg = msg + ", not IndexError('with message',)"
        unmet_specification = UnmetSpecification(unmet_msg)
        spec.verify(raise_index_error).should_raise(unmet_specification)
示例#5
0
 def verify(self, callable_result):
     ''' Check that the constraint is met '''
     try:
         self._constraint.verify(callable_result)
     except UnmetSpecification:
         return
     raise UnmetSpecification(self.describe_constraint())
示例#6
0
 def verify(self, callable_result):
     ''' Invoke callable_result() and _verify() it meets the constraint '''
     value_to_verify = self._invoke(callable_result)
     if self.verify_value(value_to_verify):
         return
     msg = '%s, not %r' % (self.describe_constraint(), value_to_verify)
     raise UnmetSpecification(msg)
示例#7
0
 def _collaboration(self, name):
     ''' Return an instance of a collaboration (in "collaboration" mode) '''
     if len(self._collaborations) == 0:
         msg = 'should not be collaborating with %s.%s()' % \
             (self._name, name)
         raise UnmetSpecification(msg)
     return self._collaborations[0].result_of(name)
示例#8
0
    def should_verify_name(self):
        ''' result_of should verify the name specification '''
        mock_call = MockSpec(name='p').foo()
        spec = Spec(mock_call)
        msg = 'should be collaborating with p.foo(), not p.bar()'
        spec.result_of('bar').should_raise(UnmetSpecification(msg))

        mock_call = MockSpec().foo()
        spec = Spec(mock_call)
        spec.result_of('foo').should_not_raise(UnmetSpecification)
示例#9
0
    def should_check_unverified_collaborations(self):
        ''' check for unverified collaborations after start_collaborating '''
        spec = Spec(MockSpec())
        spec.when(spec.foo(), spec.start_collaborating())
        spec.then(spec.verify())
        msg = 'should be collaborating with unnamed_mock.foo()'
        spec.should_raise(UnmetSpecification(msg))

        spec = Spec(MockSpec())
        spec.when(spec.foo(), spec.start_collaborating(), spec.foo())
        spec.then(spec.verify())
        spec.should_not_raise(UnmetSpecification)
示例#10
0
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))
示例#11
0
def unmet_specification():
    ''' Simple fn that raises UnmetSpecification. '''
    raise UnmetSpecification()
示例#12
0
    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')
示例#13
0
 def verify(self):
     ''' Verify that all the specified collaborations have occurred '''
     if len(self._collaborations) > 0:
         raise UnmetSpecification(self._collaborations[0].description())