2014-01-30 50 views
0

我已經設置了一個類似下面的gruntfile。目標是使用量角器爲我的angularjs項目執行e2e測試。當使用mochaProtractor運行這個時,Chrome會按預期啓動,但lint告訴我,我錯過了expect語句的依賴關係。這應該引用斷言庫chai。如何包含chai的依賴關係以使其正常工作?用mochaProtractor指定依賴關係運行grunt?柴斷言庫?

感謝

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!'); 
     }); 


}); 

回答

0

您必須在您的package.json中添加chai作爲開發依賴項。

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

然後通過

npm install` 

安裝dependenvy,然後你應該能夠編寫一個規範:

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!'); 
     }); 


});