I am trying to write a unit test that verifies that $rootScope.$broadcast('myApiPlay', { action : 'play' });
is called.
Here is the myapi.js
angular.module('myApp').factory('MyApi', function ($rootScope) {
var api = {};
api.play = function() {
$rootScope.$broadcast('myApiPlay', { action : 'play' });
}
return api;
});
And here is my Unit Test:
describe('Service: MyApi', function () {
// load the service's module
beforeEach(module('myApp'));
// instantiate service
var MyApi;
var rootScope;
beforeEach(function () {
inject(function ($rootScope, _MyApi_) {
MyApi = _MyApi_;
rootScope = $rootScope.$new();
})
});
it('should broadcast to play', function () {
spyOn(rootScope, '$broadcast').andCallThrough();
rootScope.$on('myApiPlay', function (event, data) {
expect(data.action).toBe('play');
});
MyApi.play();
expect(rootScope.$broadcast).toHaveBeenCalledWith('myApiPlay');
});
});
Here is the error i'm getting while running grunt test
:
PhantomJS 1.9.7 (Windows 7) Service: MyApi should broadcast to pause FAILED
Expected spy $broadcast to have been called with [ 'myApiPlay' ] but it was never called.
I have also tried with expect(rootScope.$broadcast).toHaveBeenCalled()
and I am having a similar error: Expected spy $broadcast to have been called.
.
I would like to verify that that method has actually been called with the right parameters.
Thank you!