Unit Tests Return MagicMock Object Instead of return_value. How to Fix This?

Photo by Chris Ried on Unsplash
user = UserController()
__init__ method in the UserController class called
__init__ method in the Picture class called
get_first_picture() method called
get_pictures_via_variable() method called
all() method called
get_second_picture() method called
get_pictures_via_method() method called
get_DB() method called
all() method called
AssertionError: 'Test 1' != <MagicMock name='mock.dbal.all().__getitem__()' id='4456571856'>
return self.picture.dbal.all()
mock_app.dbal.all.return_value = ["Test 1", "Test 2"]

A unit test for the get_second_picture method

AssertionError: 'Test 2' != <MagicMock name='mock.get_DB().all().__getitem__()' id='4387991856'>
return self.picture.get_DB().all()
AssertionError: 'Test 2' != <MagicMock name='mock.get_DB().all().__getitem__()' id='4459012640'>




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Global Utility Drone Market is estimated to reach USD 1.2

10 Interesting Python Tkinter Programs with Code

How to build a simple authentication system from scratch in Rails-6

A brief on overhead of Pushe Android SDK on Application size

Computer Science for beginners: getting started

Static and Dynamic libraries: collection of pre-compiled pieces of code

SDLC Models Explained: Agile, Waterfall, V-Shaped, Iterative, Spiral

difference between StreamController and setState , Implementation individually

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


More from Medium

Singleton pattern in python libraries

How To Create a File Converter in Python

A Look at Instance, Static, Class and Abstract Methods in Python

How to Write Clean Code (in Python) With SOLID Principles | Principle #5