Last active
December 10, 2015 09:28
-
-
Save MarkBennett/4414269 to your computer and use it in GitHub Desktop.
Am I misunderstanding how to use Angular/q deferred? Why does the result of deferred.resolve() never seem to make it into $scope.nextEvent. Thanks!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
clientApp.controller('MainCtrl', function($scope, events) { | |
events.requestNextEvent().then(function(next_event) { | |
$scope.nextEvent = next_event; | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
describe('Controller: MainCtrl', function() { | |
// load the controller's module | |
beforeEach(module('clientApp')); | |
var MainCtrl, | |
scope, | |
rootScope, | |
mockEvents; | |
// Initialize the controller and a mock scope | |
beforeEach(inject(function($rootScope, $controller, $q) { | |
var deferred; | |
rootScope = $rootScope; | |
scope = $rootScope.$new(); | |
deferred = $q.defer(); | |
mockEvents = { | |
requestNextEvent: function() { | |
var self = this; | |
deferred.promise.then(function() { | |
self.resolved = true; | |
}); | |
deferred.resolve({ | |
name: "Meetup", | |
description: "Join us this month for talks on integrating AngularJS with Rails, RubyFlux, and the new Reel server.", | |
startsOn: new Date(2012, 12, 29, 18, 30), | |
venueAddress: "1234 Not Real Street", | |
venueUrl: "http://ualberta.net", | |
rsvpUrl: "http://plus.com" | |
}); | |
return deferred.promise; | |
} | |
}; | |
spyOn(mockEvents, 'requestNextEvent').andCallThrough(); | |
MainCtrl = $controller('MainCtrl', { | |
$scope: scope, | |
events: mockEvents | |
}); | |
})); | |
it('should request the next event', function() { | |
expect(mockEvents.requestNextEvent).toHaveBeenCalled(); | |
}); | |
it('should attach the nextEvent to the scope', function() { | |
// GOT IT!! | |
// | |
// The $q service comes from the $QProvider. This provider creates a new | |
// $q service using the qFactory(nextTick, exceptionHandler) signature. | |
// The default nextTick function is what registered work to happen. By | |
// default it registers work using $rootScope.$evalAsync which isn't called | |
// until after the next digest happens. | |
// | |
// Therefore to make this test pass I just needed to digest the $rootScope | |
// and voila. Magic! :) | |
rootScope.$digest(); | |
waitsFor(function() { | |
return mockEvents.resolved; | |
}, 'requestNextEvent did not resolve', 5000); | |
runs(function() { | |
expect(scope.nextEvent).toBeDefined(); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment