node.js js 是否应该无法读取 null 的属性“应该”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18102152/
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-09-02 15:13:13  来源:igfitidea点击:

Should js Cannot read property 'should' of null

node.jsshould.js

提问by zero_coding

i try to use the testing tool mocha in node. Consider the following test scenario

我尝试在 node.js 中使用测试工具 mocha。考虑以下测试场景

var requirejs = require('requirejs');

requirejs.config({
    //Pass the top-level main.js/index.js require
    //function to requirejs so that node modules
    //are loaded relative to the top-level JS file.
    nodeRequire: require
});


describe('Testing controller', function () {

    it('Should be pass', function (done) {
            (4).should.equal(4);
            done();
    });

    it('Should avoid name king', function (done) {
        requirejs(['../server/libs/validate_name'], function (validateName) {
            var err_test, accountExists_test, notAllow_test, available_test;
            validateName('anu', function (err, accountExists, notAllow, available) {
                accountExists.should.not.be.true;
                done();
            });

        });
    });

});  

as a testing result i have got:

作为测试结果,我得到了:

$ make test
./node_modules/.bin/mocha \
                --reporter list

  . Testing controller Should be pass: 0ms
  1) Testing controller Should avoid name anu

  1 passing (560 ms)
  1 failing

  1) Testing controller Should avoid name anu:
     Uncaught TypeError: Cannot read property 'should' of null
      at d:\townspeech\test\test.spec.js:23:30
      at d:\townspeech\server\libs\validate_name.js:31:20
      at d:\townspeech\test\test.spec.js:22:13
      at Object.context.execCb (d:\townspeech\node_modules\requirejs\bin\r.js:1869:33)
      at Object.Module.check (d:\townspeech\node_modules\requirejs\bin\r.js:1105:51)
      at Object.Module.enable (d:\townspeech\node_modules\requirejs\bin\r.js:1376:22)
      at Object.Module.init (d:\townspeech\node_modules\requirejs\bin\r.js:1013:26)
      at null._onTimeout (d:\townspeech\node_modules\requirejs\bin\r.js:1646:36)
      at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)



make: *** [test] Error 1

The first pass without any complication but the second, it seems like, that could not attach module shouldjs. Why?

第一遍没有任何复杂性,但第二遍似乎无法附加模块 shouldjs。为什么?

采纳答案by Golo Roden

That's a known problem with the shouldlibrary: As it extends object, of course it only works when you have a concrete object. As null, by definition, means that you don't have an object, you can not call any methods on it or access any of its properties.

这是should库的一个已知问题:当它扩展时object,当然只有当你有一个具体的对象时它才有效。因为null,根据定义,意味着您没有对象,您不能对其调用任何方法或访问其任何属性。

Hence, shouldis not available in this case.

因此,should在这种情况下不可用。

Basically, you have two options of how to deal with this:

基本上,您有两种处理方法:

  1. You could exchange actualand expected. This way, as long as you do not expect null, you have an object in the beginning and hence can access its shouldproperty. But, this is not nice, as it changes semantics, and does not work in all cases.
  2. You could exchange the shouldlibrary by another assertion library that does not have this problem.
  1. 你可以交换actualexpected。这样,只要您不期望null,您在开始时就有一个对象,因此可以访问其should属性。但是,这并不好,因为它改变了语义,并且不适用于所有情况。
  2. 您可以should通过另一个没有此问题的断言库来交换库。

Personally, I'd go with option 2, and my highly subjective personal favorite for this is node-assertthat(which has been written by me, hence it's my favorite).

就我个人而言,我会选择选项 2,而我对此高度主观的个人最喜欢的是node-assertthat(这是我写的,因此它是我的最爱)。

Anyway, there are lots of other options, such as expect. Feel free to use any of these assertion libraries that best suits your style of writing tests.

无论如何,还有很多其他选项,例如expect。随意使用任何最适合您编写测试风格的断言库。

回答by Henry Tao

I had the same problem. I solved it by using:

我有同样的问题。我通过使用解决了它:

(err === null).should.be.true;

(err === null).should.be.true;

回答by agebrock

You can use should directly

你可以直接使用 should

should.not.exist(err);

回答by Mbrevda

One more option that I don't see mentioned here:

我没有看到这里提到的另一种选择:

should(err === null).be.null

In you example that would would be:

在你的例子中,这将是:

should(accountExists).be.null

回答by Jeff C

I always find expectworks better with nulland undefinedas follows:

我总是发现expect使用null和效果更好,undefined如下所示:

expect(err).be.null

expect(err).be.null