我有一个角分量,我想在调用异步方法之后测试状态。我该怎么做?我的方法doSomething不返回承诺!
angular.module('myModule').component('myComponent', {
template: '<p></p>',
controller: function($q) {
this.state = 1;
this.doSomething: function() {
var that = this;
setTimeout(function() { that.state = 2; }, 50);
}
}
});测试
describe('Test', function() {
var ctrl = null;
beforeEach(module('myModule'));
beforeEach(inject(function(_$componentController_) {
ctrl = _$componentController_('myComponent', null, {});
}));
it('should be 2', function() {
ctrl.doSomething();
expect(ctrl.state).toBe(2);
});
});发布于 2017-01-17 14:16:03
您需要在测试中等待更新,如下所示:
describe('Test', function() {
var ctrl = null;
beforeEach(module('myModule'));
beforeEach(inject(function(_$componentController_) {
ctrl = _$componentController_('myComponent', null, {});
}));
it('should be 2', function(done) {
ctrl.doSomething();
setTimeout(function() {
expect(ctrl.state).toBe(2);
done();
}, 52);
});
});希望能帮上忙。
https://stackoverflow.com/questions/41696178
复制相似问题