模块' httpMock'不可用

时间:2015-09-07 07:46:41

标签: javascript angularjs automation jasmine protractor

这是量角器测试使用量角器http mock的错误消息:

  

JavascriptError:javascript错误:[$ injector:nomod]模块' httpMock'   不可用!你让她拼错了模块名称或忘了   加载它。如果注册模块,请确保指定   依赖作为第二个参数。

conf.js:

// An example configuration file.
exports.config = {
  directConnect: true,
    // Selenium server
  SeleniumAddress: 'http://localhost:4444/wd/hub',
  // Capabilities to be passed to the webdriver instance.
  capabilities: {
    'browserName': 'chrome'
  },
    //baseUrl: 'http://develop.garbo.livebranches.com/sv-SE/',
    //Framework to use. Jasmine 2 is recommended.
    framework: 'jasmine2',
    //frameworks: ['mocha', 'jasmine'],

    // Spec patterns are relative to the current working directly when
    // protractor is called.
    //specs: ['testmain.js','testlogin.js'],
    //specs: ['testmain.js','testteaPartyList.js','testpositionSearchIndex.js','testpositionList.js'],
    specs: ['testlogin.js'],

    //Options to be passed to Jasmine.
  jasmineNodeOpts: {
    defaultTimeoutInterval: 250000
  },
  mocks: {
        dir: '../node_modules/protractor-http-mock',
        //dir: 'mocks',
        default: []
    },
  //=====login begin =====
 onPrepare: function() {
    require("protractor-http-mock").config = {
    rootDirectory: '../node_modules/protractor-http-mock/lib',
    //rootDirectory: __dirname,

    protractorConfig: "conf.js",  // name of the config here
    };

  }

  //=====login end========


};

testlogin.js

describe('angularjs homepage', function() {

    //browser.ignoreSynchronization = true;
  it('should login', function() {
    var mock = require('protractor-http-mock');
    var todoList;

    beforeEach(function() {

        var url ='http://dev.etest.com:285/Actor/tbUsers/LoginAndGet';

        var req = {Mobile:'14500000006',Password:'123456'};

        var rep = {UserId:164,AccountId:328,Token:'328:dc91d536ab424aa0b8d7f1ecaf64c55b',Id:328};

        mock([{
        request: {
          path: url,
          method: 'POST',
          data:req,
        },
        response: {
          data: rep,
        }
        }]);
    });
    afterEach(function() {
        mock.teardown();
    });

    browser.get('http://localhost:2024/daNiuJob/www/ionicWeb/index.html#/login');

    console.log('mock='+mock);

    element(by.model('data.userName')).sendKeys('14500000006');
    element(by.model('data.password')).sendKeys('123456');
    var btnlogin = element(by.id('Regist')).element(by.tagName('a'));   

    expect(browser.getTitle()).toEqual('userlogin');

        browser.getTitle().then(function(text){

        console.log('title='+text);
    });

    //cause mock error
    expect(mock.requestsMade()).toEqual([
        { url : 'http://dev.etest.com:285/Actor/tbUsers/LoginAndGet', method : 'GET' },
    ]);

    btnlogin.click();


    browser.sleep(8000);

    });


});

为什么找不到httpMock,谢谢!

请注意:

C:\Users\HQ-XXX\AppData\Roaming\npm\node_modules\protractor\node_modules\protractor-http-mock

这是' protractor-http-mock'

的路径

2 个答案:

答案 0 :(得分:1)

你应该给出http-mock模块文件夹的路径,而不是它里面的lib文件夹。将protractor-http-mock函数内的onPrepare()的rootDirectory路径更改为 -

rootDirectory: 'C:\Users\HQ-XXX\AppData\Roaming\npm\node_modules\protractor\node_modules\protrac‌​tor-http-mock ',

如果您需要提供相对路径,请按以下方式更改 -

rootDirectory: '..\node_modules\protrac‌​tor-http-mock ',

希望这有帮助。

答案 1 :(得分:0)

我们遇到了同样的问题,它与每个规范开头的页面重新加载有关。

这是由于html5mode和browser.get的错误配置引起的,所以它在foo.bar/开头重定向到foo.bar/#/,卸载了所有注入的量角器代码。