我试图编写一个茉莉花测试来检查数据是否输入到数组中。我有视图模型,单元(jasmine)测试文件和阶段文件来设置单元测试的信息。我得到的错误是说我的数组是未定义的。错误消息显示"预期未定义为{SiteId:0,FirstName:' Jon',LastName:' Walker',ObjectState:1}"
这是我的viewmodel。我正在尝试测试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());
}
};
}
这是我的初始化茉莉花测试信息的舞台文件:
//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);
});
});
非常感谢任何调试我的茉莉花测试的帮助。
答案 0 :(得分:0)
我在viewModel中创建了Patients和observable数组:
self.Patients = ko.observableArray();
在viewModel中编辑了addPatient:
self.addPatient = function (patient) {
self.Patients.push(patient);
},
并修改了“添加患者检查”单元测试:
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);
});
});
viewmodel和unit测试的这些更改解决了我的问题。从这里我也可以检查患者的姓名并进行其他测试以验证。 Xv在评论中的解释帮助我找到了这个解决方案