Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Jasmine js:为测试执行添加源方法_Javascript_Node.js_Unit Testing_Jasmine - Fatal编程技术网

Javascript Jasmine js:为测试执行添加源方法

Javascript Jasmine js:为测试执行添加源方法,javascript,node.js,unit-testing,jasmine,Javascript,Node.js,Unit Testing,Jasmine,我有一个简单的“hello world”项目,我想测试著名的hélloWorld函数 该项目的结构如下: ├── package.json ├── spec │   ├── helloWorldSpec.js │   └── support │   └── jasmine.json └── src └── helloWorld.js 以及文件内容: package.json { "name": "jasmineTest", "version": "0.0.0", "

我有一个简单的“hello world”项目,我想测试著名的hélloWorld函数

该项目的结构如下:

├── 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_dir": "spec",
  "spec_files": [
    "**/*[sS]pec.js"
  ],
  "helpers": [
    "helpers/**/*.js"
  ]
}
spec/helloWorldSpec.js

// var helloWorld = require('../src/helloWorld.js');
describe('Test', function() {
    it('it', function() {
        helloWorld();
    });
});
function helloWorld() {
    return "Hello world!";
}
// module.exports = helloWorld;
src/helloWorld.js

// var helloWorld = require('../src/helloWorld.js');
describe('Test', function() {
    it('it', function() {
        helloWorld();
    });
});
function helloWorld() {
    return "Hello world!";
}
// module.exports = helloWorld;
spec/support/jasmine.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_dir": "spec",
  "spec_files": [
    "**/*[sS]pec.js"
  ],
  "helpers": [
    "helpers/**/*.js"
  ]
}
我的问题:

当我运行
npm安装时
jasmine被下载。
=>好的

当我运行
/node\u modules/jasmine/bin/jasmine.js

我有一个错误
ReferenceError:helloWorld未定义ReferenceError:helloWorld未定义

我的问题:

如何在不使用module.exports=xxx的情况下访问测试范围中
src/helloWorld.js
中包含的方法helloWord。

创建包含以下内容的grunfile.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

{
  "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而不是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的工作人员说:

您不需要在配置中指定源文件,只需要从spec文件中指定它们即可

但是,您确实需要使用导出。 这不是Jasmine问题,而是香草Javascript。您想从另一个文件调用一个方法,因此需要导出并需要它。
你为什么不想呢

请分享你的Grunfile。js@EugeneKrevenets:missing Gruntile.js现在被共享了。奇怪的是,jasmine网站没有说明在哪里给出正在测试的代码的路径。