为什么我的间谍测试失败了
Why is my spy test failing?
我有以下测试:
beforeEach(inject(($document, _DirectiveReadyService_) => {
DirectiveReadyService = _DirectiveReadyService_;
foo = jasmine.createSpy('foo');
}));
it('should resolve `foo` subscriber when `foo` is published', () => {
DirectiveReadyService.subscribe('foo').then(foo);
DirectiveReadyService.publish('foo');
expect(foo).toHaveBeenCalled();
});
我的 DirectiveReadyService 看起来像:
export class DirectiveReadyService {
constructor($q, $rootScope) {
'ngInject';
this._$q = $q;
this._directives = {};
const resetDirectives = $rootScope.$on('$stateChangeStart', () => {
this._directives = {};
});
$rootScope.$on('$destroy', resetDirectives);
}
publish(type, ...params) {
if (this._directives[type]) {
console.log('publish', type);
this._directives[type].resolve(params);
}
}
subscribe(...types) {
const done = [];
console.log('subscribe', types);
types.forEach(type => {
this._directives[type] = this._directives[type] || this._$q.defer();
done.push(this._directives[type].promise);
});
return this._$q.all(done).then(result => {
console.log('done', result);
return result;
});
}
}
运行测试时,我在控制台中看到以下内容:
'subscribe', ['foo']
'publish', 'foo'
但我从来没有得到'done'
,我的测试失败了。但是,针对此服务进行开发并在浏览器控制台中预览会一直记录到'done'
因此我知道该服务有效。我在单元测试中做错了什么?
我想通了...我的测试$rootScope.$apply();
缺失。有了这个,承诺就解决了。
beforeEach(inject(($document, _$rootScope_, _DirectiveReadyService_) => {
DirectiveReadyService = _DirectiveReadyService_;
foo = jasmine.createSpy('foo');
$rootScope = _$rootScope_;
}));
it('should resolve `foo` subscriber when `foo` is published', () => {
DirectiveReadyService.subscribe('foo').then(foo);
DirectiveReadyService.publish('foo');
$rootScope.apply();
expect(foo).toHaveBeenCalled();
});
相关文章:
- 为什么不'当单元测试出现解析错误时,我的因果报应测试会失败
- Jasmine单元测试在监视服务方法时失败
- ChaiJS:无法导致测试失败
- 当我的单元测试失败时,回调被调用了两次
- 预期[false]为false量角器e2e测试失败
- 为什么在我的单元测试中instanceOf的测试失败
- yeoman generator聚合物的web组件测试失败
- 当Firefox尝试登录时,Selenium测试失败
- 将范围添加到角度种子默认视图控制器时,测试套件现在失败
- Javascript由于测试中的双反转逗号而失败
- 测试Ember.JS应用程序失败,返回ReferenceError:未定义Ember
- 量角器中的AngularJS E2E测试失败
- 用摩卡测试失败的承诺'内置的promise支持
- 这个单元测试失败的原因是什么
- 当分离到不同的文件中时,测试失败
- 量角器(用于测试角咆哮)通过/失败不一致
- 为什么这个茉莉花节点测试没有失败
- 茉莉节点未显示失败测试
- 如果服务器给出响应,如何失败测试
- Jasmine Spec Runner:失败测试的堆栈跟踪