Javascript 测试html输出,但未定义
我已经编写了一个基本的测试框架,并且正在挑战自己用香草Javascript制作一个单页应用程序 我一直在努力弄清楚为什么我的视图测试在运行“列表”构造函数时无法识别它 我的specrunner已将所有文件加载到其中,并且我以前在模型上的测试工作正常。此外,在Specrunner中使用浏览器控制台模拟测试也会给出正确的输出 如果更快的话,请随意克隆我的回购协议 请注意,我的测试框架“espresso”使用expect代替assert,并且还有一个额外的参数用于测试描述 浓缩咖啡Javascript 测试html输出,但未定义,javascript,html,unit-testing,testing,Javascript,Html,Unit Testing,Testing,我已经编写了一个基本的测试框架,并且正在挑战自己用香草Javascript制作一个单页应用程序 我一直在努力弄清楚为什么我的视图测试在运行“列表”构造函数时无法识别它 我的specrunner已将所有文件加载到其中,并且我以前在模型上的测试工作正常。此外,在Specrunner中使用浏览器控制台模拟测试也会给出正确的输出 如果更快的话,请随意克隆我的回购协议 请注意,我的测试框架“espresso”使用expect代替assert,并且还有一个额外的参数用于测试描述 浓缩咖啡 var descr
var describe = function(description, test) {
document.getElementById("output").innerHTML +=
"<br><b>" + description + "</b></br>";
try {
test();
} catch (err) {
document.getElementById("output").innerHTML +=
"<br><li>error: " + err.message + "</li></br>";
console.log(err);
}
};
var expect = {
isEqual: function(description, first, second) {
if (first === second) {
document.getElementById("output").innerHTML +=
description +
"<br><li><font color='green'>PASS: [" +
first +
"] is equal to [" +
second +
"]</li></br>";
} else {
document.getElementById("output").innerHTML +=
"<br><li><font color='red'>FAIL: [" +
first +
"] is not equal to [" +
second +
"]</li></br>";
}
}
}
list-view.js
(function(exports) {
"use strict";
function View() {
this.test = "hello there";
View.prototype.convert = function(note) {
var output = [];
list.notelist.forEach(function(element) {
output.push("<br><ul>" + element.text + "</ul></br>");
});
console.log(output);
return output;
};
}
exports.View = View;
})(this);
view-tests.js(失败的测试)
描述(“视图#转换”,函数(){
var view=新视图();
等一下(
“将便笺转换为HTML列表”,
view.convert(list.notelist),
“
你好
”
);
});
提前感谢。您需要在view-test.js中定义
列表
describe("View #convert", function() {
var list = new List();
// ...
});
如果需要在此测试函数之外定义list
,则需要将其作为参数传入,或者在窗口
对象上定义它,以便全局访问
(function(exports) {
"use strict";
function View() {
this.test = "hello there";
View.prototype.convert = function(note) {
var output = [];
list.notelist.forEach(function(element) {
output.push("<br><ul>" + element.text + "</ul></br>");
});
console.log(output);
return output;
};
}
exports.View = View;
})(this);
describe("List #initialize", function() {
var list = new List();
expect.isEqual("blank array is loaded", list.notelist.length, 0);
});
describe("List #add", function() {
var list = new List();
list.add("hello");
expect.isEqual(
"can create and store a note",
list.notelist[0].show(),
"hello"
);
list.add("goodbye");
expect.isEqual(
"second note says goodbye",
list.notelist[1].show(),
"goodbye"
);
expect.isEqual("there are two notes in the list", list.notelist.length, 2);
});
describe("View #convert", function() {
var view = new View();
expect.isEqual(
"converts the note into a HTML list",
view.convert(list.notelist),
"<br><ul>hello</ul></br>"
);
});
describe("View #convert", function() {
var list = new List();
// ...
});