Nightwatch模拟HTTP请求 [英] Nightwatch Mock HTTP Requests

查看:263
本文介绍了Nightwatch模拟HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用nock和其他库(例如sinonjs)来模拟HTTP请求,但没有成功.

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()
  }
}

是否可以用夜视器模拟HTTP请求?

Its possible mock http requests with nightwatch?

推荐答案

Nightwatch.js是一种端到端测试工具-因此,要点是,实际的UI以及它们调用的api都是实时的(而不是模拟的) 因此,也许您正在寻找为集成测试设计的框架,例如casper.js( http://casperjs.org/)或nightmare( https://github.com/segmentio/nightmare )

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)

我相信nightwatch中的模拟http调用应该可以用nock进行(如果您有一些奇怪的用例可以保证的话) 确保您没有在测试前进行HTTP调用(它们甚至可以位于before块中),例如:

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
  },
};

您的示例可能存在的问题是,您在模拟整个UI时-nightwatch可能会以某种方式阻止相同的域被拦截.在不同的域(或端口)上使用UI和API可能会解决问题

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

这篇关于Nightwatch模拟HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆