jack jack - 2 months ago 6
AngularJS Question

Module 'httpMock' is not available

This is error message on a protractor test use protractor http mock:


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);

});


});


Why can't find
httpMock
, thank!

note:

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


This is path of 'protractor-http-mock'

Answer

You should be giving the path of the http-mock module folder and not lib folder inside it. Change your rootDirectory path of protractor-http-mock inside onPrepare() function to -

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

If at all you need to provide a relative path then change it as below -

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

Hope this helps.