本文介绍了Nock不适用于Nightwatch + Selenium的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用nock模拟我的http请求.

I am using nock to mock my http requests.

我的应用是使用Redux + React编写的.

My app is written using Redux + React.

import nock from "nock"

nock("http://localhost:8080")
  .log(console.log)
  .get("/fake/users/sign_in.json")
  .reply(500, 'Error')

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

export default {
  "Do login and receive success message": (browser) => {
    doLogin(browser)
      .assert.urlContains('panel')
      .end()
  }
}

当请求返回错误时,500无法通过测试,但当前已通过.因此,我认为nock并不是在嘲笑请求.

When the request returns error 500 can not pass the test, but currently it passes. So I think that nock is not mocking the requests.

推荐答案

您必须使用

...
.execute(`var importScript = (function (oHead) {

  function loadError (oError) {
    throw new URIError("The script " + oError.target.src + " is not accessible.");
  }

  return function (sSrc, fOnload) {
    var oScript = document.createElement("script");
    oScript.type = "text\/javascript";
    oScript.onerror = loadError;
    if (fOnload) { oScript.onload = fOnload; }
    oHead.appendChild(oScript);
    oScript.src = sSrc;
  }

    })(document.head || document.getElementsByTagName("head")[0]);
     importScript('http://somewhere.com/nock.js')`)

这篇关于Nock不适用于Nightwatch + Selenium的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 20:06