2017-11-21 9 views
0

Jest에서 다음 테스트 코드가 있습니다. 내가 NativeModules 가져옵니다조롱 된 객체에 직접 액세스 할 수 없습니다.

import { NativeModules } from 'react-native'; 

그런 다음, 각 시험 전에, 내가 내 자신의 개체 추가 : 제가 테스트입니다 소스 코드에서

beforeEach(() => { 
    NativeModules.Dispatcher = { 
     methodA: jest.fn(), 
     methodB: jest.fn(), 
     methodC: jest.fn() 
    }; 
}); 

을, 나는 NativeModules 가져옵니다

import { NativeModules } from 'react-native'; 

내 조롱 된 객체를 참조하십시오.

class ClassThatIsTested { 

    someMethod(parameter) { 
     NativeModules.Dispatcher.methodA(parameter); 
     //some other code 
    } 

} 

잘 작동합니다. 그러나, 나는 직접 조롱 객체를 참조하려고하면 :

import { NativeModules } from 'react-native'; 
const { Dispatcher } = NativeModules; 
class ClassThatIsTested { 

    someMethod(parameter) { 
    Dispatcher.methodA(parameter); 
     //some other code 
    } 

} 

그것은 실패

Dispatcher.methodA is not a function 

내가 Dispatcher 인쇄하는 경우, 내가 undefined를 얻을.

왜 이런 일이 발생합니까? 왜 조롱 된 객체에 직접 액세스 할 수 없습니까?

답변

0
import NativeModules from 'react-native'; 

가 잘못, 당신은 오히려 기본 내보내기보다 import the named export NativeModules 필요 : 정말 미안 해요

import {NativeModules} from 'react-native'; 
+0

, 내가 잘못 내 코드를 복사했습니다. 사실, 나는'react-native '에서'import {NativeModules}'를 사용합니다; – octavian