【问题标题】:Issue with Realm Unit Test on AndroidAndroid 上的领域单元测试问题
【发布时间】:2016-03-27 22:49:39
【问题描述】:

使用 JUnit4 Test Runner,测试运行并且似乎可以工作,但查询没有返回结果:

@RunWith(AndroidJUnit4::class)
class LocationViewInstrumentationTest {

@Rule
public val mActivityRule: ActivityTestRule<MapsActivity> = ActivityTestRule(MapsActivity::class.java)

@Rule
var testFolder = TemporaryFolder()


@Test
fun mapViewIsRendered() {
    onView(withId(R.id.map)).check(matches(isDisplayed()))
}

@Test
@Throws(IOException::class)
fun canSaveLocation() {
    val tempFolder = testFolder.newFolder("realmdata")
    val config = RealmConfiguration.Builder(tempFolder).build()
    val realm = Realm.getInstance(config)

    realm.beginTransaction()
    val location = Location("Poppy Manor", 33.2, -121.3, 0.0)

    assertThat(location, not(nullValue()))

    realm.commitTransaction()

    RealmQuery<Location> query = realm.where(Location.class);
    RealmResults<Location> results = query.findAll();
    assertThat(results.size(), equalTo(1));

}

是的,我查看了示例项目,但不想添加所有依赖项并希望我的测试可读,因此也尽量避免所有模拟。

【问题讨论】:

  • 您没有在事务中写入领域。尝试在realm.commitTransaction()之前添加realm.copyToRealm(location)
  • 你为什么不把这个作为答案?我在 Swift 中使用了 Realm,只是搬到了 android,并没有看到那些调用。我看到了那个方法,但认为它是为了与会话重新同步。谢谢!
  • 是的,添加答案。干杯!

标签: android unit-testing junit4 realm


【解决方案1】:

您没有在事务中写入领域。 尝试在realm.commitTransaction() 之前添加realm.copyToRealm(location)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多