android - Robolectric 为测试膨胀自定义 View

标签 android robolectric

我正在尝试为自定义 View 编写一些测试,但在我的测试用例中扩展自定义 View 时遇到了问题。我得到的错误是

android.view.InflateException: <merge /> can be used only with a valid ViewGroup root and attachToRoot=true
    at android.view.LayoutInflater.inflate(LayoutInflater.java:458)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
    at com.androidas.models.ui.order.MyLocationViewTest.setUp(MyLocationViewTest.java:45)

我什至尝试制作一个带有标签的虚拟 Activity (TestActivity) 以尝试对其进行充气

public class MyLocationView extends RelativeLayout {

private TextView mTextEta;
private TextView mTextOnTime;
private Date mMeetTime;
private CalendarManager mCalendarManager;



public MyLocationView(Context context) {
    this(context, null);
}

public MyLocationView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public MyLocationView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    LayoutInflater.from(context)
            .inflate(R.layout.v_mylocation, this, true);

    mTextEta = (TextView) findViewById(R.id.order_statusTracking_txt_myEta);
    mTextOnTime = (TextView) findViewById(R.id.order_statusTracking_txt_myDelay);

}
}

布局

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
>


<ImageView
    android:id="@+id/order_statusTracking_img_walking"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_walk"
    />

<TextView
    android:id="@+id/order_statusTracking_txt_myEta"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/spacing_small"
    android:layout_toRightOf="@+id/order_statusTracking_img_walking"
    android:layout_toEndOf="@+id/order_statusTracking_img_walking"
    android:gravity="center_vertical"
    tools:text="Arrive in 7min"
    style="@style/HeaderText"
    />

<TextView
    android:id="@+id/order_statusTracking_txt_myDelay"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/order_statusTracking_txt_myEta"
    android:layout_below="@+id/order_statusTracking_txt_myEta"
    android:layout_marginTop="@dimen/spacing_normal"
    tools:text="On Time"
    style="@style/InfoText.Black"
    />
</merge>

和测试用例:

@RunWith(RobolectricTestRunner.class)
@Config(emulateSdk = 18)
public class MyLocationViewTest {

MyLocationView mLocation;
TextView mTextDelay;
Trip mTrip;
CalendarManager mCalendarManager;
Date meetupTime;


@Before
public void setUp() {


    Activity activity = Robolectric.buildActivity(TestActivity.class).create().get();
    mLocation = (MyLocationView) LayoutInflater.from(activity).inflate(R.layout.v_mylocation, null);
    mTextDelay = (TextView) mLocation.findViewById(R.id.order_statusTracking_txt_myDelay);
}

最佳答案

好吧,1 年后,但这是对我有用的奇怪的东西:

MyCustomView mView = (MyCustomView) LayoutInflater
        .from(RuntimeEnvironment.application)
        .inflate(R.layout.my_custom_view_layout, 
                 new MyCustomView(RuntimeEnvironment.application),
                 true);

关于android - Robolectric 为测试膨胀自定义 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28975057/

相关文章:

java - AssertThat 错误 : Cannot access path (java. nio.file.Path 未找到)

android - 没有 setter 的模拟模型对象

java - 放在 Intent/bundle 中的一些 Parcelables 对象可能会 self 干扰并影响 Intent/Bundle 的读取?

android - 更新 firebase 构建版本后无法运行该应用程序

android - 如何在 Corona 中使用 "back"硬件按钮?

powermock - Robolectric 和 Powermock 之间的类加载冲突

android - 使用 Glide 将 1 个图像加载到多个 ImageView

android - 如何在 Eclipse 中 merge 代码

android - robolectric 的一些方法找不到影子方法怎么办?

java - Android Studio Robolectric 单元测试 - 默认值无效