My class, Foo, has a calculate method that takes no parameters, makes a call to a database and does some calculations using class attributes, and sets another class attribute, calculation_results (which is a dict).
I'm trying to mock a separate function that includes a call to Foo.calculate().
I tried something like the below, where mock_calculate was a function defined in my test class:
def mock_calculate():
pass
@mock.patch("path.Foo.validate", new=mock_calculate)
@mock.patch("path.Foo")
def test_func_that_calls_foo(self, mock_foo):
mock_foo.return_value.calculation_results = {first_calc : 1, second_calc: 2, database_mismatches : []}
func_that_calls_foo(#TEST_INPUT)
#assertions
This code does set the calculation_result attribute, but it still calls the actual calculate function, which fails when it tries to hit the database. How can I set Foo.calculation_results directly and also not actually call calculate()? The fact that calculate() doesn't take params or return anything is making it tough to follow most examples I see of mocking--otherwise I could just set a return_value or, inside the function, set the attribute based on the params passed.