Cal*_*ter 5 javascript unit-testing node.js jasmine
我有一个简单的“ hello world”项目,我想测试著名的hélloWorld函数。
该项目的结构如下:
??? package.json
??? spec
? ??? helloWorldSpec.js
? ??? support
? ??? jasmine.json
??? src
??? helloWorld.js
Run Code Online (Sandbox Code Playgroud)
以及文件内容:
package.json
{
"name": "jasmineTest",
"version": "0.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "BSD-2-Clause",
"dependencies": {
"jasmine": "~2.1.0"
}
}
Run Code Online (Sandbox Code Playgroud)
spec / helloWorldSpec.js
// var helloWorld = require('../src/helloWorld.js');
describe('Test', function() {
it('it', function() {
helloWorld();
});
});
Run Code Online (Sandbox Code Playgroud)
src / helloWorld.js
function helloWorld() {
return "Hello world!";
}
// module.exports = helloWorld;
Run Code Online (Sandbox Code Playgroud)
spec / support / jasmine.json
{
"spec_dir": "spec",
"spec_files": [
"**/*[sS]pec.js"
],
"helpers": [
"helpers/**/*.js"
]
}
Run Code Online (Sandbox Code Playgroud)
我的问题:
当我运行npm install茉莉花被下载。
=>好
运行时./node_modules/jasmine/bin/jasmine.js
出现错误ReferenceError: helloWorld is not defined ReferenceError: helloWorld is not defined
我的问题:
如何src/helloWorld.js在不使用module.exports = xxx的情况下访问测试范围中包含的helloWord方法。
解决方案是使用Grunt。
\n\n创建一个包含以下内容的GruntFile.js:
\n\nmodule.exports = function (grunt) {\n grunt.initConfig({\n pkg: grunt.file.readJSON(\'package.json\'),\n jasmine: {\n src: [\'src/**/*.js\'],\n options: {\n specs: [\'spec/**/*Spec.js\'],\n vendor: []\n }\n }\n });\n grunt.loadNpmTasks(\'grunt-contrib-jasmine\');\n};\nRun Code Online (Sandbox Code Playgroud)\n\n使用grunt、grunt-cli和grunt-contrib-jasmine依赖项更新package.json
\n\n{\n "name": "jasmineTest",\n "version": "0.0.0",\n "description": "",\n "main": "index.js",\n "scripts": {\n "test": "echo \\"Error: no test specified\\" && exit 1"\n },\n "author": "",\n "license": "BSD-2-Clause",\n "dependencies": {\n "jasmine": "~2.1.0",\n "grunt": "~0.4.5",\n "grunt-cli": "~0.1.13",\n "grunt-contrib-jasmine": "~0.8.1"\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n更新 npm 依赖项:
\n\nnpm update\nRun Code Online (Sandbox Code Playgroud)\n\n并使用 grunt 而不是直接 jasmine 重新启动测试:
\n\n./node_modules/grunt-cli/bin/grunt jasmine\nRun Code Online (Sandbox Code Playgroud)\n\n你得到了:
\n\nRunning "jasmine:src" (jasmine) task\nTesting jasmine specs via PhantomJS\n\n Test\n - it...\nlog: Spec \'Test it\' has no expectations.\n \xe2\x9c\x93 it\n\n1 spec in 0.008s.\n>> 0 failures\nRun Code Online (Sandbox Code Playgroud)\n\n完成,没有错误。
\n| 归档时间: |
|
| 查看次数: |
2259 次 |
| 最近记录: |