小编way*_*ord的帖子

一些Robolectric测试在一起运行时失败但是单独通过

我在Android Studio 1.2,Robolectric 3.0-rc2上.

我有两个测试类,一个调用MotdTest一个测试方法,测试POJO json序列化和反序列化.调用另一个UserInfoTest,其中包含4个测试方法,用于测试我设置的用户信息SharedPreferences.如果我UserInfoTest单独运行,所有4种测试方法总是通过.但是,如果我运行所有测试,测试MotdTest成功,但两种方法UserInfoTest总是失败.我正在通过调用从命令行运行./gradlew test

当我运行所有测试时,有谁知道为什么我的一些测试失败了?在我的UserInfoTest中,我确实有一个带@After注释的方法,我通过调用clear().commit()来清理它SharedPreferences.Editor.

UserInfoTest:

testOnSignIn()assertThat(6, equalTo(prefs.getAll().size()));因为大小prefs为0 而失败.

并且testIsSignedIn()失败了assertThat(UserInfo.isSignedIn(), is(false));

@RunWith(MyRoboRunner.class)
@Config(constants = BuildConfig.class)
public class UserInfoTest {

    private String mExpectedId;

    private String mExpectedName;

    private String mExpectedEmail;

    private String mExpectedToken;

    private String mExpectedKey;

    @Before
    public void setUp() throws Exception {
        ShadowLog.stream = System.out;

        mExpectedId = "someiD";
        mExpectedName = "johnny boy";
        mExpectedEmail = …
Run Code Online (Sandbox Code Playgroud)

android robolectric robolectric-gradle-plugin

7
推荐指数
1
解决办法
1732
查看次数

defStyleAttrs参数如何在obtainStyledAttributes中使用

我试图在android上创建一个自定义复合视图(从relativelayout子类),并尝试指定默认样式.我可以做到这一点,但我想知道defStyleAttr参数用于什么,以及如何使用它.

我遇到了这个方法context.obtainStyledAttributes(AttributeSet set,int [] attrs,int defStyleAttr,int defStyleRes).

我明白那个:

  • AttributeSet set 如果从XML膨胀,我将从视图的构造函数中获取,
  • int[] attrs 将是我可能创建的任何自定义视图字段的关键值,
  • int defStyleRes 将是我可以提供的默认样式,它被定义为样式资源.

但是,我无法弄清楚如何使用int defStyleAttr参数.我已经阅读了文档并提供了这样的attr资源:

<declare-styleable name="BMTheme">
        <attr name="BMButtonStyle" format="reference" />
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)

然后在我的主题资源文件中我有这个:

<style name="Theme.Custom" parent="@android:style/Theme">
    <item name="BMButtonStyle">@style/BMButton</item>
</style>
Run Code Online (Sandbox Code Playgroud)

反过来引用这种风格:

<style name="BMButton">
    <item name="text">some string</item>
    <item name="android:paddingLeft">10dp</item>
    <item name="android:paddingRight">10dp</item>
</style>
Run Code Online (Sandbox Code Playgroud)

在代码中,当我传入0 for defStyleAttr和样式资源id时defStyleRes,我成功获得了我在BMButton样式中定义的默认值:

TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.RightArrowButton, 0, R.style.BMButton); 
Run Code Online (Sandbox Code Playgroud)

但是当我传入BMButtonStyle的attr资源id(后面又引用BMButton样式)时,为defStyleAttr0 defStyleRes然后我没有得到我在样式中指定的值.

TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.RightArrowButton, R.attr.BMButtonStyle, 0); 
Run Code Online (Sandbox Code Playgroud)

如果我defStyleAttr以错误的方式使用参数,请告诉我,如果有人知道,为什么只需要a defStyleAttrdefStyleRes参数就需要a 和参数defStyleRes …

android android-custom-view

5
推荐指数
1
解决办法
4792
查看次数