如何在开玩笑的测试中测试链接的诺言?

如何在开玩笑的测试中测试链接的诺言?

问题描述:

下面,我对我的登录操作进行了测试.我正在模拟firebase函数,并想测试signIn/signOut函数是否被调用.

Below I have a test for my Login actions. I'm mocking a firebase function and want to test if the signIn/signOut functions are called.

测试通过了,但是我看不到第二个控制台日志.这是console.log('store ==>', store);行.

The tests pass however I do not see my 2nd console log. Which is this line console.log('store ==>', store);.

it('signIn should call firebase', () => {
  const user = {
    email: 'first.last@yum.com',
    password: 'abd123'
  };

  console.log('111');
  return store.dispatch(signIn(user.email, user.password)).then(() => {
    console.log('222'); // does not reach
    expect(mockFirebaseService).toHaveBeenCalled();
  });
  console.log('333');
});

●登录操作›登录应调用firebase

● login actions › signIn should call firebase

TypeError:auth.signInWithEmailAndPassword不是函数

TypeError: auth.signInWithEmailAndPassword is not a function

// Sign in action
export const signIn = (email, password, redirectUrl = ROUTEPATH_DEFAULT_PAGE) => (dispatch) => {
  dispatch({ type: USER_LOGIN_PENDING });

  return firebase
    .then(auth => auth.signInWithEmailAndPassword(email, password))
    .catch((e) => {
      console.error('actions/Login/signIn', e);
      // Register a new user
      if (e.code === LOGIN_USER_NOT_FOUND) {
        dispatch(push(ROUTEPATH_FORBIDDEN));
        dispatch(toggleNotification(true, e.message, 'error'));
      } else {
        dispatch(displayError(true, e.message));
        setTimeout(() => {
          dispatch(displayError(false, ''));
        }, 5000);
        throw e;
      }
    })
    .then(res => res.getIdToken())
    .then((idToken) => {
      if (!idToken) {
        dispatch(displayError(true, 'Sorry, there was an issue with getting your token.'));
      }

      dispatch(onCheckAuth(email));
      dispatch(push(redirectUrl));
    });
};

全面测试

    import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';

// Login Actions
import {
  // onCheckAuth,
  signIn,
  signOut
} from 'actions';

import {
  // USER_ON_LOGGED_IN,
  USER_ON_LOGGED_OUT
} from 'actionTypes';

// String Constants
// import { LOGIN_USER_NOT_FOUND } from 'copy';

const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);

// Mock all the exports in the module.
function mockFirebaseService() {
  return new Promise(resolve => resolve(true));
}

// Since "services/firebase" is a dependency on this file that we are testing,
// we need to mock the child dependency.
jest.mock('services/firebase', () => new Promise(resolve => resolve(true)));

describe('login actions', () => {
  let store;

  beforeEach(() => {
    store = mockStore({});
  });

  it('signIn should call firebase', () => {
    const user = {
      email: 'first.last@yum.com',
      password: 'abd123'
    };

    console.log('111');
    return store.dispatch(signIn(user.email, user.password)).then(() => {
      console.log('222'); // does not reach
      expect(mockFirebaseService).toHaveBeenCalled();
    });
    console.log('333');
  });

  it('signOut should call firebase', () => {
    console.log('signOut should call firebasew');
    store.dispatch(signOut()).then(() => {
      expect(mockFirebaseService).toHaveBeenCalled();
      console.log('store ==>', store);
      expect(store.getActions()).toEqual({
        type: USER_ON_LOGGED_OUT
      });
    });
    console.log('END');
  });
});

您在这里有2个问题,

测试通过了,但是我看不到第二个控制台日志.这是哪一个 行console.log('store ==>',store);.

The tests pass however I do not see my 2nd console log. Which is this line console.log('store ==>', store);.

那是因为测试不是在等待承诺的实现,所以您应该将其返回:

That is because the test is not waiting for the promise to fulfill, so you should return it:

it('signOut should call firebase', () => {
    console.log('signOut should call firebasew');
    return store.dispatch(signOut()).then(() => { // NOTE we return the promise
      expect(mockFirebaseService).toHaveBeenCalled();
      console.log('store ==>', store);
      expect(store.getActions()).toEqual({
        type: USER_ON_LOGGED_OUT
      });
      console.log('END');
    });

  });

您可以在 redux官方文档中找到示例.

其次,您的登录测试失败,因为您嘲笑了错误的Firebase:

Secondly, your signIn test failing because you have mocked wrong firebase:

jest.mock('services/firebase', () => new Promise(resolve => resolve(true)));

那应该看起来更像是:

jest.mock('services/firebase', () => new Promise(resolve => resolve({
    signInWithEmailAndPassword: () => { return { getIdToken: () => '123'; } }
})));