[TIP] Making mock objects that call the original function

Kevin Tran hekevintran at gmail.com
Thu May 16 16:46:45 PDT 2013

The mock objects in the Jasmine testing framework have a `andCallthrough()` method.  This allows you have a mock that when called, calls the original function.  An example:

    describe("A spy, when configured to call through", function() {
      var foo, bar, fetchedBar;

      beforeEach(function() {
        foo = {
          setBar: function(value) {
            bar = value;
          getBar: function() {
            return bar;

        spyOn(foo, 'getBar').andCallThrough();

        fetchedBar = foo.getBar();

      it("tracks that the spy was called", function() {

      it("should not effect other functions", function() {

      it("when called returns the requested value", function() {

The only way I know how to do this in the Python mock library (http://www.voidspace.org.uk/python/mock/) is by setting the original function manually:

    with patch('my_project.forms.create_user') as mock_create_user:
        mock_create_user.side_effect = create_user
        client.post('/signup/', data)
        self.assertEqual(1, mock_create_user.call_count)
        self.assertEqual(1, User.objects.count())

Is there a better built-in way to do this?

More information about the testing-in-python mailing list