使用 sinon 存根时 ava 异步测试的问题
Problems with ava asynchronous tests when stubbing with sinon
我想在我的一个依赖项的 .then
和 .catch
块上 运行 进行一些测试。
import test from 'ava';
import sinon from 'sinon';
// Fake dependency code - this would be imported
const myDependency = {
someMethod: () => {}
};
// Fake production code - this would be imported
function someCode() {
return myDependency.someMethod()
.then((response) => {
return response;
})
.catch((error) => {
throw error;
});
}
// Test code
let sandbox;
test.beforeEach(() => {
sandbox = sinon.sandbox.create();
});
test.afterEach.always(() => {
sandbox.restore();
});
test('First async test', async (t) => {
const fakeResponse = {};
sandbox.stub(myDependency, 'someMethod')
.returns(Promise.resolve(fakeResponse));
const response = await someCode();
t.is(response, fakeResponse);
});
test('Second async test', async (t) => {
const fakeError = 'my fake error';
sandbox.stub(myDependency, 'someMethod')
.returns(Promise.reject(fakeError));
const returnedError = await t.throws(someCode());
t.is(returnedError, fakeError);
});
如果您 运行 单独测试,则测试通过。但是如果你 运行 这些在一起,测试 A 的设置 运行s,然后 在 它完成之前,测试 B 的设置 运行 s,你得到这个错误:
Second async test
failed with "Attempted to wrap someMethod which is already wrapped"
也许我不明白我应该如何设置我的测试。有没有办法强制测试 A 在测试 B 开始之前完成 运行ning?
AVA 测试是 运行 同时进行的,这会弄乱你的 Sinon 存根。
相反,将您的测试声明为 运行 serially 它应该有效:
test.serial('First async test', ...);
test.serial('Second async test', ...);
运行 串行会减慢测试的 运行ning 速度。当然,您可以创建单独的变量 sandbox1 和 sandbox2 以防止第一个测试使用第二个测试中设置的沙箱。但是为了确保我没有犯这个错误,我所做的是 运行 IIFE 中的每个测试(立即调用的函数表达式)。一个 IIFE 中的任何变量都不同于另一个 IIFE 中的任何变量,即使它们具有相同的名称:
(function () {
...one test
});
(function () {
...anothertest
});
我想在我的一个依赖项的 .then
和 .catch
块上 运行 进行一些测试。
import test from 'ava';
import sinon from 'sinon';
// Fake dependency code - this would be imported
const myDependency = {
someMethod: () => {}
};
// Fake production code - this would be imported
function someCode() {
return myDependency.someMethod()
.then((response) => {
return response;
})
.catch((error) => {
throw error;
});
}
// Test code
let sandbox;
test.beforeEach(() => {
sandbox = sinon.sandbox.create();
});
test.afterEach.always(() => {
sandbox.restore();
});
test('First async test', async (t) => {
const fakeResponse = {};
sandbox.stub(myDependency, 'someMethod')
.returns(Promise.resolve(fakeResponse));
const response = await someCode();
t.is(response, fakeResponse);
});
test('Second async test', async (t) => {
const fakeError = 'my fake error';
sandbox.stub(myDependency, 'someMethod')
.returns(Promise.reject(fakeError));
const returnedError = await t.throws(someCode());
t.is(returnedError, fakeError);
});
如果您 运行 单独测试,则测试通过。但是如果你 运行 这些在一起,测试 A 的设置 运行s,然后 在 它完成之前,测试 B 的设置 运行 s,你得到这个错误:
Second async test
failed with "Attempted to wrap someMethod which is already wrapped"
也许我不明白我应该如何设置我的测试。有没有办法强制测试 A 在测试 B 开始之前完成 运行ning?
AVA 测试是 运行 同时进行的,这会弄乱你的 Sinon 存根。
相反,将您的测试声明为 运行 serially 它应该有效:
test.serial('First async test', ...);
test.serial('Second async test', ...);
运行 串行会减慢测试的 运行ning 速度。当然,您可以创建单独的变量 sandbox1 和 sandbox2 以防止第一个测试使用第二个测试中设置的沙箱。但是为了确保我没有犯这个错误,我所做的是 运行 IIFE 中的每个测试(立即调用的函数表达式)。一个 IIFE 中的任何变量都不同于另一个 IIFE 中的任何变量,即使它们具有相同的名称:
(function () {
...one test
});
(function () {
...anothertest
});