0

I am trying to sub out a call to check a users full name. I have written the below method using mock to do this.

def test_UserDisplayName(self):
    appModel = Mock()
    eval = appModel.eval.return_value
    eval.userDisplayName.return_value = 'JohnDoe'
    self._SummaryModel.AppModel = appModel
    actual = self._SummaryModel.UserDisplayName()
    self.assertEqual(actual, 'JohnDoe')

This is the method it is calling.

def UserDisplayName(self):
    return self.AppModel().eval().userDisplayName()

If I attached debugger to the above line and the run it in the shell it works

[PAUSED] >>> self.AppModel().eval().userDisplayName()
'JohnDoe'
[PAUSED] >>> 

But when the run the test case for it fails as it I am getting the address back rather than the value.

   ======================================================================
   FAIL: test_UserDisplayName (unittests.model.SummaryModelTest)
   ----------------------------------------------------------------------
   Traceback (most recent call last):
     File "/unittests/summary_model.py", line 112, in test_UserDisplayName
       self.assertEqual(actual, 'JohnDoe')
   AssertionError: <Mock name='mock.userDisplayName()' id='233406864'> != 'JohnDoe'

   ----------------------------------------------------------------------
   Ran 1 test in 0.010s

   FAILED:  (failures=1)

What am I doing wrong?

1 Answer 1

0

Your code calls AppModel as well, so you need to adjust the other references for that:

eval = appModel.return_value.eval.return_value

Demo:

>>> from unittest.mock import Mock
>>> appModel = Mock()
>>> eval = appModel.return_value.eval.return_value
>>> eval.userDisplayName.return_value = 'JohnDoe'
>>> appModel().eval().userDisplayName()
'JohnDoe'

However, your <Mock name='mock.userDisplayName()' id='233406864'> object shows a name of mock.userDisplayName() which suggests that the return value was produced by using self.AppModel.userDisplayName() directly instead.

Sign up to request clarification or add additional context in comments.

4 Comments

Added the above line but still it doesn't work and when I attach the debug point it doesn't work either. code [PAUSED] >>> self.AppModel().eval().userDisplayName() <Mock name='mock.eval().userDisplayName()' id='232555600'> [PAUSED] >>>
@Eamonn: Test each individual step of that expression and see if the same mock is returned as what your test sets up. So does self.AppModel() return appModel.return_value?
I am putting dir() on each step so I started on self.AppModel() I can see 'eval' and then when I do a dir() on self.AppModel().eval() I can see userDisplayName.
@Eamonn: looking at your AssertionError I don't see the expected mock().eval().userDisplayName() name for the mock. Are you certain about how UserDisplayName is implemented?

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.