我的项目是用angular编写的,用Jasmine编写测试用例。这些测试用例与Karma一起运行。
我在控制器中有一个作用域函数,里面有一个if条件。此if条件语句在Jquery中。我不确定如何测试这段代码的if和else部分。
$scope.getHelperMessage = function(type, target) {
$scope.helperMessageHidden = false;
$(target).addClass("icon-selected");
if (type === "password") { // this if condition is covered when I pass type as "password" in the fn args
$scope.helperMessage = passwordInfoMsg;
} else if (type === "personal") {
$scope.helperMessage = personalNumberInfoMsg;
} else if (type === "email") {
if ($(".email-wrapper .error-icon").css('display') === 'none') { // this if condition is not covered as i dont know how to set this statement in my test case
$scope.helperMessageHidden = true;
} else {
$(".form-message-container p").html(alreadyRegisteredInfoMsg);
}
}
}下面是我对上述作用域函数的测试用例
describe('on focus of any input field', function() {
it('should show the password helper message', inject( function(passwordInfoMsg){
var type = "password";
var target = ".password-wrapper .info-icon";
scope.getHelperMessage(type, target);
expect(scope.helperMessageHidden).toBe(false);
expect(scope.helperMessage).toBe(passwordInfoMsg);
}));
it('should show the personal number helper message', inject( function(personalNumberInfoMsg){
var type = "personal";
var target = ".personal-wrapper .info-icon";
scope.getHelperMessage(type, target);
expect(scope.helperMessageHidden).toBe(false);
expect(scope.helperMessage).toBe(personalNumberInfoMsg);
}));
it('should show the already exists email helper message', inject( function(alreadyRegisteredInfoMsg){
var type = "email";
var target = ".email-wrapper .error-icon"; // here I dont know how to set the value for if condition
scope.getHelperMessage(type, target);
expect(scope.helperMessageHidden).toBe(false);
}));
});

发布于 2015-04-23 23:40:58
为了测试,有没有可能用一个模拟来覆盖jQuery css函数,以返回您想要的值?例如:
it('should show the already exists email helper message', inject( function(alreadyRegisteredInfoMsg){
var type = "email";
var target = ".email-wrapper .error-icon";
$.fn.css = function() {return "none";}; //make $(...).css(...) === "none"
scope.getHelperMessage(type, target);
expect(scope.helperMessageHidden).toBe(false);
}));https://stackoverflow.com/questions/29821196
复制相似问题