Checking object equality in Jasmine

前端 未结 5 1818
小蘑菇
小蘑菇 2021-02-01 11:51

Jasmine has built-in matchers toBe and toEqual. If I have an object like this:

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


        
5条回答
  •  轮回少年
    2021-02-01 12:24

    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.

    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();
    

提交回复
热议问题