Sinon-确保对象没有财产

时间:2018-12-27 23:33:20

标签: sinon

与Sinon有否定负比赛的方法?具体来说,一个对象没有给定的属性?

谢谢!

3 个答案:

答案 0 :(得分:2)

目前没有内置的匹配器。

Sinon允许您创建custom matchers,因此您可以创建自己的文件,以下是基于the built-in has matcherdoesNotHave的一个文件:

import * as sinon from 'sinon';

const doesNotHave = (prop) => sinon.match(function (actual) {
  if(typeof value === "object") {
    return !(prop in actual);
  }
  return actual[prop] === undefined;
}, "doesNotHave");

test('properties', () => {
  const obj = { foo: 'bar' };
  sinon.assert.match(obj, sinon.match.has('foo'));  // SUCCESS
  sinon.assert.match(obj, doesNotHave('baz'));  // SUCCESS
})

答案 1 :(得分:1)

您不能为此使用sinon,而必须使用类似chai的东西。

您会这样做:

cont { expect } = require("chai");

expect({ foo: true }).to.not.have.keys(['bar']);

https://runkit.com/embed/w9qwrw2ltmpz

答案 2 :(得分:0)

我刚刚意识到可以在对象的形状中指定undefined进行检查:

sinon.assert.match(actual, {
  shouldNotExists: undefined
});

不能完全确定它是否100%有效,但似乎可以完成工作。

相关问题