简体   繁体   English

在Redux中测试动作创建者时,如何模拟Firebase SDK?

[英]How do you mock the Firebase SDK when testing action creators in Redux?

I'm working on a POC with React, Redux and Firebase. 我正在使用React,Redux和Firebase进行POC。 I'm currently looking at how to test action creators. 我目前正在研究如何测试动作创作者。 I've followed this - https://redux.js.org/recipes/writing-tests#async-action-creators guide and it's been helpful so far. 我已经按照这个 - https://redux.js.org/recipes/writing-tests#async-action-creators指南,到目前为止它一直很有帮助。 However, as a simple example I want to test that an action is dispatched after successfully authenticating with Firebase like so - 但是,作为一个简单的示例,我想测试在使用Firebase成功进行身份验证后调度操作,如此 -

Action creator 行动创造者

export const authenticate = (username, password) => {
  return dispatch => {
    firebase.auth().signInWithEmailAndPassword(username, password)
      .then(() => {
        dispatch(authenticationSuccessful())
      })
      .catch(() => {

      });
  }
};

Action 行动

const authenticationSuccessful = () => {
  return {
    type: actionTypes.AUTHENTICATION_SUCCESSFUL
  };
};

For the testing side of things I have jest, redux-mock-store and expect. 对于测试方面我喜欢的东西,redux-mock-store和expect。 I've researched as to what other people are using for this scenario and I haven't found a definitive answer, I've also looked at https://www.npmjs.com/package/firebase-mock but I don't know if this is a popular choice within the community. 我已经研究过其他人在这个场景中使用了什么,我还没有找到明确的答案,我也看过https://www.npmjs.com/package/firebase-mock但是我没有知道这是否是社区内的热门选择。

Really appreciate any help in advance! 真的很感激任何提前帮助!

This answer fits the bill Best way to unit test code that depends on http calls using Jest? 这个答案符合法案使用Jest依赖于http调用的单元测试代码的最佳方法?

According to that answer, this will overwrite the signInWithEmailAndPassword allowing your code to pass without hitting firebase. 根据该答案,这将覆盖signInWithEmailAndPassword允许您的代码通过而不会触及firebase。 The below example may need some tweaking. 以下示例可能需要一些调整。 Not sure of the scope of dispatch . 不确定dispatch的范围。

jest.mock('pathToFireBase', () => ({
   signInWithEmailAndPassword(email, password) {
     return Promise.resolve({name: 'someUser'})
   }
}))

const dispatch = jest.fn();
firebase.auth().signInWithEmailAndPassword(username, password)
expect(dispatch.mock.calls.length).toBe(1);

Another option (for Node.js) is to use something like VCR . 另一个选项(对于Node.js)是使用像VCR这样的东西。 An option being sepia . 一种选择是棕褐色 Its built by LinkedIn so probably better maintained and tested. 它由LinkedIn构建,因此可能更好地维护和测试。 With this tool you record your requests once, saving responses, and replaying when called. 使用此工具,您可以记录一次请求,保存响应并在调用时重放。 This option avoids mocking, but keeps the uint tests speedy with predefined responses. 此选项可避免模拟,但可通过预定义响应快速保持uint测试。 I'm unaware of a frontend option like this though. 我不知道像这样的前端选项。


Firebasemock looks like a great option. Firebasemock看起来很不错。 Although I can't see a signInWithEmailAndPassword . 虽然我看不到signInWithEmailAndPassword Authentication is done by explicitly setting the state. 通过显式设置状态来完成身份验证。 authentication example . 验证示例

mocksdk.auth().changeAuthState({
  uid: 'testUid',
  provider: 'custom',
  token: 'authToken',
  expires: Math.floor(new Date() / 1000) + 24 * 60 * 60,
  auth: {
    isAdmin: true
  }
});
mocksdk.auth().flush();
console.assert(document.location.href === '#/admin', 'redirected to admin');

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

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