Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I don't think $httpBackend is what you're after here, you want the whole factory to be mocked without it having a dependency on $http?</p> <p>Take a look at <a href="http://docs.angularjs.org/api/ng.$q" rel="noreferrer">$q</a>, in particular the code sample under the Testing header. Your issue might be resolved with code that looks like this:</p> <pre><code>'use strict'; describe('mocking the factory response', function () { beforeEach(module('myApp.controllers')); var scope, fakeFactory, controller, q, deferred; //Prepare the fake factory beforeEach(function () { fakeFactory = { requestPeople: function () { deferred = q.defer(); // Place the fake return object here deferred.resolve({ "one": "three" }); return deferred.promise; } }; spyOn(fakeFactory, 'requestPeople').andCallThrough(); }); //Inject fake factory into controller beforeEach(inject(function ($rootScope, $controller, $q) { scope = $rootScope.$new(); q = $q; controller = $controller('MyCtrl1', { $scope: scope, MyFactory: fakeFactory }); })); it('The peopleList object is not defined yet', function () { // Before $apply is called the promise hasn't resolved expect(scope.peopleList).not.toBeDefined(); }); it('Applying the scope causes it to be defined', function () { // This propagates the changes to the models // This happens itself when you're on a web page, but not in a unit test framework scope.$apply(); expect(scope.peopleList).toBeDefined(); }); it('Ensure that the method was invoked', function () { scope.$apply(); expect(fakeFactory.requestPeople).toHaveBeenCalled(); }); it('Check the value returned', function () { scope.$apply(); expect(scope.peopleList).toBe({ "one": "three" }); }); }); </code></pre> <p>I've added some tests around what $apply does, I didn't know that until I started playing with this!</p> <p>Gog</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload