Python Mock getting address instead return_value


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.

<pre class="lang-py prettyprint-override">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

<pre class="lang-py prettyprint-override">[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.

<pre class="lang-py prettyprint-override"> ====================================================================== 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?


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

eval = appModel.return_value.eval.return_value


>>> 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() <em>directly</em> instead.


  • AttributeError 'module' object has no attribute 'runserver' in django
  • Using shared memory with matrices
  • Mock standard input - multi line in python 3
  • GetActiveObject() vs. GetObject() — MK_E_UNAVAILABLE Error
  • Django testing - Multiple queries when externally modifying the data. Cache Issue?
  • Problem with altering model attributes in controller
  • Android app working perfect in marshmallow and lollipop or nougat but it does not work in kitkat
  • Tail recursion in Erlang
  • IronPython/C# Float data comparison
  • CakePHP Auth Component Not logging in when using $this->Auth->login();
  • Jasmine 2.0 async beforeEach not waiting for async to finish
  • hibernate exception: Batch update returned unexpected row count from update [0]; actual row count: 0
  • Delphi: StringGrid, position and Context Menu
  • Delphi TCollection - Possible to prevent changing items in run-time?
  • Why does Internet Explorer sometimes trigger a local storage event before the data's ready? [du
  • Converting RVML to SVG using XSLT in C#
  • Touch events over two dom elements?
  • Maven repository location is not updated in eclipse
  • Efficiently getting XML into Elasticsearch
  • Team Foundation Server 2015 vs. Bitbucket Server (Stash)
  • How to get the url of a file of google drive to download in android
  • Is there an easy way to associate an event with a ListViewItem?
  • Typecasting `this` of a base class template to its derived class
  • init_seg and warning C4073 from library code?
  • iOS Localization Doesn't Work with More Than 63 Files
  • Bound DataGridViewCheckBoxColumn not updating binding source
  • why calling cd shell command through system() or execvp() from a child process won't work?
  • How do I mock an exported typescript function in a jasmine test?
  • SyntaxError: (irb):26: both block arg and actual block given
  • Salesforce Different WSDL files and when to use
  • presentShareDialogWithParams posts to FB wall, but callback handler results say error
  • Time complexity of a program which involves multiple variables
  • Checking free space on FTP server
  • QLineEdit password safety
  • Why winpcap requires both .lib and .dll to run?
  • Is there any way to bind data to data.frame by some index?
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • How to push additional view controllers onto NavigationController but keep the TabBar?