javascript 在 Jasmine 中检查对象相等性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16401237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 04:30:46  来源:igfitidea点击:

Checking object equality in Jasmine

javascriptbddjasmineobject-comparison

提问by Dan

Jasminehas built-in matchers toBeand toEqual. If I have an object like this:

Jasmine具有内置的匹配器toBetoEqual. 如果我有这样的对象:

function Money(amount, currency){
    this.amount = amount;
    this.currency = currency;

    this.sum = function (money){
        return new Money(200, "USD");
    }
}

and try to compare new Money(200, "USD")and the result of sum, these built-in matchers will not work as expected. I have managed to implement a work-around based on a custom equalsmethodand custom matcher, but it just seems to much work.

并尝试比较new Money(200, "USD")sum 的结果,这些内置匹配器将无法按预期工作。我已经设法实现了基于自定义equals方法和自定义匹配器的变通方法,但似乎工作量很大。

What is the standard way to compare objects in Jasmine?

在 Jasmine 中比较对象的标准方法是什么?

回答by lukas.pukenis

I was looking for the same thing and found an existing way to do so without any custom code or matchers. Use toEqual().

我一直在寻找同样的东西,并找到了一种无需任何自定义代码或匹配器的现有方法。使用toEqual().

回答by obfk

If you're looking to compare partial objects, you might consider:

如果您想比较部分对象,您可能会考虑:

describe("jasmine.objectContaining", function() {
  var foo;

  beforeEach(function() {
    foo = {
      a: 1,
      b: 2,
      bar: "baz"
    };
  });

  it("matches objects with the expect key/value pairs", function() {
    expect(foo).toEqual(jasmine.objectContaining({
      bar: "baz"
    }));
  });
});

cf. jasmine.objectContaining">jasmine.github.io/partial-matching

参见 jasmine.objectContaining">jasmine.github.io/partial-matching

回答by Andreas K?berle

Its the expected behavior, as two instances of an object are not the same in JavaScript.

这是预期的行为,因为一个对象的两个实例在 JavaScript 中是不同的。

function Money(amount, currency){
  this.amount = amount;
  this.currency = currency;

  this.sum = function (money){
    return new Money(200, "USD");
  }
}

var a = new Money(200, "USD")
var b = a.sum();

console.log(a == b) //false
console.log(a === b) //false

For a clean test you should write your own matcher that compares amountand currency:

对于干净的测试,您应该编写自己的匹配器来比较amountcurrency

beforeEach(function() {
  this.addMatchers({
    sameAmountOfMoney: function(expected) {
      return this.actual.currency == expected.currency && this.actual.amount == expected.amount;
    }
  });
});

回答by Alexander Poshtaruk

I found that lodash _.isEqual is good for that

我发现 lodash _.isEqual 对此有好处

expect(_.isEqual(result, expectedResult)).toBeTruthy()

回答by Baer

Your problem is with truthyness. You are trying to compare two different instances of an object which is true for regular equality ( a == b ) but not true for strict equality ( a === b). The comparator that jasmine uses is jasmine.Env.equals_()which looks for strict equality.

你的问题是真实性。您正在尝试比较一个对象的两个不同实例,该实例对于常规相等 ( a == b ) 成立,但对于严格相等 ( a === b ) 不成立。jasmine 使用的比较器是 jasmine.Env.equals_(),它寻找严格相等。

To accomplish what you need without changing your code you can use the regular equality by checking for truthyness with something a little like the following:

为了在不更改代码的情况下完成您需要的操作,您可以通过检查真实性来使用常规等式,如下所示:

expect(money1.sum() == money2.sum()).toBeTruthy();