简体   繁体   English

Chai:断言变量不存在

[英]Chai: Assert a variable does not exist

How do I use Chai to verify that some variable does not exist?如何使用 Chai 验证某些变量不存在?

That is to say, my js doesn't have a statement var never_declared , and I want to verify that there isn't any such variable.也就是说,我的 js 没有声明var never_declared ,我想验证没有任何这样的变量。

I have tried all of following, but Karma keeps complaining "Can't find variable: never_declared".我已经尝试了以下所有方法,但 Karma 一直在抱怨“找不到变量:never_declared”。 Which is exactly what I'm trying to test.这正是我想要测试的。

should.not.exist(never_declared);
expect(never_declared).to.not.exist;
expect(never_declared).to.not.exist;
expect(never_declared).to.be.undefined;
expect(never_declared).to.be.an('undefined');
assert.isNotObject(never_declared);
assert.isUndefined(never_declared);

If Karma has a problem with it you can use another valid Chai form like:如果 Karma 有问题,您可以使用另一种有效的 Chai 形式,例如:

expect(typeof never_declared).to.eq('undefined');

or或者

expect(typeof never_declared === "undefined").to.be.true;

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

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