Simple example to mock ajax call with Mocha, Chai and Sinon

805 views
Skip to first unread message

yle...@gmail.com

unread,
Nov 14, 2014, 1:21:06 PM11/14/14
to moc...@googlegroups.com
Hi,
I'm new AngularJS and Mocha.
I'm trying to get a Mocha test working on AngularJS call.
I know Sinon can stub jQuery ajax call like
sinon.stub(jQuery, 'ajax') etc.
Can I do simlar for AngularJS $http.GET?

Or do I have to use sinon server? I tried sinon server, doesn't work like this:
        before(function() {
           ...... inject my module to be test
            server = sinon.fakeServer.create();
        });
        it("mock ajax return data", function () {
            var controller = createController();
            ...call my function (no parameter)

            // This is part of the FakeXMLHttpRequest API
            server.requests[0].respond(
                200,
                { "Content-Type": "application/json" },
                JSON.stringify([jobJSON])
            );

            expect(....

I have a HTML fixture for Mocha test, but it stopped when I try with nock, like
var nock=require('nock');

Thanks a lot.

Christopher Hiller

unread,
Nov 14, 2014, 1:33:34 PM11/14/14
to moc...@googlegroups.com

See ngMock and it’s mock $httpBackend implementation.

--
You received this message because you are subscribed to the Google Groups "Mocha" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mochajs+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

yle...@gmail.com

unread,
Nov 14, 2014, 1:53:27 PM11/14/14
to moc...@googlegroups.com
Well, in Mocha code, I tried to inject $httpBackend. But it doesn't work. It works for Jasmine.
before(function() {
            // Set up the mock http service responses
            var $injector = angular.injector([ 'ng',]);
            $httpBackend = $injector.get('$httpBackend');

Then I see it's popular to use Mocha, chai, and Sinon combination. But I couldn't figure out or find example code to do ajax mock with Sinon

Christopher Hiller

unread,
Nov 18, 2014, 12:11:40 AM11/18/14
to moc...@googlegroups.com
You need to RTFM and copy the examples.
Reply all
Reply to author
Forward
0 new messages