首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >read本机jest测试: TypeError:无法读取未定义的属性“unsubscribeFromTopic”

read本机jest测试: TypeError:无法读取未定义的属性“unsubscribeFromTopic”
EN

Stack Overflow用户
提问于 2017-02-09 16:21:26
回答 1查看 2.3K关注 0票数 1

我正在使用反应-自然-fcmjest来测试我的React。我有一个很基本的测试,看起来是这样的:

代码语言:javascript
复制
import 'react-native';
import React from 'react';
import PushController from '../app/PushController';
// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';

it('works correctly', () => {
  const tree = renderer.create(
    <PushController />
  );
});

PushController有点大,所以这里有一些有趣的部分

代码语言:javascript
复制
import React, { Component } from 'react';
import { AsyncStorage } from 'react-native';
import FCM from 'react-native-fcm';

export default class PushController extends Component {
(...)

  componentDidMount() {
    if (this.notificationListener) this.notificationListener.remove();

    this.notificationListener = FCM.on('notification', (notif) => {

        if (!notif.local_notification) {
          this.notifyUser(notif.coffee);
        }

    });
    FCM.unsubscribeFromTopic('/topics/coffee');
    FCM.subscribeToTopic('/topics/coffee');
  }
(...)

然而,当运行测试时,我得到

代码语言:javascript
复制
__tests__/PushControllerTest.js
  ● works correctly

    TypeError: Cannot read property 'unsubscribeFromTopic' of undefined

      at Object.FCM.unsubscribeFromTopic (node_modules/react-native-fcm/index.js:86:15)
      at PushController.componentDidMount (app/PushController.js:44:26)
      at node_modules/react-test-renderer/lib/ReactCompositeComponent.js:265:25
      at measureLifeCyclePerf (node_modules/react-test-renderer/lib/ReactCompositeComponent.js:75:12)
      at node_modules/react-test-renderer/lib/ReactCompositeComponent.js:264:11
      at CallbackQueue.notifyAll (node_modules/react-test-renderer/lib/CallbackQueue.js:76:22)
      at ReactTestReconcileTransaction.ON_DOM_READY_QUEUEING.close (node_modules/react-test-renderer/lib/ReactTestReconcileTransaction.js:36:26)
      at ReactTestReconcileTransaction.TransactionImpl.closeAll (node_modules/react-test-renderer/lib/Transaction.js:206:25)
      at ReactTestReconcileTransaction.TransactionImpl.perform (node_modules/react-test-renderer/lib/Transaction.js:153:16)
      at batchedMountComponentIntoNode (node_modules/react-test-renderer/lib/ReactTestMount.js:69:27)

我试过在测试中包括很多东西,比如jest.mock('react-native-fcm')和其他东西,但是我根本无法让它发挥作用。我知道jest会自动模拟库,但我不明白为什么FCM是未定义的。有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-09 16:47:21

我终于解决了!只需将我的测试更改为

代码语言:javascript
复制
import 'react-native';
import React from 'react';
import PushController from '../app/PushController';
// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';
import FCM from 'react-native-fcm'; // <-- This

it('works correctly', () => {
  FCM.unsubscribeFromTopic = jest.fn(); // <-- These two 
  FCM.subscribeToTopic = jest.fn();
  const tree = renderer.create(
    <PushController />
  );
});

以确保实际的调用被模仿。在此之前我做了很多谷歌搜索,所以我相信这对某些人是有用的。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42141783

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档