javascript - 模块 'httpMock' 不可用

标签 javascript angularjs automation jasmine protractor

这是使用 Protractor http 模拟进行 Protractor 测试时的错误消息:

JavascriptError: javascript error: [$injector:nomod] Module 'httpMock' is not available! You eit her misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

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”的路径

最佳答案

您应该提供 http-mock 模块文件夹的路径,而不是其中的 lib 文件夹。将 onPrepare() 函数内 protractor-http-mock 的 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 ',

希望这有帮助。

关于javascript - 模块 'httpMock' 不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32433546/

相关文章:

Ruby win32ole - 如何传递 VARIANT 参数?

testing - 质量检查和测试:跨区域方法

javascript - react 国家 -> 省/州 -> 城市选择器?

javascript - AngularJS - 当嵌套子 ngrepeat 为空时如何显示消息。

javascript - AngularJs:让方法在内部调用 $http 或 $resource 时同步返回

javascript - 使用 Angular 过滤 $http json 数据

iphone - 如何在 Cocoa for iPhone 中模拟鼠标点击 UIWebView?

javascript - 链接到从 SVG 生成的下载文件

javascript - 环回验证邮件 http ://https://example. api.com:port

javascript - HighCharts 饼图,50 多个标签,未显示全部