过滤器对象和茉莉花的返回计数

时间:2017-04-07 05:09:48

标签: jasmine karma-jasmine

我是茉莉花业的新手。所以试图弄清楚如何在下面的场景中执行。

这很好用

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"
    }));
  });
});

但是现在如果我将对象更改为对象数组则不起作用。 那么,如何从对象数组中过滤并返回计数。

例如

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

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

       ];
      });

      it("matches objects with the expect key/value pairs", function() {
        expect(foo).//find object(s) containing bar:"baz" and it should return count=1
        }));
      });
});

1 个答案:

答案 0 :(得分:1)

如果没有jasmine.objectContaining,我们就可以实施:



describe("array.filter returns entries", function() {
  var foo;

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

  it("matched by key/value", function() {
    expect(foo.filter(function(element) {
         return element.bar === 'baz'
    }).length).toBe(1);
  });
});

<link href="//safjanowski.github.io/jasmine-jsfiddle-pack/pack/jasmine.css" rel="stylesheet" />
<script src="//safjanowski.github.io/jasmine-jsfiddle-pack/pack/jasmine-2.0.3-concated.js"></script>
&#13;
&#13;
&#13;