Angularjs Jasmine:测试返回承诺的函数
我正在使用Jasmine为我的角度应用程序编写测试。所有的考试都通过了。我的班级情况如下:Angularjs Jasmine:测试返回承诺的函数,angularjs,testing,typescript,jasmine,Angularjs,Testing,Typescript,Jasmine,我正在使用Jasmine为我的角度应用程序编写测试。所有的考试都通过了。我的班级情况如下: class xyz implements ng.IComponentController { private myList: ng.IPromise<MyList[]> ; //declare necessary variables /* @ngInject */ constructor(private ListService: ListService,
class xyz implements ng.IComponentController {
private myList: ng.IPromise<MyList[]> ;
//declare necessary variables
/* @ngInject */
constructor(private ListService: ListService,
) {
this.myList = this.ListService.getList();
}
public onChange(): void {
this.isNameUnique(this.name).then(function(unique){
scope.isUnique = unique;
scope.errorNameInput = !reg.test(scope.name) || !scope.isUnique;
scope.myFunction({
//do something
});
});
}
public isNameUnique(name: string): ng.IPromise<boolean> {
return this.myList
.then(
(names) => {
_.mapValues(names, function(name){
return name.uuid.toLowerCase();
});
return (_.findIndex(names, { uuid : uuid.toLowerCase() }) === -1) ? true : false;
});
}
}
我希望这个测试涵盖以下内容:scope.errorNameInput=!注册测试(范围名称)| |!scope.isUnique
和调用myFunction()
,但它仍然显示未覆盖。不知道为什么
请让我知道如果你看到任何其他错误,因为我是新的角度和茉莉花。谢谢 您需要调用
$scope.$digest()
使您的承诺在测试中得到解决。有一个方便的教程深入讨论了这一点
希望有帮助 您需要调用
$scope.$digest()
使您的承诺在测试中得到解决。有一个方便的教程深入讨论了这一点
希望有帮助 我认为承诺已经解决了,这就是为什么它给了我正确的唯一性结果,并且测试通过了。让我仔细读一下。谢谢。测试你的承诺解析理论很容易——只需在你的
then()
中插入console.log()
,看看它是否在测试中被击中:)这里有一篇关于Angular 1.x的更深入的文章(同样的原理仍然适用):这有助于了解,但仍然无法回答我的任何问题。就在expect之前(唯一)。toEqual(错误)
放置一个console.log-如果在测试运行时它打印到您的屏幕上,这将很快验证您的承诺是否在测试中得到了解决。我认为承诺已经得到了解决,这就是为什么它给了我正确的唯一性结果,并且测试通过了。让我详细阅读一下。谢谢。测试这个理论很容易你的承诺解决-只需在你的then()
中插入console.log()
,看看它是否在你的测试中被击中:)这里有一篇关于Angular 1.x的更深入的文章(同样的原则仍然适用):这有助于了解,但仍然无法回答我的任何问题。就在expect(unique).toEqual(false)之前代码>放置一个console.log-如果在测试运行时它打印到您的屏幕上,这将很快验证您的承诺在测试中得到了解决。
this.$scope.myFunction = jasmine.createSpy('myFunction');
it('should ...', function() {
this.view.find(NAME_INPUT).val('blue').change(); // my view element.
this.getList.resolve(myList);
this.controller.isNameUnique('blue').then(function (unique) {
expect(unique).toEqual(false); //since blue is already in my json
expect(this.controller.errorNameInput).toEqual(true); //since its not unique, errornameinput will be set to true
expect(this.$scope.myFunction).toHaveBeenCalled();
});
});