Nightwatch Mock HTTP Requests

天涯浪子 提交于 2019-12-04 12:21:04

问题


I try mocking HTTP requests with nock and other libs like sinonjs but without success.

import nock from "nock"

const URL = "http://localhost:8080/"

const SIGN_IN_PATH = "/fake/users/sign_in.json"

export const signInRequest = (status, payload = {}) => {
  return nock(URL).get(SIGN_IN_PATH).reply(status, payload)
}

-

import { signInRequest } from "./../../utils/fakeRequests"

const doLogin = (browser) => {
  return browser
          .url("http://localhost:8080")
          .waitForElementVisible('form', 1000)
          .setValue('input[name=email]', 'foo@foo.com')
          .setValue('input[name=password]', 'somepass')
          .click('button[type=submit]')
          .pause(500)
}

export default {
  "Do login and shows error message": (browser) => {
    signInRequest(403)

    doLogin(browser)
      .waitForElementVisible('.error', 1000)
      .end()
  }
}

Its possible mock http requests with nightwatch?


回答1:


Nightwatch.js is an end to end testing tool - so the point is that actual UIs as well as the api they call will be live (and not mocked) So maybe you are looking for a framework designed for integration tests like casper.js (http://casperjs.org/) or nightmare (https://github.com/segmentio/nightmare)

However mocking http calls in nightwatch should be doable with nock i believe (if you have some strange use case that warrants it) Ensure that you're nock http calls are before tests (they can even be in a before block), eg:

module.exports = {
  'test abc' : function (browser) {
    nock('http://example.com')
      .get('/users')
      .query({name: 'martin'})
      .reply(200, {results: [{id: '123'}]});

    // do test stuff
  },
};

Possible problem with your example is that your mocking the entire UI - nightwatch might be somehow preventing the same domain from being intercepted. Having your UI and API on different domains (or ports) might solve the issue



来源:https://stackoverflow.com/questions/38404013/nightwatch-mock-http-requests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!