So we’ll say
json.dumps() and we’ll just put in a very simple dictionary here with
'a' as the key and
1 as the value. And let’s remind ourself what is available, what methods come with this
But remember that when you call a method on a
Mock object, it creates an instance of another
Mock object, so the
dir() representation of
json.dumps will also have this
.assert_called() method because it is a
Mock object as well.
So I’m going to copy this, paste it. And another method we have here is
'assert_called_once'. So we’re going to change this to
.assert_called_once(), save it, and let’s clear the screen, and we’ll run the program again.
And we get another
None. So I interpret this as basically, like, true. So it was called once because on line 5, we called it once. Now let’s try to break this and we’ll repeat this line here, so we’re actually calling
Now we’ll save this and run the program again, and this time we get an
AssertionError. It says
Expected 'dumps' to have been called once. But it’s
Called 2 times. Now you may start to see why this is really valuable and useful in your tests, because sometimes you will want to test that your methods or your functions are only called a certain number of times.
I’m going to clear the screen, and let’s look at another assertion method. We’ll copy line 9 and we’ll change this to
.assert_called_with(). This assertion method allows you to check and verify that your functions are called with the parameters that you expect them to be called with. So first, let’s try to make this pass the test, so to speak.
Become a Member to join the conversation.