【问题标题】:Writing unit test for react-native native android methods为 react-native 本机 android 方法编写单元测试
【发布时间】:2018-10-02 09:22:08
【问题描述】:

我正在构建 react-native 应用程序,其中包含一些原生 android 模块。
在 MainApplication.java 中,

protected List<ReactPackage> getPackages() {
    return Arrays.<ReactPackage>asList(
        new VectorIconsPackage(),
        new MyCustomPackage()
    );
  }

在我的 MyCustomPackage 中,

public class MyCustomPackage implements ReactPackage {

  @Override
  public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
    return Collections.emptyList();
  }

  @Override
  public List<NativeModule> createNativeModules(
                              ReactApplicationContext reactContext) {
    List<NativeModule> modules = new ArrayList<>();

    modules.add(new MyCustomModule(reactContext));

    return modules;
  }

}

我有几个其他模块,但这是一个示例。所有功能都运行良好。现在我想为 MyCustomModule java 类中的方法编写单元测试。我尝试使用 Robolectric 框架,但不知道它是如何与 react native 一起工作的。有没有其他工具可以做到这一点?谁能给我一些示例或指导为 react-native 本机 android 代码编写单元测试?

【问题讨论】:

  • 已经找到解决方案了吗?
  • 没有。仍然没有找到解决方案。

标签: android unit-testing react-native react-native-android react-native-native-module


【解决方案1】:

使用 Robolectric 4。

查看我的 cmets。

我的做法是

  1. 模拟应用程序以删除不兼容的依赖项加载。
  2. 将 ApplicationContext 包装在 ReactApplicationContext 中以实例化模块。

@Config 和自定义应用程序可能需要 删除 robolectric 未处理的二进制依赖项,例如 Bugsnag 和通用 soloader。如果您的所有依赖项都可用于您的 dev env 系统架构(这不太可能),也许可以在没有的情况下工作。

@RunWith(AndroidJUnit4.class)
@Config(
    application = TestApplication.class
)
public class ReactModuleSpec {

    private ReactModule reactModule;

    @Before
    public void beforeEach() {
        // Retrieve application context.
        Context applicationContext = ApplicationProvider.getApplicationContext();

        // Recreate ReactApplicationContext which ReactModule depends upon.
        // ReactApplicationContext sole purpose as documented in its source code
        // is to preserve type integrity of ApplicationContext over Context
        // (which android Context obviously does not). This should be safe
        // thus. See my post here:
        // `https://stackoverflow.com/questions/49962298/writing-unit-test-for-react-native-native-android-methods`.
        ReactApplicationContext reactApplicationContext = new ReactApplicationContext(applicationContext);

        // Instantiate the module.
        reactModule = new ReactModule(reactApplicationContext);
    }

    // ...

}
public class TestApplication extends Application implements ReactApplication {

    // ...

    // Remove packages incompatible with Robolectric.
    @Override
    protected List<ReactPackage> getPackages() {
        List<ReactPackage> packages = new PackageList(this).getPackages();

        // ... Your standard stuffs

        packages.removeIf(pkg -> pkg.getClass().isInstance(BugsnagReactNative.getPackage().getClass()));

        return packages;
    }

    // Remove soloader !
    @Override
    public void onCreate() {
        super.onCreate();

        // Bye bye!
        // BugsnagReactNative.start(this);
        // SoLoader.init(this, /* native exopackage */ false);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-12
    相关资源
    最近更新 更多