茉莉花测试函数返回本机承诺
jasmine test function returning native promise
我正在尝试编写一个 jasmine 测试用例来测试一个 returns promise 对象的函数。
我的 javascript 看起来像这样:
var eventUtil = (function() {
function ListWidget(config) {
var el = document.getElementById(config.elementID);
var promise = new Promise(function(resolve, reject) {
httpUtil(config.dataURL).get(null).then(function(data) {
data = JSON.parse(data);
var events = data.events;
resolve("success");
}, function(error) {
reject("error");
});
});
return promise;
}
return {
listWidget: ListWidget
}
})()
我的规格是这样的:
describe("Test suite for events", function() {
beforeEach(function() {
var eventList = {};
document.body.insertAdjacentHTML("beforeend", "<div id='list'></div>");
spyOn(window, "httpUtil").and.returnValue({
'get': function() {
return {
then: function(callback) {
return callback(eventList);
}
}
}
});
});
it("shall create event table", function() {
eventList = `{
"events": [{
"name": "A",
}, {
"name": "B",
}, {
"name": "C",
}, {
"name": "D",
}]
}`;
var promise = eventUtil.listWidget({
elementID: "test",
dataURL: "js/events.json"
});
var listDiv = document.getElementById('list');
expect(listDiv.innerHTML).toContain('<caption>Events ABC</caption>');
});
afterEach(function() {
var listDiv = document.getElementById('list');
listDiv.parentNode.removeChild(listDiv);
});
});
如何测试 eventUtil.ListWidget 函数 returns promise 对象?
将可选的 done 参数传递给您的 it 块。
it("shall create event table", function(done) {
eventList = {
"events": [{
"name": "A",
}, {
"name": "B",
}, {
"name": "C",
}, {
"name": "D",
}]
};
eventUtil.listWidget({
elementID: "test",
dataURL: "js/events.json"
}).then(function(){
var listDiv = document.getElementById('list');
expect(listDiv.innerHTML).toContain('<caption>Events ABC</caption>');
done();
});
});
我正在尝试编写一个 jasmine 测试用例来测试一个 returns promise 对象的函数。
我的 javascript 看起来像这样:
var eventUtil = (function() {
function ListWidget(config) {
var el = document.getElementById(config.elementID);
var promise = new Promise(function(resolve, reject) {
httpUtil(config.dataURL).get(null).then(function(data) {
data = JSON.parse(data);
var events = data.events;
resolve("success");
}, function(error) {
reject("error");
});
});
return promise;
}
return {
listWidget: ListWidget
}
})()
我的规格是这样的:
describe("Test suite for events", function() {
beforeEach(function() {
var eventList = {};
document.body.insertAdjacentHTML("beforeend", "<div id='list'></div>");
spyOn(window, "httpUtil").and.returnValue({
'get': function() {
return {
then: function(callback) {
return callback(eventList);
}
}
}
});
});
it("shall create event table", function() {
eventList = `{
"events": [{
"name": "A",
}, {
"name": "B",
}, {
"name": "C",
}, {
"name": "D",
}]
}`;
var promise = eventUtil.listWidget({
elementID: "test",
dataURL: "js/events.json"
});
var listDiv = document.getElementById('list');
expect(listDiv.innerHTML).toContain('<caption>Events ABC</caption>');
});
afterEach(function() {
var listDiv = document.getElementById('list');
listDiv.parentNode.removeChild(listDiv);
});
});
如何测试 eventUtil.ListWidget 函数 returns promise 对象?
将可选的 done 参数传递给您的 it 块。
it("shall create event table", function(done) {
eventList = {
"events": [{
"name": "A",
}, {
"name": "B",
}, {
"name": "C",
}, {
"name": "D",
}]
};
eventUtil.listWidget({
elementID: "test",
dataURL: "js/events.json"
}).then(function(){
var listDiv = document.getElementById('list');
expect(listDiv.innerHTML).toContain('<caption>Events ABC</caption>');
done();
});
});