Mocking Ajax services call with Protractor and Durandal

139 views
Skip to first unread message

Daniel Cabrera

unread,
Jun 16, 2015, 4:13:26 PM6/16/15
to duran...@googlegroups.com
Hi,

I am trying to build an E2E test for my application, but I do not want to call my services (as they are already tested and provide unexpected results).
I have been searching for a way to do something similar to what $httpBackend does for angular, so I can test my already deployed application for example.
Basically my idea would be to intercept those service calls that I want to mock, and leave the rest to return null (as they are not important for the test), so I can have faster tests and more controlled.
Is there any way to do this on Durandal?

Regards,

Mark Anders

unread,
Jun 17, 2015, 1:08:42 AM6/17/15
to duran...@googlegroups.com
I've had good success using MockJax: https://github.com/jakerella/jquery-mockjax.

I've done exactly what you describe, i.e., mock only a few methods and let the rest be handled normally.

HTH.

MAnders.

Daniel Cabrera

unread,
Jun 19, 2015, 10:39:22 AM6/19/15
to duran...@googlegroups.com
Hi Mark, thanks for the response. That is exactly what I needed!
One quick question, because I have been trying to use it, but I wasn't able to import mockjjax using require on protractor. For some reason I am getting undefined everytime.
I saw in the js of mockjax itself the following comment:

// NOTE: To use Mockjax as a Node module you MUST provide the factory with
        // a valid version of jQuery and a window object (the global scope):
        // var mockjax = require('jquery.mockjax')(jQuery, window);

But so far I wasn't able to properly import mockjax to use it.
Do you have any example on this or any page that I can use as reference?

Thanks & Regards,

Mark Anders

unread,
Jun 19, 2015, 1:50:08 PM6/19/15
to duran...@googlegroups.com
We don't use Node, just plain ol' ASP.Net, and the way I use it was so easy to implement that I did not give a second thought to how it was being loaded.

In my main.js, as part of the app.start().then() flow, I call:

        // Set up the test harness.
        testHarness.initialize();

TestHarness is one of my modules, and it is referenced via the standard define([]) declaration.

In the initialize() method is the following (plus a bunch of others just like it):

        $.mockjax({
          url: "/api/systeminfo",
          contentType: 'text/json',
          response: function(settings) {
                // Create a first-class object, convert to Json, return as responseText.
                var open = moment.utc().startOf('day').add('hours', 14.50);
                var close = moment.utc().startOf('day').add('hours', 21.00);
                this.responseText = {marketOpen: open.toJSON(), marketClose: close.toJSON() };
            }
        });


That's all I do!

Sorry I can't be more helpful ...
Reply all
Reply to author
Forward
0 new messages