atecarlos / protractor-http-mock

A library designed to work alongside Protractor for setting up mocks for your HTTP ajax requests.
MIT License
173 stars 70 forks source link

Module 'httpMock' is not available #71

Closed cayblood closed 8 years ago

cayblood commented 8 years ago

I'm trying to get a very basic mock setup working but it seems to be skipping the http mock altogether. The browser request executes normally and I get an error when selenium is not running. When I call mock.requestsMade() just to try to see what is happening, I get a Module 'httpMock' is not available error. Any help you can provide would be greatly appreciated.

Config:

require('babel-core/register')({ presets: ['es2015'] });

exports.config = {
  capabilities: {
    browserName: 'chrome'
  },
  specs: ['test/e2e/browser.js'],
  baseUrl: 'http://localhost:8080',
  frameworks: ['mocha', 'chai'],
  mocks: {
    default: ['default'],
    dir: 'test/e2e/http-mocks'
  },
  onPrepare: () => {
    browser.ignoreSynchronization = true;
    require('protractor-http-mock').config = {
      rootDirectory: __dirname,
      protractorConfig: 'protractor.conf.js'
    };
  }
};

Test:

import 'mocha';
const { browser, describe, beforeEach, afterEach, it, $ } = global;
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import { expect } from 'chai';
import mock from 'protractor-http-mock';

chai.use(chaiAsPromised);

describe('root route', () => {
  const defaultUrl = '/review/842b0a5f67ff880444737abc2b245517';

  beforeEach(() => {
    mock([{
      request: {
        path: defaultUrl,
        method: 'GET'
      },
      response: {
        data: {
          test: 'hello'
        }
      }
    }]);
  });

  afterEach(() => {
    mock.teardown();
  });

  describe('as a non-android, non-ios device', () => {
    it('should go to main page', () => {
      browser.get(defaultUrl);
      expect(mock.requestsMade()).to.eventually.equal([]);
      expect($('body').getText()).to.eventually.match(/Would you recommend/);
    });
  });
});

Result:

    Failed: javascript error: [$injector:nomod] Module 'httpMock' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
    http://errors.angularjs.org/1.5.0/$injector/nomod?p0=httpMock
    JavaScript stack:
    Error: [$injector:nomod] Module 'httpMock' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
    http://errors.angularjs.org/1.5.0/$injector/nomod?p0=httpMock
        at http://localhost:8080/vendor.js:32:381
        at http://localhost:8080/vendor.js:32:10599
        at t (http://localhost:8080/vendor.js:32:10072)
        at Object.module (http://localhost:8080/vendor.js:32:10384)
        at eval (eval at executeAsyncScript (unknown source), <anonymous>:3:26)
        at eval (eval at executeAsyncScript (unknown source), <anonymous>:6:5)
        at eval (eval at executeAsyncScript (unknown source), <anonymous>:6:31)
        at executeAsyncScript (<anonymous>:321:26)
        at <anonymous>:337:29
        at callFunction (<anonymous>:229:33)
      (Session info: chrome=48.0.2564.103)
      (Driver info: chromedriver=2.20.353124 (035346203162d32c80f1dce587c8154a1efa0c3b),platform=Mac OS X 10.11.3 x86_64) (WARNING: The server did not provide any stacktrace information)
    Command duration or timeout: 11 milliseconds
    Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:59:12'
    System info: host: 'Carls-MBP', ip: '192.168.1.74', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.11.3', java.version: '1.8.0_66'
    Driver info: org.openqa.selenium.chrome.ChromeDriver
    Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, chrome={userDataDir=/var/folders/rd/pfxg2wp96hb3z2xlw9p_w5s80000gn/T/.org.chromium.Chromium.9DNDBF}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=48.0.2564.103, platform=MAC, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
    Session ID: 516d7da26de522850404096b8971e032
Crevil commented 8 years ago

Du you register the mock module in AngularJS ?

cayblood commented 8 years ago

Do I need to? The docs say that no changes to your angular app are needed.

Crevil commented 8 years ago

No, that was my point. The injection error is from Angular, so I suspected this to be the case. Can you supply the complete code base to study? I'd gladly help.

cayblood commented 8 years ago

It's company code so I can't make it public, but I will try to reproduce the problem with a simple example and get back to you.

cayblood commented 8 years ago

It occurs to me that I'm trying to mock out not just Ajax calls but the request for the app's main index page. I suppose this might be out of the scope of this project, right?

Crevil commented 8 years ago

It doesn't sound right, to me at least. What is the purpose of this?

cayblood commented 8 years ago

Thanks. I was trying to mock the initial page load, which doesn't make sense. Sorry for the trouble. I will try this out when I'm ready to test other ajax calls that are made after the app loads.