使用mocha Protractor运行grunt指定依赖项?柴断言图书馆?

时间:2014-01-30 23:00:55

标签: gruntjs mocha specifications protractor

我已经设置了一个如下所示的gruntfile。目的是使用量角器为我的angularjs项目执行e2e测试。当使用mochaProtractor运行时,Chrome会按预期启动,但是lint告诉我,我错过了expect语句的依赖项。这应该引用断言库chai。如何为chai包含依赖项以使其正常工作?

由于

module.exports = function(grunt) {

  // Project configuration.
  grunt.initConfig({
    watch: {
      scripts: {
        files: ['public/specs/e2e/*.js'],
        tasks: ['mochaProtractor','jshint'],
        options: {
          spawn: false,
        },
      },
    },
    jshint: {
      all: [
        'Gruntfile.js',
        'public/app/js/*.js'
      ],
      options: {
        curly: true,
        eqeqeq: true,
        immed: true,
        latedef: true,
        newcap: true,
        noarg: true,
        sub: true,
        undef: true,
        unused: true,
        boss: true,
        eqnull: true,
        node: true
      }
    },
    mochaProtractor: {
      options: {
        browsers: ['Chrome']
      },
      files: ['public/specs/e2e/*.js'],
      baseUrl: 'http://localhost:3000/'
    },
  });

  // These plugins provide necessary tasks.
  grunt.loadNpmTasks('grunt-contrib-jshint');

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

  grunt.loadNpmTasks('grunt-mocha-protractor');

以下是我试图测试的规范。注意:我在顶部添加了require语句,试图让它工作。有什么想法吗?

var chai = require('chai');

var expect = chai.expect;

describe("Given a task entry screen", function() {
        ptor = protractor.getInstance();

      beforeEach(function() {
        ptor.get('#/');
        button = ptor.findElement(protractor.By.className('btn-say-hello'));
        button.click();
      });

      it('says hello', function() {
        message = ptor.findElement(protractor.By.className('message'));
        expect(message.getText()).toEqual('Hello!');
      });


});

1 个答案:

答案 0 :(得分:0)

您必须在package.json中添加chai作为开发依赖项。

{
  "name": "yourProject",
  "devDependencies": {
    "chai": "~1.8.1"
  }
}

然后通过

安装dependdenvy
npm install`

然后你应该能够写一个规范:

var chai = require('chai');

var expect = chai.expect;

describe("Given a task entry screen", function() {
        ptor = protractor.getInstance();

      beforeEach(function() {
        ptor.get('#/');
        button = ptor.findElement(protractor.By.className('btn-say-hello'));
        button.click();
      });

      it('says hello', function() {
        message = ptor.findElement(protractor.By.className('message'));
        expect(message.getText()).toEqual('Hello!');
      });


});