简体   繁体   English

如何使用chai在redux-sagas生成器功能测试用例中测试catch块?

[英]How to test catch block in redux-sagas generator function test cases using chai?

export function* testGeneratorFunction() {
   try {
       yield put(showSuccess(success));
   } catch (error) {
       yield put(showError(error));
   }
}

In the above function i need to test the catch block. 在上述功能中,我需要测试catch块。 I have to test whether showError() function is called or not. 我必须测试是否调用了showError()函数。

You might want to use an helper library for that, such as redux-saga-testing . 您可能想要使用帮助程序库,例如redux-saga-testing

Disclaimer: I wrote this library to solve that exact same problem 免责声明:我写了这个库来解决同样的问题

For your specific example, using Jest (but works the same for Mocha), you would write: 对于您的特定示例,使用Jest(但对Mocha的工作原理相同),您将编写:

import sagaHelper from 'redux-saga-testing';
import { call, put } from 'redux-saga/effects';
import { showSuccess, showError } from './my-actions';
import { api } from './my-api';

function* testGeneratorFunction() {
    try {
        const data = yield call(api);
        yield put(showSuccess(data));
    } catch (e) {
        yield put(showError(e.message));
    }   
}

describe('When testing a Saga that throws an error', () => {
    const it = sagaHelper(testGeneratorFunction());

    it('should have called the API first, which will throw an exception', result => {
        expect(result).toEqual(call(api));
        return new Error('Something went wrong');
    });

    it('and then trigger an error action with the error message', result => {
        expect(result).toEqual(put(showError('Something went wrong')));
    });
});

describe('When testing a Saga and it works fine', () => {
    const it = sagaHelper(testGeneratorFunction());

    it('should have called the API first, which will return some data', result => {
        expect(result).toEqual(call(api));
        return 'some data';
    });

    it('and then call the success action with the data returned by the API', result => {
        expect(result).toEqual(put(showSuccess('some data')));
    });
});

I modified your example slightly, because "put" would never throw an exception. 我稍微修改了您的示例,因为“ put”将永远不会引发异常。

In real life, a call to an API or some other logic would be more likely to throw an exception. 在现实生活中,对API或某些其他逻辑的调用更有可能引发异常。

You'll find plenty of other examples (more complex ones) on the project's GitHub. 您将在项目的GitHub上找到许多其他示例(更复杂的示例)

There is a decent chapter on Error Handling in the manual. 手册中有关于错误处理的适当章节。 In short, you can use .throw instead of .next to trigger the catch block in your saga: 简而言之,您可以使用.throw而不是.next来触发传奇中的catch块:

assert.deepEqual(
  gen.throw({message: "error!").value, // <-- notice the throw instead of next
  put({ type: 'PRODUCTS_REQUEST_FAILED', error }),
  "fetchProducts should yield an Effect put({ type: 'PRODUCTS_REQUEST_FAILED', error })"
)

I'm not familiar with Chai syntax, but for instance using Jest's expect it would be: 我对Chai语法不熟悉,但是例如使用Jest的expect是:

expect(gen.throw({message: "server ded"}).value).toEqual(
  put({ type: 'PRODUCTS_REQUEST_FAILED', error })
)

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

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