【问题标题】:Jasmine test for object propertiesJasmine 测试对象属性
【发布时间】:2015-10-10 06:23:48
【问题描述】:

我想做的事

describe('my object', function() {
  it('has these properties', function() {
    expect(Object.keys(myObject)).toEqual([
      'property1',
      'property2',
      ...
    ]);
  });
});

但是Object.keys 当然会返回一个数组,根据定义,它是有序的……我更愿意让这个测试通过而不管属性排序(这对我来说很有意义,因为无论如何都没有对象键排序的规范...(至少到 ES5))。

我如何验证我的对象具有它应该具有的所有属性,同时确保它没有丢失任何属性,而不必担心以正确的顺序列出这些属性?

【问题讨论】:

    标签: javascript jasmine


    【解决方案1】:

    我是built in now

    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"
        }));
        expect(foo).not.toEqual(jasmine.objectContaining({
          c: 37
        }));
      });
    });
    

    或者,您可以使用外部检查,例如 _.has(包装 myObject.hasOwnProperty(prop)):

    var _ = require('underscore');
    describe('my object', function() {
      it('has these properties', function() {
        var props = [
          'property1',
          'property2',
          ...
        ];
        props.forEach(function(prop){
          expect(_.has(myObject, prop)).toBeTruthy();
        })
      });
    });
    

    【讨论】:

    • 但第二种方式的值不能被忽略,对吧?!
    • 好点,OP需要第一种方法来解决“没有测试值的测试键”的用例
    • 点赞jasmine.objectContaining 部分
    【解决方案2】:

    最简单的解决方案?排序。

    var actual = Object.keys(myObject).sort();
    var expected = [
      'property1',
      'property2',
      ...
    ].sort();
    
    expect(actual).toEqual(expected);
    

    【讨论】:

      【解决方案3】:
      it('should contain object keys', () => {
        expect(Object.keys(myObject)).toContain('property1');
        expect(Object.keys(myObject)).toContain('property2');
        expect(Object.keys(myObject)).toContain('...');
      });
      

      【讨论】:

      • 欢迎来到 StackOverflow!仅代码的答案不被认为是高质量的。请详细说明您的代码的作用以及它如何帮助解决问题。
      • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。
      【解决方案4】:

      我最终来到这里是因为我正在寻找一种方法来检查对象是否具有特定的属性子集。 我从_.hasObject.hasOwnProperties 开始,但是Expected false to be truthy 失败时的输出不是很有用。

      使用下划线的交集给了我更好的预期/实际输出

        var actualProps = Object.keys(myObj); // ["foo", "baz"]
        var expectedProps =["foo","bar"];
        expect(_.intersection(actualProps, expectedProps)).toEqual(expectedProps);
      

      在这种情况下,失败可能看起来更像 Expected [ 'foo' ] to equal [ 'foo', 'bar' ]

      【讨论】:

        【解决方案5】:

        这里也有一些新的可能解决方案:

        【讨论】:

          【解决方案6】:

          我更喜欢用这个;因为,你有更多的机会进行单独的测试。

          import AuthRoutes from '@/router/auth/Auth.ts';
          
          describe('AuthRoutes', () => {
              it('Verify that AuthRoutes be an object', () => {
                  expect(AuthRoutes instanceof Object).toBe(true);
              });
          
              it("Verify that authroutes in key 'comecios' contains expected key", () => {
                  expect(Object.keys(AuthRoutes.comercios)).toContain("path");
                  expect(Object.keys(AuthRoutes.comercios)).toContain("component");
                  expect(Object.keys(AuthRoutes.comercios)).toContain("children");
                  expect(AuthRoutes.comercios.children instanceof Array).toBe(true);
          
                  // Convert the children Array to Object for verify if this contains the spected key
                  let childrenCommerce = Object.assign({}, AuthRoutes.comercios.children);
                  expect(Object.keys(childrenCommerce[0])).toContain("path");
                  expect(Object.keys(childrenCommerce[0])).toContain("name");
                  expect(Object.keys(childrenCommerce[0])).toContain("component");
                  expect(Object.keys(childrenCommerce[0])).toContain("meta");
          
                  expect(childrenCommerce[0].meta instanceof Object).toBe(true);
                  expect(Object.keys(childrenCommerce[0].meta)).toContain("Auth");
                  expect(Object.keys(childrenCommerce[0].meta)).toContain("title");
          
              })
          });
          

          【讨论】:

            【解决方案7】:

            我迟到了这个话题,但是有一种方法可以让您检查对象是否具有属性或键/值对:

            expect(myObject).toHaveProperty(key);
            expect({"a": 1, "b":2}).toHaveProperty("a");
            

            expect(myObject).toHaveProperty(key,value);
            expect({"a": 1, "b":2}).toHaveProperty("a", "1");
            

            【讨论】:

            • 我没有看到toHaveProperty() 方法。这是茉莉花吗?
            • Jasmine 不支持该方法
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-04-01
            • 1970-01-01
            • 2020-08-29
            • 2014-03-04
            • 1970-01-01
            相关资源
            最近更新 更多