Javascript 茉莉花未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14676064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 17:34:47  来源:igfitidea点击:

Jasmine toBeUndefined

javascriptjasmine

提问by Tom Doe

I'm new to Jasmine and assumed using the .not.toBeDefined()or .toBeUndefined()matches you could check if a variable is undefined:

我是 Jasmine 的新手,假设使用.not.toBeDefined()or.toBeUndefined()匹配,您可以检查变量是否为undefined

describe('toBeDefined', function() {

    it('should be defined', function() {
        var obj = {};
        expect(obj).toBeDefined(); // Passes
    });

    it('should not be defined using .not.tobeDefined()', function() {
        //var obj = {};
        expect(obj).not.toBeDefined(); // Fails // ReferenceError: obj is not defined
    });

    it('should not be defined using .tobeUnefined()', function() {
        //var obj = {};
        expect(obj).toBeUndefined(); // Fails // ReferenceError: obj is not defined
    });

});

I completely get that this would fail within the code, but I assumed using those matches, it wouldn't. Am I just using these wrong, or is it not possible to write a spec to check if something is undefined?

我完全明白这会在代码中失败,但我假设使用这些匹配,它不会。我只是使用这些错误,还是无法编写规范来检查是否有问题undefined

回答by Pointy

The problem is that

问题是

  expect(obj).toBeUndefined();

fails before the call to Jasmine even happens. It's erroneous to refer to a variable that's not defined (in new browsers or in "strict" mode at least).

甚至在调用 Jasmine 之前就失败。引用未定义的变量是错误的(至少在新浏览器或“严格”模式下)。

Try this setup instead:

试试这个设置:

it('should not be defined using .tobeUnefined()', function() {
    var obj = {};
    expect(obj.not_defined).toBeUndefined(); 
});

In that code, there's a variable "obj" whose value is an empty object. It's OK in JavaScript to refer to a non-existent property of an object, and because such a reference results in undefinedthe test will pass. Another way to do it would be:

在该代码中,有一个变量“obj”,其值为空对象。在 JavaScript 中引用一个对象不存在的属性是可以的,因为这样的引用结果在undefined测试中会通过。另一种方法是:

it('should not be defined using .tobeUnefined()', function() {
  var no_value;

  expect(no_value).toBeUndefined();
});

回答by Philip Raath

Note that your error says that obj is not defined.

请注意,您的错误表明obj is not defined.

It does not say that obj is undefined.

它并没有说 obj 是undefined

undefinedis actually a data type in Javascript.

undefined实际上是 Javascript 中的一种数据类型。

Any declared variable without an assignment is given a value of undefinedby default, but that variable must be declared.

默认情况下,任何没有赋值的声明变量都会被赋予undefined值,但必须声明该变量。

So when Jasmine tests whether a value is undefined, it is determining if obj === undefined.

因此,当 Jasmine 测试一个值是否为undefined 时,它正在确定是否为obj === undefined

If you declare

如果你声明

var obj;

then objwill test as undefined.

然后obj将测试为未定义。

If you do not declare obj, then there is nothing for the Jasmine matcher to compare against the data type of undefined.

如果您不声明obj,那么 Jasmine 匹配器将无法undefined的数据类型进行比较。