数组的 Jasmine 测试一直失败
Jasmine test for an array keeps failing
我正在尝试编写 jasmine 测试来检查数据是否输入到数组中。我有视图模型、单元 (jasmine) 测试文件和用于设置单元测试信息的阶段文件。我得到的错误是说我的数组未定义。错误消息显示 "Expected undefined to be {SiteId: 0, FirstName: 'Jon', LastName: 'Walker', ObjectState: 1}"
这是我的视图模型。我正在尝试测试 self.addPatient:
var EF = (function () {
return {
ObjectState: {
Unchanged: 0,
Added: 1,
Modified: 2,
Deleted: 3
}
};
})();
var patientMapping = {
'Patients': {
key: function (patient) {
return ko.utils.unwrapObservable(patient.PatientId);
},
create: function (options) {
return new PatientViewModel(options.data);
}
}
};
PatientViewModel = function (data) {
var self = this;
ko.mapping.fromJS(data, {}, self);
self.flagPatientAsEdited = function () {
if (self.ObjectState() != ObjectState.Added) {
self.ObjectState(ObjectState.Modified);
}
return true;
},
self.FullName = ko.computed(function () {
return (self.FirstName() + " " + self.LastName());
});
}
SiteViewModel = function (data) {
var self = this;
ko.mapping.fromJS(data, patientMapping, self);
self.save = function () {
$.ajax({
url: "/Site/Save/",
type: "POST",
data: ko.toJSON(self),
contentType: "application/json",
success: function (data) {
if (data.siteViewModel != null) {
alert("Changes were saved successfully.");
ko.mapping.fromJS(data.siteViewModel, {}, self);
}
if (data.newLocation != null) {
window.location = data.newLocation;
}
}
});
},
self.flagSiteAsEdited = function () {
if (self.ObjectState() != ObjectState.Added) {
self.ObjectState(ObjectState.Modified);
}
return true;
},
self.addPatient = function () {
var patient = new PatientViewModel({ SiteId: 0, FirstName: "", LastName: "", ObjectState: ObjectState.Added });
self.Patients.push(patient);
},
self.deletePatient = function (patient) {
self.Patients.remove(this);
if (patient.PatientId() > 0 && self.PatientsToDelete.indexOf(patient.PatientId()) == -1) {
self.PatientsToDelete.push(patient.PatientId());
}
};
}
这是我的阶段文件,用于为 jasmine 测试初始化信息:
//staged data for view model testing
var OBP = OBP || {};
OBP.Testing = (function () {
var reset = function () {
var isReadOnly = true;
window.isReadOnly = true;
window.ObjectState = 2;
window.FirstName = "Joe";
window.LastName = "Mitchell";
window.SiteId = 5;
window.patient = { "SiteId": 0, "FirstName": "Jon", "LastName": "Walker", "ObjectState": 1 };
window.patientDelete = { "PatientId": 1, "SiteId": 0, "FirstName": "Frank", "LastName": "Smith", "ObjectState": 1 };
};
return {
reset: reset
};
})();
OBP.Testing.reset();
这是我的茉莉花测试本身失败了:
/// <reference path="../../lib/jasmine.js"/>
/// <reference path="../../lib/mock-ajax.js" />
/// <reference path="../../jquery-2.1.3.min.js" />
/// <reference path="../../lib/jasmin-jquery.js" />
/// <reference path="../../bootstrap.js" />
/// <reference path="../../knockout-3.2.0.js" />
/// <reference path="../../knockout.mapping-latest.js" />
/// <reference path="SiteViewModelStage.js" />
/// <reference path="../../siteviewmodel.js" />
describe("Add patient check:", function () {
beforeEach(function () {
OBP.Testing.reset()
});
var Patients = new Array();
var viewModel = new SiteViewModel({ patient: window.patient });
it("Add patient works", function () {
expect(Patients[0]).toBe(patient);
});
});
如果能帮助调试我的茉莉花测试,我们将不胜感激。
我在 viewModel 中创建了 Patients 和 observable 数组:
self.Patients = ko.observableArray();
在 viewModel 中编辑了 addPatient:
self.addPatient = function (patient) {
self.Patients.push(patient);
},
并修改了 "Add patient check" 单元测试:
describe("Add patient check:", function () {
beforeEach(function () {
OBP.Testing.reset()
});
var viewModel = new SiteViewModel({ patient: window.patient });
viewModel.addPatient(new PatientViewModel({ PatientId: 1, SiteId: 0, FirstName: "", LastName: "", ObjectState: ObjectState.Added }));
it("Add patient works", function () {
expect(viewModel.Patients().length == 1).toBe(true);
});
});
对视图模型和单元测试的这些更改解决了我的问题。从这里我还可以检查患者的姓名并进行其他测试以进行验证。 Xv 在评论中的解释帮助我找到了这个解决方案
我正在尝试编写 jasmine 测试来检查数据是否输入到数组中。我有视图模型、单元 (jasmine) 测试文件和用于设置单元测试信息的阶段文件。我得到的错误是说我的数组未定义。错误消息显示 "Expected undefined to be {SiteId: 0, FirstName: 'Jon', LastName: 'Walker', ObjectState: 1}"
这是我的视图模型。我正在尝试测试 self.addPatient:
var EF = (function () {
return {
ObjectState: {
Unchanged: 0,
Added: 1,
Modified: 2,
Deleted: 3
}
};
})();
var patientMapping = {
'Patients': {
key: function (patient) {
return ko.utils.unwrapObservable(patient.PatientId);
},
create: function (options) {
return new PatientViewModel(options.data);
}
}
};
PatientViewModel = function (data) {
var self = this;
ko.mapping.fromJS(data, {}, self);
self.flagPatientAsEdited = function () {
if (self.ObjectState() != ObjectState.Added) {
self.ObjectState(ObjectState.Modified);
}
return true;
},
self.FullName = ko.computed(function () {
return (self.FirstName() + " " + self.LastName());
});
}
SiteViewModel = function (data) {
var self = this;
ko.mapping.fromJS(data, patientMapping, self);
self.save = function () {
$.ajax({
url: "/Site/Save/",
type: "POST",
data: ko.toJSON(self),
contentType: "application/json",
success: function (data) {
if (data.siteViewModel != null) {
alert("Changes were saved successfully.");
ko.mapping.fromJS(data.siteViewModel, {}, self);
}
if (data.newLocation != null) {
window.location = data.newLocation;
}
}
});
},
self.flagSiteAsEdited = function () {
if (self.ObjectState() != ObjectState.Added) {
self.ObjectState(ObjectState.Modified);
}
return true;
},
self.addPatient = function () {
var patient = new PatientViewModel({ SiteId: 0, FirstName: "", LastName: "", ObjectState: ObjectState.Added });
self.Patients.push(patient);
},
self.deletePatient = function (patient) {
self.Patients.remove(this);
if (patient.PatientId() > 0 && self.PatientsToDelete.indexOf(patient.PatientId()) == -1) {
self.PatientsToDelete.push(patient.PatientId());
}
};
}
这是我的阶段文件,用于为 jasmine 测试初始化信息:
//staged data for view model testing
var OBP = OBP || {};
OBP.Testing = (function () {
var reset = function () {
var isReadOnly = true;
window.isReadOnly = true;
window.ObjectState = 2;
window.FirstName = "Joe";
window.LastName = "Mitchell";
window.SiteId = 5;
window.patient = { "SiteId": 0, "FirstName": "Jon", "LastName": "Walker", "ObjectState": 1 };
window.patientDelete = { "PatientId": 1, "SiteId": 0, "FirstName": "Frank", "LastName": "Smith", "ObjectState": 1 };
};
return {
reset: reset
};
})();
OBP.Testing.reset();
这是我的茉莉花测试本身失败了:
/// <reference path="../../lib/jasmine.js"/>
/// <reference path="../../lib/mock-ajax.js" />
/// <reference path="../../jquery-2.1.3.min.js" />
/// <reference path="../../lib/jasmin-jquery.js" />
/// <reference path="../../bootstrap.js" />
/// <reference path="../../knockout-3.2.0.js" />
/// <reference path="../../knockout.mapping-latest.js" />
/// <reference path="SiteViewModelStage.js" />
/// <reference path="../../siteviewmodel.js" />
describe("Add patient check:", function () {
beforeEach(function () {
OBP.Testing.reset()
});
var Patients = new Array();
var viewModel = new SiteViewModel({ patient: window.patient });
it("Add patient works", function () {
expect(Patients[0]).toBe(patient);
});
});
如果能帮助调试我的茉莉花测试,我们将不胜感激。
我在 viewModel 中创建了 Patients 和 observable 数组:
self.Patients = ko.observableArray();
在 viewModel 中编辑了 addPatient:
self.addPatient = function (patient) {
self.Patients.push(patient);
},
并修改了 "Add patient check" 单元测试:
describe("Add patient check:", function () {
beforeEach(function () {
OBP.Testing.reset()
});
var viewModel = new SiteViewModel({ patient: window.patient });
viewModel.addPatient(new PatientViewModel({ PatientId: 1, SiteId: 0, FirstName: "", LastName: "", ObjectState: ObjectState.Added }));
it("Add patient works", function () {
expect(viewModel.Patients().length == 1).toBe(true);
});
});
对视图模型和单元测试的这些更改解决了我的问题。从这里我还可以检查患者的姓名并进行其他测试以进行验证。 Xv 在评论中的解释帮助我找到了这个解决方案