在Robolectric 3阴影getFilesDir() [英] Shadowing getFilesDir() in Robolectric 3

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

问题描述

我想影 Context.getFilesDir()以虚拟化我的应用程序在测试环境中运行的文件系统,但我找不到正确的封闭类的影子。

I'd like to shadow Context.getFilesDir() in order to "virtualize" the filesystem my app is running in the test environment, but I can't find the right enclosing class to shadow.

我试过阴影上下文

@Implements(Context.class)
public class MyShadow extends ShadowContext {
    private File testRoot;

    public MyShadow() {
        testRoot = new File(FileUtils.getTempDirectory(), "androidTestRoot-" + System.currentTimeMillis());
    }

    @Implementation
    public File getFilesDir() {
        return new File(testRoot, "data/data/com.myapp/files");
    }
}

但方法不会被调用,因为很明显我的应用程序调用 ContextWrapper.getFilesDir()未阴影。

ContextWrapper 由Robolectric的 ShadowContextWrapper 阴影。但是,即使我的影子 ContextWrapper 改变我的类头为

ContextWrapper is shadowed by Robolectric's ShadowContextWrapper. But even if I shadow ContextWrapper changing my class header as

@Implements(ContextWrapper.class)
public class MyShadow extends ShadowContextWrapper 

我的方法是不是太叫

my method isn't called too.

ShadowContext ShadowContextWrapper 的超类,为上下文的阴影,但我找不到具体的实施 getFilesDir的()被调用。

ShadowContext, the superclass of ShadowContextWrapper, is the shadow of Context, but I cannot find the concrete implementation of getFilesDir() that is called.

所以我的问题是:是否有可能影 getFilesDir()?如果是这样,怎么样?

So my question is: is it possible to shadow getFilesDir() ? If so, how?

推荐答案

我真的不知道你正在试图做什么,但我已经使用下列以获得对Robolectric测试数据目录:

I'm not really sure of what you're trying to do, but I have used the following to get the data directory on Robolectric tests:

RuntimeEnvironment.application.getApplicationContext().getFilesDir();

从我已经能够创建虚拟文件不管出于什么目的,我需要在一个测试。希望它帮助。

From that I have been able to create dummy files for whatever purpose I need in a test. Hope it helps.

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

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