2

I'm using the mock library and I want to be able to mock the save() function of my model class in the following way:

Twice the original function should be invoked (and succeed in actually saving the model), and the third time it should throw an exception.

This is for a unit test of a function that calls save three times (and this particular test needs to handle a case in which only the third call fails).

5
  • "fails"? That's weird. Are you testing to see of the ORM works? Are you testing to see if your database works? Those are very odd things to test. If you're testing to see if duplicates are rejected, you don't need a mock, just a fixture. What are you testing? Commented Aug 31, 2011 at 19:18
  • I'm trying to test how my code reacts to a failure to save, which can occur legitimately for a variety of reasons. I don't see how a fixture can help here. Commented Sep 1, 2011 at 5:07
  • A "failure to save"? I'm utterly baffled by this. Are you trying to detect a failure in the RDBMS? Through your application? That's so wrong. If the RDBMS fails, then the system -- as a whole -- is too broken to make any valid assertions about. This is NOT the kind of think you unit test. If you test RDBMS failure, you have to test OS failure, power failure, and other random acts of God. Why stop with the RDBMS? Why not test Apache failures? mod_wsgi failures? Commented Sep 1, 2011 at 9:42
  • Saving can fail, for example, when doing so violates some constraint. However, that's beside the point. The question above is about a technique in general and I really don't see the point in arguing over whether or not the particular test in question is warranted. I'd appreciate answers about mocking repeated calls with different results, as described above. Not interested in another fixtures vs. mocks comment war (as in stackoverflow.com/questions/3813688/… ). Commented Sep 1, 2011 at 15:52
  • "Saving can fail, for example, when doing so violates some constraint". Then provide a test case which does that. No mocking involved. Since you have an very elegant example, why not use it? Commented Sep 1, 2011 at 15:58

1 Answer 1

1

Based on http://pypi.python.org/pypi/mock documentation.

>>> values = [1, 2]
>>> def side_effect():
...     return values.pop()
...
>>> real = SomeModelClass()
>>> real.save = Mock(side_effect=side_effect)

Should work twice, then fail with an IndexError every time after that.

I have no clue what that can possibly demonstrate about your code when the infrastructure stops working. Are you also testing all OS calls? All Python library calls? All other Django methods to see that your application somehow copes with those failures?

Sign up to request clarification or add additional context in comments.

2 Comments

This seems to just return two values.. I need the first two calls to be delegated to the original method and the 3rd call to raise an exception.
@gigantt.com: The 3rd call will raise an exception. Did you try it? The "delegation" shouldn't much matter, since you're UNIT testing, not integration testing.

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.