呼び出されたことを確認する単体テストを作成しようとしてい$rootScope.$broadcast('myApiPlay', { action : 'play' });ます。
ここにmyapi.jsがあります
angular.module('myApp').factory('MyApi', function ($rootScope) {
var api = {};
api.play = function() {
$rootScope.$broadcast('myApiPlay', { action : 'play' });
}
return api;
});
そして、ここに私のユニットテストがあります:
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');
});
});
実行中に発生するエラーは次の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.
私も試してみましexpect(rootScope.$broadcast).toHaveBeenCalled()たが、同様のエラーが発生しています: Expected spy $broadcast to have been called..
そのメソッドが実際に正しいパラメーターで呼び出されていることを確認したいと思います。
ありがとうございました!