gpt4 book ai didi

node.js - js应该不能读取null的属性 'should'

转载 作者:IT老高 更新时间:2023-10-28 21:57:45 26 4
gpt4 key购买 nike

我尝试在 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。为什么?

最佳答案

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

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

关于node.js - js应该不能读取null的属性 'should',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18102152/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com