gpt4 book ai didi

javascript - 监视服务方法时 Jasmine 单元测试失败

转载 作者:行者123 更新时间:2023-11-30 16:57:21 24 4
gpt4 key购买 nike

我有一个 Angular 模块,验证,它是这样声明的:

(function(ns){
ns.validation = angular.module("validation", []);
})(blog);

该模块包含两个服务,validator 和 validationRulesProvider,如下所示:

(function(module){
module
.factory("validationRulesProvider", function(){

var _getRules = function() {
return [{
isValid: function(post) {
return post.length > 0;
}
}];
};

return {
getRules: _getRules
};

});
})(blog.validation);

(function(module){
module
.factory("validator", ["validationRulesProvider", function(validationRulesProvider){

var _validate = function(post) {
var rules = validationRulesProvider.getRules();
for (var rule in rules) {
if (!rule.isValid(post)) {
return false;
}
}
return true;
};

return {
validate: _validate
};

}]);
})(blog.validation);

我正在尝试(使用 Jasmine)测试

  • validationRulesProvider 的 getRules 方法实际上是从 validate 方法调用的
  • post 参数贯穿从所述方法返回的每个规则

我有以下 Jasmine 测试脚本:

describe("Validator: ", function(){

var _validator;
var _mockedValidationRulesProvider;
var _mockRule;

beforeEach(function(){

module("validation");

inject(function(validationRulesProvider){
_mockedValidationRulesProvider = validationRulesProvider;
});

_mockRule = jasmine.createSpy();

spyOn(_mockedValidationRulesProvider, "getRules")
.and
.returnValue([{
isValid: _mockRule
}]);

inject(function(validator){
_validator = validator;
});

});

describe("getRules - ", function(){

it("gets a collection of rules from the rules provider", function(){
_validator.validate("");
expect(_mockedValidationRulesProvider.getRules).toHaveBeenCalled();
});

it("should pass the post through each rule received from the rules provider", function(){
expect(_mockRule.calls.count()).toEqual(_mockedValidationRulesProvider.getRules().length);
});

});

});

所以,我只是想创建一个伪造的 validationRulesProvider.getRules 实现。我的麻烦是这两个测试都失败了。如果我更改行:

    spyOn(_mockedValidationRulesProvider, "getRules")
.and
.returnValue([{
isValid: _mockRule
}]);

简单地成为

    spyOn(_mockedValidationRulesProvider, "getRules")
.and
.returnValue([]);

然后两个测试中的第一个通过,因为永远不会进入 validator.validate 中的循环。

Karma 给出以下输出:

PhantomJS 1.9.8 (Windows 7) Validator: getRules - gets a collection of rules from the rules provider FAILED TypeError: 'undefined' is not a function (evaluating 'rule.isValid(post)') at C:/Users/User/JS/Angular/Learning/blogsite/scripts/validation/validator.js:8 at C:/Users/User/JS/Angular/Learning/blogsite/scripts/tests/validator.test.js:32 PhantomJS 1.9.8 (Windows 7) Validator: getRules - should pass the post through each rule received from the rules provider FAILED Expected 0 to equal 1. at C:/Users/User/JS/Angular/Learning/blogsite/scripts/tests/validator.test.js:37 PhantomJS 1.9.8 (Windows 7): Executed 5 of 5 (2 FAILED) (0 secs / 0.039 secs)

对于为什么测试在第一个实例中失败,我有点不知所措,因为看起来我应该从 spy 返回的是一个包含“isValid”函数的对象的数组 - 这是正是从该函数的实际实现返回的内容。

我做错了什么?

最佳答案

当你想在 javascript 中循环一个数组时,你需要使用 javascript 基本的 'for loop' 而不是 'for-in loop'

验证器工厂中的代码应该是这样的。

for (var i = 0; i < rules.length; i++) {
var rule = rules[i];
if (!rule.isValid(post)) {
return false;
}
}

关于javascript - 监视服务方法时 Jasmine 单元测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29463570/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com