【问题标题】:Should js Cannot read property 'should' of nulljs 是否应该无法读取 null 的属性“应该”
【发布时间】:2013-08-08 18:09:43
【问题描述】:

我尝试在 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();
            });

        });
    });

});  

作为我得到的测试结果:

$ 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

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

【问题讨论】:

  • 是 cus accountExists 为 null,并且 null 上没有任何属性。你也可以使用expect 吗?如果是这样,expect(accountExists).to.not.equal(true) 会起作用。
  • 你也可以使用 should.not.exist(accountExists);

标签: node.js should.js


【解决方案1】:

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

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

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

  1. 您可以交换actualexpected。这样,只要您不期望null,您在开头就有一个对象,因此可以访问它的should 属性。但是,这并不好,因为它会改变语义,并且并非在所有情况下都有效。
  2. 您可以将should 库替换为另一个没有此问题的断言库。

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

不管怎样,还有很多其他的选择,比如expect。随意使用任何最适合您编写测试风格的断言库。

【讨论】:

  • 您可以使用 shuold 通过 should.exists(object) 测试空对象
  • 好电话,@zaboco。 should.exist(object) 是正确的调用方法。我实际上无法编辑我的评论想法:(
  • 你不是说should.not.exist(possibleNullVariable),因为你正在测试null吗?
【解决方案2】:

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

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

【讨论】:

  • 喜欢)我也用(!res).should.be.ok();
  • 在 8.2.2 版本中,true 是一个函数,必须像 (err === null).should.be.true() 一样才能工作
  • 或者使用(err === null).should.equal(true)会更好,因为true实际上不是动词
  • 这对我不起作用。即使 err 是非空字符串,它也始终返回 true。但是,(err === null).should.equal(true) 按预期工作
【解决方案3】:

你可以直接使用 should

should.not.exist(err);

【讨论】:

  • should.not.exist(err); Uncaught TypeError: Object # has no method 'exist'
  • 你需要使用const should = chai.should();来定义should
  • 这直接来自 ShouldJS 文档:shouldjs.github.io/#should-exist
【解决方案4】:

这里没有提到的另一个选项:

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

在你的例子中,这将是:

should(accountExists).be.null

【讨论】:

  • 未捕获的类型错误:对象# 的属性“应该”不是函数
  • @FelikZ 您必须显示一些代码才能为该错误提供一些上下文。你还记得 require() 应该吗?
  • 是的,应该是必需的。该错误仅与 null 和未定义的变量一起出现。其他应该按预期工作。我的工作解决方案是帖子:stackoverflow.com/a/19994851/734493
【解决方案5】:

我总是发现expectnullundefined 配合使用效果更好,如下所示:

expect(err).be.null

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-25
    • 1970-01-01
    • 2022-08-14
    • 2021-03-16
    • 1970-01-01
    • 2017-02-21
    • 1970-01-01
    • 2019-09-05
    相关资源
    最近更新 更多