带有 Fabric Crashlytics 的 Robolectric 3 [英] Robolectric 3 with Fabric Crashlytics

查看:55
本文介绍了带有 Fabric Crashlytics 的 Robolectric 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ShadowClass Crashlytics/Fabric,以便 Robotlectric 3 测试不会失败.到目前为止,我所拥有的是:

I am trying to ShadowClass Crashlytics/Fabric so that Robotlectric 3 tests do not fail. What I have so far is this:

为 Fabric 添加 Shadow 类的自定义测试运行器:

The custom test runner that adds the Shadow class for Fabric:

public class TestRunner extends RobolectricGradleTestRunner {
    public TestRunner(Class<?> klass) throws InitializationError {
        super(klass);
    }

    @Override
    protected ShadowMap createShadowMap() {
        return super.createShadowMap()
            .newBuilder().addShadowClass(ShadowFabric.class).build();
    }

    @Override
    public InstrumentationConfiguration createClassLoaderConfig() {
        InstrumentationConfiguration.Builder builder = InstrumentationConfiguration.newBuilder();
        builder.addInstrumentedClass(ShadowFabric.class.getName());
        return builder.build();
    }
}

Fabric 的影子类:

The shadow class for Fabric:

@Implements(Fabric.class)
public class ShadowFabric {
    @Implementation
    public static Fabric with(Context context, Kit... kits) {
        System.out.println("Shadowing Fabric");
        return null;
    }
}

我的应用程序类:

public class MyApp extends Application {
    @Override
    public void onCreate() {
        setupCrashlytics();
    }
    protected void setupCrashlytics() {
        Crashlytics crashlyticsKit = new Crashlytics.Builder().core(new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build()).build();
        // Initialize Fabric with the debug-disabled crashlytics.
        Fabric.with(this, crashlyticsKit);
    }
}

这是在 Debug 中通过的测试(因为 Crashlytics 在其上被禁用),但在发布模式下失败,因为 ShadowClass 无法正常工作:

And here is the test that passes in Debug (because Crashlytics is disabled on it), but fails in release mode because the ShadowClass is not working correctly:

@RunWith(TestRunner.class)
@Config(constants = BuildConfig.class, sdk=21, packageName="com.my.release.package.name", shadows={ShadowFabric.class})
public class MyTest {
    @Test
    public void testGreenDAOsave() {
        // blah
    }
}

我在测试过程中使用 Crashlytics/Fabric 遇到的错误如下:

The error I get with Crashlytics / Fabric during the test is the following:

STANDARD_ERROR
io.fabric.sdk.android.services.concurrency.UnmetDependencyException: com.crashlytics.android.core.CrashlyticsMissingDependencyException:
This app relies on Crashlytics. Please sign up for access at https://fabric.io/sign_up
install an Android build tool and ask a team member to invite you to this app's organization.

堆栈跟踪显示正在调用 MyApp.setupCrashlytics() 并且 Fabric.with() 失败.

The stack trace shows that MyApp.setupCrashlytics() is being called and Fabric.with() is failing.

1) 是的,该应用已在 Crashlytics 注册.

1) YES, the app is registered with Crashlytics.

2) 是的,我确实联系了 Crashlytics 支持电子邮件.有人告诉我不支持 Robolectric".

2) YES, I did contact Crashlytics support email. I was told 'Robolectric is not supported'.

据我所知,我只需要让影子类工作,然后 Crashlytics 就会被影子而不是初始化.

From what I can see, I just need to get the shadow class thing working and then Crashlytics will get shadowed and not init'd.

非常感谢您的想法/帮助!

Ideas / Help would be very much appreciated!

推荐答案

这是我通常的建议,如何针对不可测试的内容编写测试.

This is my usual advice how to write a test against something not testable.

将您的 Fabric 初始化提取到受保护的方法:

Extract you Fabric initialisation to protected method:

public class <MyApplicationName> {

public void onCreate() {
   initFabric();
}

@VisibileForTesting
void initFabric() {
....
}
}

在测试源中创建 Test 类(相同的包并覆盖 Fabric 初始化:

Create Test<MayApplicationName> class in test sources (same package and override Fabric initialisation:

public class Test<MyApplicationName> {
void initFabric() {
//nothing to do
}
}

在您需要使用 Fabric 的任何地方使用 DI(依赖注入)在测试中模拟 Fabric.更重要的是,我建议您创建 Analytics/Crash/Distribution 类,并在整个应用程序中隐藏 Fabric 使用情况.

Everywhere where you need using Fabric use DI (Dependency Injection) to mock Fabric in tests. Even more, I would suggest you create Analytics/Crash/Distribution class and hide Fabric usage from entire application.

最后,您留下了包装/隐藏 Fabric 的类.在这里,您可以编写自定义阴影、监视真实对象或使其未经测试.而且您已经尝试过编写自定义阴影但没有成功,而且,这里不能进行间谍活动.

And final you have left classes that wrap/hide the Fabric. Here you can write a custom shadow, spy on the real object or leave it untested. And you already tried to write custom shadow without success, also, spying is not an option here.

快乐编码!

这篇关于带有 Fabric Crashlytics 的 Robolectric 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆