如何测试 redux-saga 延迟
How to test redux-saga delay
问题
在redux-saga
中,我正在使用yield delay(1000);
。
在我的单元测试中,我做 expect(generator.next().value).toEqual(delay(1000));
.
我希望测试通过。
这是我的 sagas.js
:
import { delay } from 'redux-saga';
export function* incrementAsync() {
yield delay(1000);
}
这是我的sagas.test.js
import { delay } from 'redux-saga';
import { incrementAsync } from '../sagas';
describe('incrementAsync Saga test', () => {
it('should incrementAsync', () => {
const generator = incrementAsync();
expect(generator.next().value).toEqual(delay(1000));
});
});
● incrementAsync Saga 测试 › 应该 incrementAsync
expect(received).toEqual(expected)
Expected value to equal:
{"@@redux-saga/CANCEL_PROMISE": [Function anonymous]}
Received:
{"@@redux-saga/CANCEL_PROMISE": [Function anonymous]}
Difference:
Compared values have no visual difference.
问题
如何测试 redux-saga delay ?
如果你检查 delay
saga effect code 你可以看到它是一个绑定函数:
export const delay = call.bind(null, delayUtil)
因此,如果您在两个不同的模块中导入 delay
,这将是两个不同的函数,没有视觉差异。
您可以在 codesandbox 示例中检查这一点(请参阅测试选项卡):
const testFunction = () => {};
describe("example bound functions equality test", () => {
it("Two bound functions are not equal", () => {
expect(testFunction.bind(this))
.not.toEqual(testFunction.bind(this));
});
});
结果是:
要测试你的传奇,你应该模拟你的 delay
效果(如果你使用 Jest);
import { delay } from "redux-saga";
import { incrementAsync } from "../sagas";
jest.mock("redux-saga");
describe("incrementAsync Saga test", () => {
it("should incrementAsync", () => {
const generator = incrementAsync();
expect(generator.next().value).toEqual(delay(1000));
});
});
测试 Redux Saga 调用的一个好方法是使用 call
效果。在这种情况下,您可以按如下方式稍微重构您的 saga:
import { delay } from 'redux-saga';
import { call } from 'redux-saga/effects';
export function* incrementAsync() {
yield call(delay, 1000);
}
然后你可以这样测试:
import { delay } from 'redux-saga';
import { call } from 'redux-saga/effects';
describe('incrementAsync', () => {
it('should incrementAsync()', () => {
const generator = incrementAsync();
expect(generator.next().value).toEqual(call(delay, 1000));
});
});
这是可行的,因为 call
的 yield 结果是一个简单的对象,描述了对 delay
函数的调用。不需要任何模拟 :)
当然还有很棒的 redux-saga-test-plan
帮助程序库。使用它,您的测试将变成以下内容:
import { testSaga } from 'redux-saga-test-plan';
import { delay } from 'redux-saga';
import { call } from 'redux-saga/effects';
describe('incrementAsync', () => {
it('should incrementAsync()', () => {
testSaga(incrementAsync)
.next()
.call(delay, 1000)
.next()
.isDone();
});
});
问题
在redux-saga
中,我正在使用yield delay(1000);
。
在我的单元测试中,我做 expect(generator.next().value).toEqual(delay(1000));
.
我希望测试通过。
这是我的 sagas.js
:
import { delay } from 'redux-saga';
export function* incrementAsync() {
yield delay(1000);
}
这是我的sagas.test.js
import { delay } from 'redux-saga';
import { incrementAsync } from '../sagas';
describe('incrementAsync Saga test', () => {
it('should incrementAsync', () => {
const generator = incrementAsync();
expect(generator.next().value).toEqual(delay(1000));
});
});
● incrementAsync Saga 测试 › 应该 incrementAsync
expect(received).toEqual(expected)
Expected value to equal:
{"@@redux-saga/CANCEL_PROMISE": [Function anonymous]}
Received:
{"@@redux-saga/CANCEL_PROMISE": [Function anonymous]}
Difference:
Compared values have no visual difference.
问题
如何测试 redux-saga delay ?
如果你检查 delay
saga effect code 你可以看到它是一个绑定函数:
export const delay = call.bind(null, delayUtil)
因此,如果您在两个不同的模块中导入 delay
,这将是两个不同的函数,没有视觉差异。
您可以在 codesandbox 示例中检查这一点(请参阅测试选项卡):
const testFunction = () => {};
describe("example bound functions equality test", () => {
it("Two bound functions are not equal", () => {
expect(testFunction.bind(this))
.not.toEqual(testFunction.bind(this));
});
});
结果是:
要测试你的传奇,你应该模拟你的 delay
效果(如果你使用 Jest);
import { delay } from "redux-saga";
import { incrementAsync } from "../sagas";
jest.mock("redux-saga");
describe("incrementAsync Saga test", () => {
it("should incrementAsync", () => {
const generator = incrementAsync();
expect(generator.next().value).toEqual(delay(1000));
});
});
测试 Redux Saga 调用的一个好方法是使用 call
效果。在这种情况下,您可以按如下方式稍微重构您的 saga:
import { delay } from 'redux-saga';
import { call } from 'redux-saga/effects';
export function* incrementAsync() {
yield call(delay, 1000);
}
然后你可以这样测试:
import { delay } from 'redux-saga';
import { call } from 'redux-saga/effects';
describe('incrementAsync', () => {
it('should incrementAsync()', () => {
const generator = incrementAsync();
expect(generator.next().value).toEqual(call(delay, 1000));
});
});
这是可行的,因为 call
的 yield 结果是一个简单的对象,描述了对 delay
函数的调用。不需要任何模拟 :)
当然还有很棒的 redux-saga-test-plan
帮助程序库。使用它,您的测试将变成以下内容:
import { testSaga } from 'redux-saga-test-plan';
import { delay } from 'redux-saga';
import { call } from 'redux-saga/effects';
describe('incrementAsync', () => {
it('should incrementAsync()', () => {
testSaga(incrementAsync)
.next()
.call(delay, 1000)
.next()
.isDone();
});
});