0

I am using unittest.mock to test my Django application.

The set-up is the following:

  • I want to test a function foo
  • foo uses a method X which is a constructor from an external package
  • X is called in foo with some parameters argsA to return an object x
  • x.create_dataset is called in foo with some parameters argsB to return an object ds
  • ds.load is called in foo with some parameters argsC to store data in ds.data
  • ds.data is manipulated (merge, agg...) in foo to create the return value of foo

I want to test that the function foo works as expected, knowing the return values of the other functions calls given argsA, argsB, argsC.

At the moment, I can't even run my test as I have a TypeError when ds.data is being manipulated : TypeError: expected pandas DataFrame or Series, got 'MagicMock'

3
  • Please provide a minimal reproducible example. It is generally better to share code than to list down what the code is doing. With your description we have no idea about which object is mocked, nor do we know what exactly goes wrong. Commented Apr 9, 2024 at 9:20
  • Thanks for your message. Unfortunately, the external package I refer is specific to my company so that I can't easily share a reproducible example. I might look for a package built with the same logic to create a reproducible example sharing the same logic. However any guidance for the logic outlined would be greatly appreciated! Commented Apr 9, 2024 at 9:26
  • Please see: What should an asker do if they're not able to include source code in their question? we can't really help you fix a problem we can't even reproduce. Instead of searching for some package with similar logic you can simply create a small python module of your own as a replacement. In this case it should probably be simple for you to spin up some code that reproduces the problem you are facing. Commented Apr 9, 2024 at 9:33

0

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.