Checking object equality in Jasmine

前端 未结 5 1812
小蘑菇
小蘑菇 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:14

    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.github.io/partial-matching

提交回复
热议问题