【问题标题】:Jasmine is complaining that an object is not equal to an object茉莉花在抱怨一个对象不等于一个对象
【发布时间】:2015-01-20 16:58:18
【问题描述】:

我正在尝试使用 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。这将进行深度比较(如果所有属性都相等,则两个对象都被视为相等)。
  • 这似乎解决了我的问题。您可以将其添加为答案以便我接受吗?

标签: meteor jasmine meteor-velocity meteor-jasmine


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-24
    • 1970-01-01
    • 2016-03-21
    • 2016-10-13
    • 1970-01-01
    • 2014-12-22
    • 1970-01-01
    相关资源
    最近更新 更多