node.js - Should js Cannot read property 'should' of null -
i try use testing tool mocha in node. consider following test scenario
var requirejs = require('requirejs'); requirejs.config({ //pass top-level main.js/index.js require //function requirejs node modules //are loaded relative top-level js file. noderequire: require }); describe('testing controller', function () { it('should 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 testing result have got:
$ make test ./node_modules/.bin/mocha \ --reporter list . testing controller should 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 @ d:\townspeech\test\test.spec.js:23:30 @ d:\townspeech\server\libs\validate_name.js:31:20 @ d:\townspeech\test\test.spec.js:22:13 @ object.context.execcb (d:\townspeech\node_modules\requirejs\bin\r.js:1869:33) @ object.module.check (d:\townspeech\node_modules\requirejs\bin\r.js:1105:51) @ object.module.enable (d:\townspeech\node_modules\requirejs\bin\r.js:1376:22) @ object.module.init (d:\townspeech\node_modules\requirejs\bin\r.js:1013:26) @ null._ontimeout (d:\townspeech\node_modules\requirejs\bin\r.js:1646:36) @ timer.listontimeout [as ontimeout] (timers.js:110:15) make: *** [test] error 1
the first pass without complication second, seems like, not attach module shouldjs. why?
that's known problem should
library: extends object
, of course works when have concrete object. null
, definition, means don't have object, can not call methods on or access of properties.
hence, should
not available in case.
basically, have 2 options of how deal this:
- you exchange
actual
,expected
. way, long not expectnull
, have object in beginning , hence can accessshould
property. but, not nice, changes semantics, , not work in cases. - you exchange
should
library assertion library not have problem.
personally, i'd go option 2, , highly subjective personal favorite node-assertthat (which has been written me, hence it's favorite).
anyway, there lots of other options, such expect. feel free use of these assertion libraries best suits style of writing tests.
Comments
Post a Comment