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

Photo by Chris Ried on Unsplash
user = UserController()
print("---")
print(user.get_first_picture())
print("---")
print(user.get_second_picture())
__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
Pic1
---
get_second_picture() method called
get_pictures_via_method() method called
get_DB() method called
all() method called
Pic2
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'>
mock.get_DB().all().__getitem__()

--

--

--

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
adil

adil

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