集成测试React-Native iOS无法打印(console.log)

时间:2016-06-23 23:15:55

标签: javascript ios testing asynchronous react-native

所以我目前正在遵循本地反应原则所规定的格式:

完整的附注:如果您有一百万小时来设置集成测试,就像我一样,让我帮助您并使用下面的代码,然后您所要做的就是编辑方案并设置一个run参数。检查他们的UIExplorer应用程序,找出参数是什么以及将它设置为什么和东西。你只需要设置主目标。还要将IntegrationTesting.js和Tests.js放在文件夹的根目录中。)

回到问题:

https://facebook.github.io/react-native/docs/testing.html

我有三个主要文件。它们是IntegrationTests.m,IntegrationTests.js和Tests。 IntegrationTests.m如下所示:

#import <UIKit/UIKit.h>
#import <XCTest/XCTest.h>

#import <RCTTest/RCTTestRunner.h>

#import "RCTAssert.h"

#define RCT_TEST(name)                  \
- (void)test##name                      \
{                                       \
[_runner runTest:_cmd module:@#name]; \
}

@interface UIExplorerIntegrationTests : XCTestCase

@end

@implementation UIExplorerIntegrationTests
{
  RCTTestRunner *_runner;
}

- (void)setUp
{
  _runner = RCTInitRunnerForApp(@"IntegrationTests", nil);
}

#pragma mark - Test harness

- (void)testTheTester_waitOneFrame
{
  [_runner runTest:_cmd
            module:@"Tests"
      initialProps:@{@"waitOneFrame": @YES}
configurationBlock:nil];
}


#pragma mark - JS tests

// This list should be kept in sync with IntegrationTestsApp.js
RCT_TEST(Tests)



@end

IntegrationTests.js看起来像这样

'use strict';

var React = require('react');
var ReactNative = require('react-native');
var {
  AppRegistry,
  ScrollView,
  StyleSheet,
  Text,
  TouchableOpacity,
  View,
} = ReactNative;

var TESTS = [
  require('./Tests'),
];

TESTS.forEach(
  (test) => AppRegistry.registerComponent(test.displayName, () => test)
);

// Modules required for integration tests
// require('LoggingTestModule');

type Test = any;

var IntegrationTests = React.createClass({
  getInitialState: function() {
    return {
      test: (null: ?Test),
    };
  },
  render: function() {
    if (this.state.test) {
      return (
        <ScrollView>
          <this.state.test />
        </ScrollView>
      );
    }
    return (
      <View style={styles.container}>
        <Text style={styles.row}>
          Click on a test to run it in this shell for easier debugging and
          development.  Run all tests in the testing environment with cmd+U in
          Xcode.
        </Text>
        <View style={styles.separator} />
        <ScrollView>
          {TESTS.map((test) => [
            <TouchableOpacity
              onPress={() => this.setState({test})}
              style={styles.row}>
              <Text style={styles.testName}>
                {test.displayName}
              </Text>
            </TouchableOpacity>,
            <View style={styles.separator} />
          ])}
        </ScrollView>
      </View>
    );
  }
});

var styles = StyleSheet.create({
  container: {
    backgroundColor: 'white',
    marginTop: 40,
    margin: 15,
  },
  row: {
    padding: 10,
  },
  testName: {
    fontWeight: '500',
  },
  separator: {
    height: 1,
    backgroundColor: '#bbbbbb',
  },
});

AppRegistry.registerComponent('IntegrationTests', () => IntegrationTests);

Tests.js看起来像这样:

'use strict';

var React = require('react');
var ReactNative = require('react-native');

var {
  Text,
  View,
} = ReactNative;
var { TestModule } = ReactNative.NativeModules;

var Tests = React.createClass({
  propTypes: {
    waitOneFrame: React.PropTypes.bool
  },

  getInitialState() {
    return {
      done: false,
    };
  },

  componentDidMount() {
    console.log("I'll never see this print statement in the xcode console")
    this.runTest();
  },

  runTest() {

    async function SampleCall(){
        try{
          console.log("hey whatsup inside async!")
        }catch(e){
          throw new Error("ERROR: " + e);
          console.log("download failed: " + e)
        }
    }
    SampleCall();


  },

  render() {
    return (
      <View style={{backgroundColor: 'white', padding: 40}}>
        <Text>
          {this.constructor.displayName + ': '}
          {this.state.done ? 'Done' : 'Testing...'}
        </Text>
      </View>
    );
  }
});

Tests.displayName = 'Tests';

module.exports = Tests;

由于某种原因,我无法使用console.log进行打印。无论我是否在异步功能内打印,我都无法打印。我尝试使用远程调试器方法,但这会导致奇怪的行为。我能够打印,但我的测试会破坏并抱怨这个:

  
      
  • 确保Packager服务器正在运行。
  •   
  • 确保JavaScript调试器正在运行且未在断点或异常上暂停,并尝试重新加载。&#34;
  •   

是的,我尝试将localhost设置为我的IP地址,然后整个事情开始变得更加疯狂。

NSString *URLString = [NSString stringWithFormat:@"http://MY.IPA.DDR.ESS:%zd/debugger-proxy?role=client", port];
  

失败:捕获&#34; NSInternalInconsistencyException&#34;,&#34; RedBox错误:运行时尚未准备好进行调试。

如果你知道为什么我的console.log语句在xctests运行xctests时是不可见的,其格式是react-native已经设置了他们的集成测试,或者你知道我为什么无法设置远程调试器,请帮帮我!感谢

1 个答案:

答案 0 :(得分:0)

您应该尝试Reactotron获取日志,并测试AVA。 使用该工具进行测试比当前方法更简单,this是其完成方式的一些示例,它为我节省了大量时间。