Jasmine 在抱怨一个对象不等于一个对象
Jasmine is complaining that an object is not equal to an object
我正在尝试使用 jasmine 进行一些集成测试。我正在执行以下操作:
Jasmine.onTest(function () {
describe("Form", function() {
it("should lazy-load HeaderFields and FormFields", function() {
var hf1 = new HeaderField({
label: "Test HF1",
required: false,
sequence: 1,
field_type: "TEXT",
allows_pictures: false,
record_geo: false,
form_report_searchable: false
});
hf1.save();
var ff1 = new FormField({
label: "Test FF1",
required: false,
sequence: 1,
field_type: "TEXT",
allows_pictures: false,
allows_comments: false,
record_geo: false
});
ff1.save();
var form = new Form({
title: "Test Title",
header_fields: [hf1.id],
form_fields: [ff1.id],
created_by: "4444444",
created_on: new Date()
});
form.save();
// _headerFields and _formFields should both be undefined right now
expect(form._headerFields).toBe(undefined);
expect(form._formFields).toBe(undefined);
// Now trigger the lazy-loading of both, now they should not be null
var headers = form.headerFields;
var fields = form.formFields;
expect(form._headerFields).toBe([{_id: hf1.id, _label: 'Test HF1', _form_id: null, _sequence: 1, _field_type: 'TEXT', _field_options: null, _field_value: null, _required: false, _allows_pictures: false, _record_geo: false, _form_report_searchable: false}]);
expect(form._formFields).toBe([ff1]);
});
});
});
但是当它运行时,Jasmine 抱怨如下:
Expected [ { _id: '8WwdEfxfm7Df3Z8EH', _label: 'Test HF1', _form_id: null, _sequence: 1, _field_type: 'TEXT', _field_options: null, _field_value: null, _required: false, _allows_pictures: false, _record_geo: false, _form_report_searchable: false } ] to be [ { _id: '8WwdEfxfm7Df3Z8EH', _label: 'Test HF1', _form_id: null, _sequence: 1, _field_type: 'TEXT', _field_options: null, _field_value: null, _required: false, _allows_pictures: false, _record_geo: false, _form_report_searchable: false } ]
所以,从错误消息来看,它说 A 不等于 A。我如何修改测试才能使其正常工作?
您需要使用 toEqual
而不是 toBe
。 toEqual
将进行深度比较:如果所有属性都相等,则认为两个对象相等。
我正在尝试使用 jasmine 进行一些集成测试。我正在执行以下操作:
Jasmine.onTest(function () {
describe("Form", function() {
it("should lazy-load HeaderFields and FormFields", function() {
var hf1 = new HeaderField({
label: "Test HF1",
required: false,
sequence: 1,
field_type: "TEXT",
allows_pictures: false,
record_geo: false,
form_report_searchable: false
});
hf1.save();
var ff1 = new FormField({
label: "Test FF1",
required: false,
sequence: 1,
field_type: "TEXT",
allows_pictures: false,
allows_comments: false,
record_geo: false
});
ff1.save();
var form = new Form({
title: "Test Title",
header_fields: [hf1.id],
form_fields: [ff1.id],
created_by: "4444444",
created_on: new Date()
});
form.save();
// _headerFields and _formFields should both be undefined right now
expect(form._headerFields).toBe(undefined);
expect(form._formFields).toBe(undefined);
// Now trigger the lazy-loading of both, now they should not be null
var headers = form.headerFields;
var fields = form.formFields;
expect(form._headerFields).toBe([{_id: hf1.id, _label: 'Test HF1', _form_id: null, _sequence: 1, _field_type: 'TEXT', _field_options: null, _field_value: null, _required: false, _allows_pictures: false, _record_geo: false, _form_report_searchable: false}]);
expect(form._formFields).toBe([ff1]);
});
});
});
但是当它运行时,Jasmine 抱怨如下:
Expected [ { _id: '8WwdEfxfm7Df3Z8EH', _label: 'Test HF1', _form_id: null, _sequence: 1, _field_type: 'TEXT', _field_options: null, _field_value: null, _required: false, _allows_pictures: false, _record_geo: false, _form_report_searchable: false } ] to be [ { _id: '8WwdEfxfm7Df3Z8EH', _label: 'Test HF1', _form_id: null, _sequence: 1, _field_type: 'TEXT', _field_options: null, _field_value: null, _required: false, _allows_pictures: false, _record_geo: false, _form_report_searchable: false } ]
所以,从错误消息来看,它说 A 不等于 A。我如何修改测试才能使其正常工作?
您需要使用 toEqual
而不是 toBe
。 toEqual
将进行深度比较:如果所有属性都相等,则认为两个对象相等。