I'm having a lot of trouble getting this simple test working.
I've got an $scope.$on
listener in a controller that I want to test. I just want to make certain it's called after a broadcast event.
To do this, I thought the following code would work:
describe("Testing the parent controller: ", function() {
var scope, ctrl;
beforeEach(function() {
module("myApp");
inject(function($rootScope, $controller) {
scope = $rootScope.$new();
ctrl = $controller('parent-ctrl', {
$scope: scope,
});
});
});
it ("should trigger broadcast when current page updates", function() {
spyOn(scope, "$on");
scope.$broadcast("myEvent", 999);
expect(scope.$on).toHaveBeenCalled();
});
});
It doesn't (Expected spy $on to have been called.
). I've dug through numerous examples:
- How do I test an event has been broadcast in AngularJS? in-angularjs
- How do I test $scope.$on in AngularJS
- How can I test events in angular?
- unit test spy on $emit
- How do I unit test $scope.broadcast, $scope.$on using Jasmine
- How do I test $scope.$on in AngularJS
- How can I test Broadcast event in AngularJS
and learned a lot, but for some reason I'm just not making some critical connection.
I have noticed that the $on
handler does respond post-assertion, which is unhelpful. I've tried scope.$apply()
and .andCallThrough()
in various configurations but nothing seems to work.
How is this done?