I have a controller that does two things:
1) executes a function when promise is fulfilled ($q.then)
2) executes a function when 'onLocationChangedStart' event is fired
In my unit test, I want to test the functionality of (1). I do this by executing $RootScope.$apply() to trigger the promise resolution. Unfortunately, it turns out that $rootScope.$apply() also emits an 'onLocationChangedStart' event where the url = http://server/ (this is within the angular code).
Obviously that's an issue since it will fire (2) even though that is not what I wanted for this particular unit test.
Is there any simple way around this that does not involve modifying original code just to get the unit test to run? (for example, I could put function (2) on $scope and then mock it to null prior to running the test. But that seems hackish)
Updated with scrubbed code sample:
angular.module('myMod')
.controller("ctrl", function ($scope, $log, myService, $location) {
myService.doSomething()
.then(function () {
//does something
})
})
$scope.$on('$locationChangeStart', routeChange);
function routeChange(event, currentUrl, nextUrl) {
//does something
};
})
//Jasmine unit test
describe('test', function () {
var $scope,
ctrl,
$rootScope;
beforeEach(function () {
module("myMod");
module("myServiceMock");
inject(function (_$rootScope_, $controller, _myService_) {
$rootScope = _$rootScope_;
$scope = $rootScope.$new();
ctrl = $controller('ctrl', {
$scope: $scope
});
});
});
it("Should set startup values appropriately", inject(function () {
//doing this triggers both the myService promise AND the location event listener
$rootScope.$apply();
}));