【发布时间】:2018-04-30 07:10:07
【问题描述】:
由于我是单元测试的新手,我想测试我的 javascript 代码,它非常令人困惑.. 我真的想要一些可以告诉我问题的解决方案的帮助.??
if (!Notification) {
Notification = {};
else {
if (typeof Notification != "object") {
throw new Error(" already exists ");
}
}
Notification.admin = function() {
var res = {};
var prevent = "";
var DefaultValue = function(ans, type, commnon, status) {
var notify = type.concat(common);
if ($("#method").val() == "false" && type == "Text") {
if (status) {
return data = '<label class="checkbox-label"><input data-role="ux-checkbox" type="checkbox" disabled="disabled" data-type="' + notify + '" class="grid-checkbox">  </label>';
} else {
return data = '<label class="checkbox-label">' + '<span class="no-checkbox-label" >' + "-" + '</span>' + '</label>';
}
} else if (status) {
if (ans) {
return data = '<label class="checkbox-label"><input data-role="ux-checkbox" checked=checked type="checkbox" data-type="' + notify + '" class="grid-checkbox ">  </label>';
} else {
return data = '<label class="checkbox-label"><input data-role="ux-checkbox" type="checkbox" data-type="' + notify + '" class="grid-checkbox">  </label>';
}
} else {
return data = '<label class="checkbox-label">' + '<span class="no-checkbox-label" >' + "-" + '</span>' + '</label>';
}
};
this.init = function() {
};}; Notification.Obj = new Notification.admin();
$(document).ready(function() {
Notification.Obj.init();
});
这是我的私人功能,我想在使用 mocha 和 chai 时对其进行单元测试..
我无法对这个函数进行单元测试
【问题讨论】:
-
你想要测试的单元是什么?我将
private function误读为private method。您的代码示例并没有真正说明snipplet 应该做什么以及您想要测试什么。在我看来,您可能会混淆单元测试(非常容易设置,不包括服务集成)和功能浏览器测试(自动浏览器点击)。因此,您的问题要么过于宽泛,要么实际上是您尝试的解决方案的 XY 问题。 -
@k0pernikus 我想测试 com.help.Notification.admin 函数中的 Defaultvalue 函数....
-
不要将您的
DefaultValue函数定义为管理函数中的闭包,而是使其可导入。您还可以通过 jquery 选择器$("#method").va获得副作用,它应该被传递到该函数中,以便您可以模拟它。然后它应该成为一个函数,需要 5 个输入值并创建一个标签标签字符串,然后您可以对其进行测试。
标签: javascript jquery unit-testing mocha.js chai