简体   繁体   English

如何使用nightwatch.js和sinon.js模拟API响应?

[英]How to mock the API response with nightwatch.js and sinon.js?

I'm writing e2e test for a single page application with nightwatch.js . 我正在写E2E测试与单页的应用程序nightwatch.js

I have some API request like an authentication. 我有一些API请求,如身份验证。 So I want to use fakeServer of sinon.js for mocking response data. 所以我想使用sinon.js的fakeServer来模拟响应数据。 Here's my code. 这是我的代码。

import sinon from 'sinon';

const WAIT_TIME = 5000;
const host = 'http://localhost:3000/#/';
const uri = new RegExp(escape('/users/login'));

module.exports = {
  'Login Test': function(browser) {
    let server;

    browser
      .windowSize('basicTest', 1440, 710)
      .url(host + 'account/login')
      .waitForElementVisible('body', WAIT_TIME)
      .setValue('input[type=email]', 'sample@sample.com')
      .setValue('input[type=password]', 'password')
      .execute(function() {
        server = sinon.fakeServer.create();
        server.respondWith('POST', uri, [
          200, { 'Content-Type': 'application/json' }, JSON.stringify(someResponseData),
        ]);
      })
      .submitForm('form')
      .execute(function() {
        server.respond();
      })
      .waitForElementNotPresent('input.[type=submit]', WAIT_TIME) // the page should be redirected to another page
      .execute(function() {
        server.restore();
        server = null;
      })
      .end();
  },
};

I can't mock response, and got the error below (When the API serve is running, got no error, but the response won't be mocked one). 我无法模拟响应,并得到以下错误(当API服务正在运行时,没有错误,但响应将不会被模拟)。

Error: Origin is not allowed by Access-Control-Allow-Origin

I want to know, first of all, is it correct way to use sinon.js's fakeServer? 我想知道,首先,使用sinon.js的fakeServer是正确的方法吗? And is that possible on e2e(and nightwatch.js)? 这可能在e2e(和nightwatch.js)上吗?

Please give me a help. 请给我一个帮助。

To answer your question I first need to explain the nature of the error you receive. 要回答您的问题,我首先需要解释您收到的错误的性质。

That's a CORS (cross origin resource sharing) error. 这是一个CORS (跨源资源共享)错误。

Basically, some service behind your single page app knows not to allow requests which don't originate from your app. 基本上,单页应用程序背后的某些服务知道不允许不是来自您的应用程序的请求。 The service returning that error (I can't tell what it is from the information you've posted) detects a request coming from not your app, and rejects it. 返回的错误(我不知道它是什么,从您发布的信息)服务检测到来自不是你的应用程序来的请求,并拒绝接受。

You could disable the CORS security of the service (I highly recommend you don't do this) or you could attempt to change the origin header of the request. 您可以禁用服务的CORS安全性(我强烈建议您不要这样做)或者您可以尝试更改请求的原始标头。 This is tricky, as most modern browsers specifically prevent this change in order to protect users. 这很棘手,因为大多数现代浏览器专门阻止此更改以保护用户。 Since you are using Nightwatch, your environment will, in general, be that of a browser. 由于您使用的是Nightwatch,因此您的环境通常是浏览器的环境。

Based on this error you must have set up the server incorrectly because it seems as if your request is still hitting your actual API and not the mocked server . 基于此错误, 您必须错误地设置服务器,因为您的请求似乎仍然是您的实际API而不是模拟服务器

Probably because you when submit the form, the browser will still submit the form to where it is supposed to go (and not your mock server) unless it is told otherwise. 可能是因为您在提交表单时,浏览器仍然会将表单提交到它应该去的地方(而不是您的模拟服务器),除非另有说明。 Looking at your code, you are setting up a mock server, but from this file alone it is not clear how the browser is supposed to know to send requests to that mocked server. 查看您的代码,您正在设置一个模拟服务器,但仅从该文件中不清楚浏览器应该知道如何向该模拟服务器发送请求。

See nock for an alternative solution, I'm about to start using it :) 看到nock的替代解决方案,我即将开始使用它:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM