问题
i try to use the testing tool mocha in node. Consider the following test scenario
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?
回答1:
That's a known problem with the should
library: 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.
Hence, should
is not available in this case.
Basically, you have two options of how to deal with this:
- You could exchange
actual
andexpected
. This way, as long as you do not expectnull
, you have an object in the beginning and hence can access itsshould
property. But, this is not nice, as it changes semantics, and does not work in all cases. - You could exchange the
should
library by another assertion library that does not have this problem.
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).
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.
回答2:
I had the same problem. I solved it by using:
(err === null).should.be.true;
回答3:
You can use should directly
should.not.exist(err);
回答4:
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
回答5:
I always find expect
works better with null
and undefined
as follows:
expect(err).be.null
来源:https://stackoverflow.com/questions/18102152/should-js-cannot-read-property-should-of-null