So what does flexmock actually help you do?
Creating fake objects¶
Making a new object in Python requires defining a new class with all the fake methods and attributes you’re interested in emulating and then instantiating it. For example, to create a FakePlane object to use in a test in place of a real Plane object we would need to do something like:
class FakePlane(object): operational = True model = "MIG-21" def fly(self): pass plane = FakePlane() # this is tedious!
In other words, we must first create a class, make sure it contains all required attributes and methods, and finally instantiate it to create the object.
flexmock provides an easier way to generate a fake object on the fly using the flexmock() function:
plane = flexmock( operational=True, model="MIG-21")
It is also possible to add methods to this object using the same notation and Python’s handy lambda keyword to turn an attribute into a method:
plane = flexmock( operational=True, model="MIG-21", fly=lambda: None)
Replacing parts of existing objects and classes (stubs)¶
While creating fake objects from scratch is often sufficient, many times it is easier to take an existing object and simply stub out certain methods or replace them with fake ones. flexmock makes this easy as well:
flexmock( Train, # this can be an instance, a class, or a module get_destination="Tokyo", get_speed=200)
By passing a real object (or class or module) into the flexmock() function as the first argument it is possible to modify that object in place and provide default return values for any of its existing methods.
In addition to simply stubbing out return values, it can be useful to be able to call an entirely different method and substitute return values based on test-specific conditions:
(flexmock(Train) .should_receive("get_route") .replace_with(lambda x: custom_get_route()))
Creating and checking expectations¶
flexmock features smooth integration with pretty much every popular test runner, so no special setup is necessary. Simply importing flexmock into your test module is sufficient to get started with any of the following examples.
Expectations take many flavors, and flexmock has many different facilities and modes to generate them. The first and simplest is ensuring that a certain method is called:
The .once() modifier ensures that Train.get_destination() is called at some point during the test and will raise an exception if this does not happen.
Of course, it is also possible to provide a default return value:
Or check that a method is called with specific arguments:
In this example we used .times(1) instead of .once() and added the .at_least() modifier to demonstate that it is easy to match any number of calls, including 0 calls or a variable amount of calls. As you’ve probably guessed there is also an at_most() modifier.
While replacing method calls with canned return values or checking that they are called with specific arguments is quite useful, there are also times when you want to execute the actual method and simply find out how many times it was called. flexmock uses should_call() to generate this sort of expectations instead of should_receive():
In the above case the real get_destination() method will be executed, but flexmock will raise an exception unless it is executed exactly once. All the modifiers allowed with should_receive() can also be used with should_call() so it is possible to tweak the allowed arguments, return values and call times.
(flexmock(Train) .should_call("set_destination") .once() .with_args(object, str, int) .and_raise(Exception, re.compile("^No such dest.*")))
The above example introduces a handful of new capabilities – raising exceptions, matching argument types (object naturally matches any argument type) and regex matching on string return values and arguments.