简体   繁体   English

如何检查某些属性值而其他属性仅存在?

[英]How to check some properties values and other properties only existence?

Using chaijs, how do I check that some properties of the object are equal to something and other properties only exists in one?使用 chaijs,如何检查对象的某些属性是否等于某物而其他属性仅存在于一个中?

Something like类似的东西

return expect(profilePromise)
        .to.eventually.be.fulfilled
        .to.have.property('__data')
        .to.any.deep.equal({
          id: Number, <--------- here
          firstName: facebookProfile.first_name,
          lastName: facebookProfile.last_name,
          email: facebookProfile.email,
          gender: facebookProfile.gender,
          isPopularVerified: facebookProfile.is_verified,
          isVerified: facebookProfile.verified,
          locale: facebookProfile.locale,
          timezone: facebookProfile.timezone,

          createdOn: Number, <--------- here
          updatedOn: Number, <--------- here
        })

I want to know that I have id , createdOn and updatedOn properties, but I actually do not care about its values.我想知道我有idcreatedOnupdatedOn属性,但我实际上并不关心它的值。

Thank you谢谢

You can use a pattern match.您可以使用模式匹配。 Chai JSON Pattern plugin can help you to do this. Chai JSON Pattern插件可以帮助您做到这一点。

Eg例如

const chai = require('chai');
const chaiJsonPattern = require('chai-json-pattern').default;
const chaiAsPromised = require('chai-as-promised');

chai.use(chaiJsonPattern);
chai.use(chaiAsPromised);

const { expect } = chai;

describe('38598544', () => {
  it('should pass', () => {
    const profilePromise = Promise.resolve({
      __data: {
        id: Math.random(),
        firstName: 'facebookProfile.first_name',
        lastName: 'facebookProfile.last_name',
        email: 'facebookProfile.email',
        gender: 'facebookProfile.gender',
        isPopularVerified: 'facebookProfile.is_verified',
        isVerified: 'facebookProfile.verified',
        locale: 'facebookProfile.locale',
        timezone: 'facebookProfile.timezone',
        createdOn: Date.now(),
        updatedOn: Date.now(),
      },
    });

    return expect(profilePromise).to.eventually.be.fulfilled.to.have.property('__data').to.matchPattern(`{
      "id": Number,
      "firstName": "facebookProfile.first_name",
      "lastName": "facebookProfile.last_name",
      "email": "facebookProfile.email",
      "gender": "facebookProfile.gender",
      "isPopularVerified": "facebookProfile.is_verified",
      "isVerified": "facebookProfile.verified",
      "locale": "facebookProfile.locale",
      "timezone": "facebookProfile.timezone",

      "createdOn": Number,
      "updatedOn": Number,
    }`);
  });
});

test result:测试结果:

  38598544
    ✓ should pass


  1 passing (28ms)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM