测试对象是否具有多个属性

Testing if object has multiple properties

我在检查这个对象时遇到问题,我需要做些什么来链接多个 .to.have.property 语句吗?我相信我只是将上一个 .to.have.property 的结果返回到下一个

expect(shopify.formatRequestOptions("shop"))
  .to.have.property('url', "https://"+settings.shop+"/admin/shop.json")
  .to.have.property('method', "GET")
  .to.have.deep.property('headers.X-Shopify-Access-Token', settings.accessToken)

看来我可以用这样的东西chai-subset来检查一个对象。没有办法将它们链接在一起吗?我不想这样做。

var result = shopify.formatRequestOptions("shop")
expect(result).to.have.property('url', "https://"+settings.shop+"/admin/shop.json")
expect(result).to.have.property('method', "GET")
expect(result).to.have.deep.property('headers.X-Shopify-Access-Token', settings.accessToken)

可以构建您自己的功能 returns true / false 并且具有任何接口。

let example = {
  'name': 'thomas'
}

let hasAllProps = (obj, props) => {
  let propsTrue = _.chain(props)
  .map(prop => _.has(obj, prop))
  .without(false)
  .value()
  return (propsTrue.length === props.length)
}

console.log(hasAllProps(example, ['name'])) // true
console.log(hasAllProps(example, ['age'])) // false

我们可以使用nested.includes.

var result = shopify.formatRequestOptions("shop")
expect(result).to.nested.include({
    url: "https://"+settings.shop+"/admin/shop.json"
    method: 'GET',
    'headers.X-Shopify-Access-Token': settings.accessToken // this where nested kicks in, on the dot path
});