$rootScope.$broadcast('myApiPlay', { action : 'play' });
'un çağrıldığını doğrulayan bir birim testi yazmaya çalışıyorum. İşte
angular.module('myApp').factory('MyApi', function ($rootScope) {
var api = {};
api.play = function() {
$rootScope.$broadcast('myApiPlay', { action : 'play' });
}
return api;
});
Ve burada benim Birim Test: İşte
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
çalıştırırken i alıyorum hatadır:
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()
ile de denedim ve benzer bir hata yaşıyorum: Expected spy $broadcast to have been called.
.
Bu yöntemin gerçekten doğru parametrelerle çağrıldığını doğrulamak istiyorum.
Teşekkür ederiz!
sadece sıralamasını değiştirmek gerekir mi? Aslında çağrılmadan önce beklemeniz gerekebilir, sizin durumunuzda, bunu bekleyiniz. Ayrıca, çağrı sayısını doğrulayabilirsiniz. – hassassin
Siparişi değiştirdim, aynı hata. – Andrea