我试图弄清楚如何测试React Native(而不是React JS)组件。即使查看React Native的初学者代码,也很难看出如何测试它。
createClass
我已经能够使用Babel来转换JSX语法以及使用Mockery来模拟React库方法StyleSheet.create
和static NHttpClientConnection httpConn = null;
public static void testOne() throws Exception {
HttpAsyncRequestExecutor protocolHandler = new HttpAsyncRequestExecutor();
// Create client-side I/O event dispatch
final IOEventDispatch ioEventDispatch = new DefaultHttpClientIODispatch(protocolHandler, ConnectionConfig.DEFAULT);
final ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor();
PoolingNHttpClientConnectionManager connManager = new PoolingNHttpClientConnectionManager(ioReactor);
connManager.setMaxTotal(100);
long connectTimeout=1;
long leaseTimeout=4;
TimeUnit timeUnit = TimeUnit.SECONDS;
Object state = null;
//HttpRoute route = new HttpRoute(new HttpHost("www.google.com", 80));
HttpRoute route = new HttpRoute(new HttpHost("www.google.com"));
// Run the I/O reactor in a separate thread
Thread t = new Thread(new Runnable() {
public void run() {
try {
// Ready to go!
ioReactor.execute(ioEventDispatch);
} catch (InterruptedIOException ex) {
System.err.println("Interrupted");
} catch (IOException e) {
System.err.println("I/O error: " + e.getMessage());
}
System.out.println("Shutdown");
}
});
t.start();
Future<NHttpClientConnection> connFuture = connManager.requestConnection(route, state, connectTimeout, leaseTimeout, timeUnit,
new FutureCallback<NHttpClientConnection>() {
public void completed(final NHttpClientConnection c) {
System.out.println("completed");
httpConn = c;
}
public void failed(final Exception ex) {
System.out.println("failed");
}
public void cancelled() {
System.out.println("cancelled");
}
} );
System.out.println("Step3");
connFuture.get();
System.out.println("Done");
ioReactor.shutdown();
}
,但是在一天结束时,我似乎无法创造任何有意义的测试。
答案 0 :(得分:3)
你应该模拟React Native包以及设置babel变换器和其他一些设置。也许你可以检查我的组件的单元测试,React Native Router Flux:
https://github.com/aksonov/react-native-router-flux/tree/master/tests
答案 1 :(得分:3)
要使用Jest运行测试,您应该使用__mocks__
文件夹将ReactNative替换为React,使用带有浅渲染器的TestUtils
并使用react-shallow-renderer-helpers来查找虚拟树。
答案 2 :(得分:1)
最后,我使用Mocha设置了TravisCI的回购,用于BDD样式的单元测试。您可以在https://github.com/sghiassy/react-native-sglistview
看到回购答案 3 :(得分:-1)
看看ReactNative的UIExplorer示例项目,该项目由多个混合XCTest&amp; amp; require(&#39; NativeModules&#39;)。TestModule测试套件。