spec Module
00:00
You can also, instead of typing out the attributes one by one, you can actually pass in a module to the spec parameter directly. Let me show you what that looks like. Clear the screen, and this time I am in the directory in which we have my_calendar.py, so I do have access to that module.
00:22
I can say calendar equals a Mock object, and the spec is going to come from my_calendar.
00:35
Um, okay—I haven’t defined my_calendar, so I just need to import my_calendar.
00:42
Let me try that again. Okay. So now we’ve set the spec to the module my_calendar, so if we look at the dir() of calendar again, we will see some more built-in functions and all the Mock object functions and then 'get_holidays' and 'is_weekday'.
01:06
And we also see requests because we had imported that in the my_calendar module. Clear the screen. And I’ll just remind you that this is what we typed in. We’ve set the spec as the my_calendar module, so if we try to say calendar.is_weekday(),
01:27
it returns a Mock object. If we do the misspelled version, we get an AttributeError because is_weekdayy() doesn’t exist in the my_calendar module.
01:38
So the spec just sets the interface of the Mock object to be the same as the interface of the actual module, and that’s what we pretty much always want.
01:50 We always want it to be that way when we run our tests.
Become a Member to join the conversation.
