Jasmine js:为测试执行添加源方法 [英] Jasmine js: Add source method for test execution
问题描述
我有一个简单的hello world项目,我想测试着名的hélloWorld函数。
I have as simple "hello world" project and I want to test the famous hélloWorld function.
项目的结构如下:
├── package.json
├── spec
│ ├── helloWorldSpec.js
│ └── support
│ └── jasmine.json
└── src
└── helloWorld.js
文件内容:
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"
}
}
spec / helloWorldSpec.js
// var helloWorld = require('../src/helloWorld.js');
describe('Test', function() {
it('it', function() {
helloWorld();
});
});
src / helloWorld.js
function helloWorld() {
return "Hello world!";
}
// module.exports = helloWorld;
spec / support / jasmine.json
{
"spec_dir": "spec",
"spec_files": [
"**/*[sS]pec.js"
],
"helpers": [
"helpers/**/*.js"
]
}
我的问题:
My problem:
当我运行 npm install
jasmine已下载。
=> ok
When I run npm install
jasmine is downloaded.
=> ok
当我运行 ./ node_modules / jasmine / bin / jasmine.js
我有错误 ReferenceError:helloWorld is未定义ReferenceError:未定义helloWorld
我的问题:
My Question:
如何在不使用module.exports = xxx的情况下访问测试范围中 src / helloWorld.js
中包含的方法helloWord 。
How can I access the method helloWord contained in src/helloWorld.js
in the test scope without using module.exports = xxx.
推荐答案
解决方案是使用 Grunt 。
创建 Gr untFile.js 包含:
module.exports = function (grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
jasmine: {
src: ['src/**/*.js'],
options: {
specs: ['spec/**/*Spec.js'],
vendor: []
}
}
});
grunt.loadNpmTasks('grunt-contrib-jasmine');
};
使用 package.json ://gruntjs.com/rel =nofollow> grunt , grunt-cli 和 grunt-contrib-jasmine 依赖项
Update package.json with grunt, grunt-cli and grunt-contrib-jasmine dependencies
{
"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",
"grunt": "~0.4.5",
"grunt-cli": "~0.1.13",
"grunt-contrib-jasmine": "~0.8.1"
}
}
更新npm依赖项:
npm update
并使用grunt重启测试而不是直接茉莉花:
And relaunch test using grunt and not directly jasmine:
./node_modules/grunt-cli/bin/grunt jasmine
你得到了:
Running "jasmine:src" (jasmine) task
Testing jasmine specs via PhantomJS
Test
- it...
log: Spec 'Test it' has no expectations.
✓ it
1 spec in 0.008s.
>> 0 failures
完成,没有错误。
这篇关于Jasmine js:为测试执行添加源方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!