1

我正在尝试使用 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。如何修改测试以使其工作?

4

1 回答 1

1

您需要使用toEqual而不是toBe. toEqual将进行深度比较:如果所有属性都相等,则两个对象都被视为相等。

于 2015-01-20T19:48:56.380 回答