无法在grunt jasmine测试中使用require

时间:2016-12-07 19:25:10

标签: javascript node.js gruntjs jasmine

我试图在GRUNT框架中运行非常简单的茉莉花测试。此测试已使用jasmine_node成功,但在尝试在GRUNT

下运行时失败

Gruntfile.js:

'use strict';

module.exports = function(grunt) {

// Project configuration.
grunt.initConfig({
// Metadata.
pkg: grunt.file.readJSON('package.json'),
banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' +
  '<%= grunt.template.today("yyyy-mm-dd") %>\n' +
  '<%= pkg.homepage ? "* " + pkg.homepage + "\\n" : "" %>' +
  '* Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author.name %>;' +
  ' Licensed <%= _.pluck(pkg.licenses, "type").join(", ") %> */\n',
// Task configuration.

jasmine: {
    pivotal: {
        src: 'app/*.js',
        options: {
            specs: 'spec/helloWorldSpec.js',
            summary: true,
            template: require('grunt-template-jasmine-requirejs')

        }
    }
}
});

grunt.loadNpmTasks('grunt-contrib-jasmine');

grunt.registerTask('test', ['jasmine']);
grunt.registerTask('default', ['jasmine']);

};

package.json文件:

{
  "name": "autodemo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
  "test": "grunt test"
},
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.14.0",
    "jasmine-node": "^1.14.5",
    "request": "^2.79.0"
  }
}

规格文件夹下的helloWorldSpec:

console.log('test started');

var request = require('request');

var base_url = "http://192.168.2.122:3000/"

describe("Hello World Server", function() {
  describe("GET /", function() {

it("returns status code 200", function(done) {
  request.get(base_url, function(error, response, body) {
    expect(response.statusCode).toBe(200);
    done();
  });
});

it("returns Hello World", function(done) {
  request.get(base_url, function(error, response, body) {
    expect(body).toBe("Hello World");
    done();
  });
});

 });

});

我得到的错误:

C:\Users\motip\Desktop\gruntDemo>grunt jasmine
Running "jasmine:js" (jasmine) task
Testing jasmine specs via PhantomJS

>> Error: notloaded: Module name "request" has not been loaded yet for context:_. Use require([])
>> http://requirejs.org/docs/errors.html#notloaded at
>> ..\..\..\..\C:\Users\motip\Desktop\gruntDemo\_SpecRunner.html:38 onError
>> ..\..\..\..\C:\Users\motip\Desktop\gruntDemo\.grunt\grunt-contrib-jasmine\require.js:12 v
>> ..\..\..\..\C:\Users\motip\Desktop\gruntDemo\.grunt\grunt-contrib-jasmine\require.js:26 h
>> ..\..\..\..\C:\Users\motip\Desktop\gruntDemo\.grunt\grunt-contrib-jasmine\require.js:31 requirejs
Warning: No specs executed, is there a configuration error? Use --force to continue.

Aborted due to warnings.

我转到http://requirejs.org/docs/errors.html#notloaded并交换代码并将所有内容放在块下:     要求([&#39;请求&#39;],功能(请求){         // foo现在已加载。     });

但似乎代码在要求中失败而未到达描述:

C:\Users\motip\Desktop\gruntDemo>grunt jasmine
Running "jasmine:pivotal" (jasmine) task
Testing jasmine specs via PhantomJS


log: test started
Warning: No specs executed, is there a configuration error? Use --force to continue.

Aborted due to warnings.

由于

0 个答案:

没有答案