1

That's my first attempt to use mock in order to mock a db and cannot understand how it works.

My main file :

 def _test( db ) :
       res = {}
       for x in db.getData() :
          res[ x['id'] ] = x['name'] 
       return res

and this is my test :

def test_small() :
   mock_db = mock.Mock()
   mock_db.getData.return_value = [{ u'name':u'Nick',u'id':1234}]
   _test( mock_db )
   assert mock_db.getData.assert_called_once()

Assert fails though. Error is :

assert None
E        +  where None = <bound method Mock.assert_called_once of <Mock name='mock.getData' id='140103447969552'>>()
E        +    where <bound method Mock.assert_called_once of <Mock name='mock.getData' id='140103447969552'>> = <Mock name='mock.getData' id='140103447969552'>.assert_called_once
E        +      where <Mock name='mock.getData' id='140103447969552'> = <Mock id='140103462485712'>.getData

Can someone explain me what am I missing? ideally I want to put some asserts later on - that result of _test is the return value of my mock.

1
  • fixed sorry - edited. Commented Oct 31, 2017 at 17:45

1 Answer 1

1

assert_called_once method itself already done the assertion for you:

def assert_called_once(_mock_self):
    """assert that the mock was called only once.
    """
    self = _mock_self
    if not self.call_count == 1:
        msg = ("Expected '%s' to have been called once. Called %s times." %
               (self._mock_name or 'mock', self.call_count))
        raise AssertionError(msg)

you could remove the surplus assert clause:

mock_db.getData.assert_called_once()

or

assert mock_db.getData.call_count == 1
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.