我有一个用angular 1.5开发的页面,它显示了初始API调用时的加载进度,我想为此编写测试用例。我使用变量this.loaded
来处理加载进度。最初是this.loaded = false
,一旦所有内容都被加载,它将是this.loaded =true
,但是当我编写测试用例时this.loaded
总是 true ,因为测试用例正在检查最终值变量。我如何减慢流量并检查进度是否被触发或不使用Jasmine测试?
HTML:
<loading-directive is-loaded='$ctrl.loaded'>
DOM to show after loaded
</loading-directive>
JS:
function componentCtrl (someFactory) {
var _this = this;
_this.loaded = false;
someFactory.someMethod().then(function () {
_this.loaded = false;
});
}
我想在调用发生之前检查_this.loaded = false。
答案 0 :(得分:0)
在出厂调用之前,您无法检查_this.loaded的值。你可以做的解决办法是以不改变_this.loaded的方式模拟工厂调用,你仍然可以检查它是错误的。我将创建一个可能为您提供见解的示例。我希望它有所帮助:
describe('MyController Spec', function() {
// Load module
beforeEach(angular.mock.module('myApp'));
it('Check if loaded is false if factory call does not change its value', inject(function($controller) {
//setup
spyOn(someFactory, 'someMethod').and.callFake(function() {
//create a promise mock that will not execute the code that
//changes _this.loaded
var uselessPromiseMock = {
then: function() {}
};
return uselessPromiseMock;
});
//action
var myController = $controller("componentCtrl");
//assert
expect(myController.loaded).toBeFalsy();
}));
it('Check if loaded is true after a normal controller initialization', inject(function($controller) {
//action
var myController = $controller("componentCtrl");
//assert
expect(myController.loaded).toBeTruthy();
}));
});
PS:我假设您输入错误并且您传递给工厂调用的回调将_this.loaded更改为true:)