mdasberg / ng-apimock

Node plugin that provides the ability to use scenario based api mocking: for local development for protractor testing
MIT License
99 stars 26 forks source link

Getting 404/ requests not being intercepted #63

Closed julissamackey closed 5 years ago

julissamackey commented 5 years ago

Using Angular 4.3.4 & Protractor 5.4.1 Below are my configurations and .json file It seems I must be missing a step or doing something because I am getting 404, the console does output this however

Process all the mocks
Register mocks
Generate the mocking web interface
Generate protractor.mock.js

protractor.config:

exports.config = {
  allScriptsTimeout: 30000,
  specs: [
    './e2e/**/*.e2e-spec.ts'
  ],
  capabilities:{
        'browserName': 'chrome',    
    },
  directConnect: true,
  framework: 'jasmine',
  jasmineNodeOpts: {
    showColors: true,
    defaultTimeoutInterval: 60000,
    print: function() {}
  },
  onPrepare() {
    require('ts-node').register({
      project: 'e2e/tsconfig.json'
    });

    global.ngApimock = require('./.tmp/mocks/protractor.mock');

    jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
  },
  ngApimockOpts: {
    angularVersion: 4,  // {number} provide major version of Angular
    hybrid: false // optional boolean which can be used for testing Angular apps within an AngularJs app.
  },
  specReporter: {
    maxLogLines: 5,  // limit number of lines logged per test
    suppressErrorSummary: false,  // do not print error summary
    suppressFailed: false,  // do not print information about failed tests
    suppressPassed: false,  // do not print information about passed tests
    suppressSkipped: true,  // do not print information about skipped tests
    showSpecTiming: false // print the time elapsed for each spec
  }
};

mock

{
    "expression": "api/env",
    "method": "GET",
    "name": "environmentService",
    "isArray": false,
    "responses": {
      "defaultResponse": {
        "default": true, 
        "status": 200, 
        "headers": {}, 
        "data": {
            "success": true
            }
        },
        "file": "",
        "statusText": ""
      }
    }
  }

in my test:

const bodyParser = require('body-parser'),
    express = require('express'),
    cors = require('cors'),
    ngApiMock = require('ng-apimock')();

describe('Test Page', ()=>{
    let page: TestPage;
    const app = express();
            app.use(bodyParser.json());
            app.use(bodyParser.urlencoded({ extended: false }));
            app.use(cors());
            app.use(require('ng-apimock/lib/utils').ngApimockRequest);
            ngApiMock.run({
        "baseUrl": browser.baseUrl,
        "src":"./mocks"
    });

I am getting response

ok: false
status: 404
statusText: "Not Found"
type: 2
url: "http://localhost:49153/api/env"
Local9 commented 5 years ago

Well not alone on the 404 front: https://github.com/mdasberg/ng-apimock/issues/40#issuecomment-453744313

mdasberg commented 5 years ago

@julissamackey your mock json was incorrect.

{
  "expression": "api/env",
  "method": "GET",
  "name": "environmentService",
  "isArray": false,
  "responses": {
    "defaultResponse": {
      "default": true,
      "status": 200,
      "headers": {},
      "data": {
        "success": true
      }
    },
    "file": "",
    "statusText": ""
  }
}

You had a } at the end that did not belong there. This works for me. Can you verify?

mdasberg commented 5 years ago

@Local9 and @julissamackey I have created a pr that upgrades to the latest version of ng-apimock

The problem was that you selected a scenario before you browsed to the page. What happens then is that you have no ngapimockid in your request to the backend. In the new version this has been fixed.

Local9 commented 5 years ago

@mdasberg thank you very much, this is all working now. I've updated with your PR and all my santity test worked. I didn't think, due to it being in protractor, about the events process that the site had to be loaded before calling on the API Mock. This new change will now allow setups to be called before landing pages are accessed.

julissamackey commented 5 years ago

thank you!