0
我有具有以下视图模型淘汰赛组件出现故障:淘汰赛,requirejs和Jasmine测试
define(["jquery", "knockout"], function ($, ko) {
return function() {
function companySet(value) {
var self = this;
self.id = ko.observable(value.id);
self.name = ko.observable(value.name);
self.numberOfCompanies = ko.observable(value.numberOfCompanies);
self.numberOfEmployees = ko.observable(value.numberOfEmployees);
}
function companySetList() {
var self = this;
self.companySets = ko.observableArray([]);
self.getCompanySets = function() {
self.companySets.removeAll();
$.getJSON("/companysets", function(data) {
$.each(data, function(key, value) {
self.companySets.push(new companySet(value));
});
});
};
}
this.viewModel = new companySetList();
this.viewModel.getCompanySets();
};
});
我的规格文件看起来像这样;
define(["jquery"], function ($) {
describe("Company Set List Tests", function() {
beforeEach(function(){
jasmine.Ajax.install();
})
afterEach(function(){
jasmine.Ajax.uninstall();
})
describe("when loaded", function() {
it("it should load the company sets", function() {
require(["app/files/company-set-list"]);
var request = jasmine.Ajax.requests.mostRecent();
expect(request.method).toBe("GET");
expect(request.url).toBe("/companysets");
});
});
describe("Testing ajax stuff", function(){
it("just test the friggen call", function(){
$.getJSON("/test", function(){});
expect(jasmine.Ajax.requests.mostRecent().url).toBe("/test");
});
});
});
});
公司集列表被加载,但它不调用getCompanySets方法。因此,我无法让测试工作。当作为挖空组件的一部分加载时,公司集列表视图模型按预期工作。
我错过了什么?
您的测试似乎不构建您的视图模型的实例吗?我假设'require'调用应该有一个正在实例化的返回值? –
我已经尝试了很多方法来要求在该模块中拉...更好的问题是如何测试Jasmine视图模型中的$ .getJSON调用? – cloggins