我正在尝试为使用node.js
框架编写的服务器端逻辑编写一些单元测试。我想使用jasmine
库进行单元测试和模拟,因为我对它有点熟悉。
现在,我尝试了类似这样的模拟文件系统read
函数:
'use strict';
var loginRedirectMiddleware = require('../server/loginRedirectMiddleware');
var fs = require('fs');
var util = require('../server/util');
describe('loginRedirectMiddleware', function(){
spyOn(fs,'read');
...
});
现在我在使用spyOn
调用的行上收到错误:
1) loginRedirectMiddleware encountered a declaration exception
Message:
TypeError: Cannot call method 'spyOn' of null
Stacktrace:
TypeError: Cannot call method 'spyOn' of null
at null.<anonymous> (/home/adebksi/Nauka/js-linkedin-connector/server_test/loginRedirectMiddlewareSpec.js:11:3)
at Object.<anonymous> (/home/adebksi/Nauka/js-linkedin-connector/server_test/loginRedirectMiddlewareSpec.js:10:1)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (/home/adebksi/Nauka/js-linkedin-connector/node_modules/karma/node_modules/coffee-script/lib/coffee-script/coffee-script.js:211:36)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.jasmine.executeSpecsInFolder (/home/adebksi/Nauka/js-linkedin-connector/node_modules/grunt-jasmine-node/node_modules/jasmine-node/lib/jasmine-node/index.js:151:9)
at Object.<anonymous> (/home/adebksi/Nauka/js-linkedin-connector/node_modules/grunt-jasmine-node/tasks/jasmine-node-task.js:110:19)
at Object.thisTask.fn (/home/adebksi/Nauka/js-linkedin-connector/node_modules/grunt/lib/grunt/task.js:78:16)
at Object.<anonymous> (/home/adebksi/Nauka/js-linkedin-connector/node_modules/grunt/lib/util/task.js:282:30)
at Task.runTaskFn (/home/adebksi/Nauka/js-linkedin-connector/node_modules/grunt/lib/util/task.js:235:24)
at Task.<anonymous> (/home/adebksi/Nauka/js-linkedin-connector/node_modules/grunt/lib/util/task.js:281:12)
at Task.start (/home/adebksi/Nauka/js-linkedin-connector/node_modules/grunt/lib/util/task.js:290:5)
at Object.grunt.tasks (/home/adebksi/Nauka/js-linkedin-connector/node_modules/grunt/lib/grunt.js:155:8)
at Object.module.exports [as cli] (/home/adebksi/Nauka/js-linkedin-connector/node_modules/grunt/lib/grunt/cli.js:38:9)
at Object.<anonymous> (/home/adebksi/.nvm/v0.10.15/lib/node_modules/grunt-cli/bin/grunt:41:20)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
我想知道我做错了什么?
当我console.log(typeof fs.read)
时,我得到function
所以此属性是一个函数。
答案 0 :(得分:3)
好的,事实证明这是我的错,我试图在describe
函数中创建间谍,我应该把它放在it
中。