【发布时间】:2017-05-27 22:13:45
【问题描述】:
我正在尝试使用 nock 来拦截/模拟我的应用程序中的一些 HTTP 流量以进行测试。我们的应用程序向我们的另一个站点进行身份验证,我需要 nock 来模拟 HTTP 200(带有 JSON 数据)和 HTTP 401(没有数据)来测试用户登录或未登录时的行为(分别) .
我有两个测试在单独运行时都可以正常工作,但是如果我运行整个测试套件,其中一个总是失败。我意识到 nock 是共享状态,因为它修改了 node.js 本身处理网络流量的方式,我认为这是竞争条件的原因,但我不能是唯一一个曾经为同一个请求使用两个不同的 nock 拦截器的人两个不同的测试,所以我知道我错过了一些东西。
谁能帮我弄清楚为什么这些测试会互相影响?
我的问题与How to retest same URL using Mocha and Nock? 有关,但我做了那里建议的事情,但它们没有帮助。
我的测试文件(同样,如果单独调用它们都可以正常工作,但在作为同一测试通过的一部分运行时会失败)如下所示:
import { expect } from 'chai';
import nock from 'nock';
import * as actionTypes from '../../src/constants/action-types';
import * as panoptes from '../../src/services/panoptes';
import { user } from '../modules/users/test-data';
const stagingHost = 'https://my-staging-server.org';
describe('Panoptes', () => {
afterEach(function (done) {
nock.cleanAll();
nock.disableNetConnect();
done();
});
beforeEach(function (done) {
nock.cleanAll();
nock.disableNetConnect();
done();
});
describe('with a valid user', function (done) {
let lastAction = null;
const scope = nock(stagingHost)
.get(/^\/oauth\/authorize/)
.reply(302, '', {
'location': 'https://localhost:3000',
'Strict-Transport-Security': 'max-age=31536000; includeSubDomains',
'X-Frame-Options': 'SAMEORIGIN',
'X-XSS-Protection': '1; mode=block',
});
scope
.get(/^\/api\/me/)
.reply(200, {
users: [user],
});
panoptes.checkLoginUser((action) => { lastAction = action; }).then(() => {
nock.removeInterceptor(scope);
done();
});
it('should know when somebody is logged in', function () {
expect(lastAction).to.not.be.null;
expect(lastAction.type).to.equal(actionTypes.SET_LOGIN_USER);
expect(lastAction.user).to.not.be.null;
expect(lastAction.user.id).to.equal(user.id);
expect(lastAction.user.login).to.equal(user.login);
});
});
});
和
import { expect } from 'chai';
import nock from 'nock';
import * as actionTypes from '../../src/constants/action-types';
import * as panoptes from '../../src/services/panoptes';
const stagingHost = 'https://my-staging-server.org';
describe('Panoptes', () => {
afterEach(function (done) {
nock.cleanAll();
nock.disableNetConnect();
done();
});
beforeEach(function (done) {
nock.cleanAll();
nock.disableNetConnect();
done();
});
describe('with no user', function (done) {
let lastAction = null;
const scope = nock(stagingHost)
.get(/^\/oauth\/authorize/)
.reply(302, '', {
'Cache-Control': 'no-cache',
'location': 'https://my-staging-server.org/users/sign_in',
'Strict-Transport-Security': 'max-age=31536000; includeSubDomains',
'X-Frame-Options': 'SAMEORIGIN',
'X-XSS-Protection': '1; mode=block',
});
scope
.get(/^\/api\/me/)
.reply(401);
panoptes.checkLoginUser((action) => { lastAction = action; }).then(() => {
nock.removeInterceptor(scope);
done();
});
it('should know that nobody is logged in', function () {
expect(lastAction).to.not.be.null;
expect(lastAction.type).to.equal(actionTypes.SET_LOGIN_USER);
expect(lastAction.user).to.be.null;
});
});
});
【问题讨论】:
-
同时运行这两个测试时看到什么错误?
-
我没有收到错误,实际上,只是第二次测试在应该通过时失败了。似乎第二个测试受到第一个测试的模拟的影响。
标签: node.js testing asynchronous mocha.js nock