【发布时间】:2023-04-06 07:52:01
【问题描述】:
我有两个数据类 - HindiTranslation 和 EnglishTranslation。我正在使用第三个数据类 - HindiAndEnglishTranslation,将它们映射到房间数据库中的一对多关系中。我试图通过断言给定和检索到的 HindiAndEnglishTranslation 对象是否相等来测试 Room Dao,但即使两个对象中的数据相同,测试也会失败。
印地语翻译:
@Entity(tableName = "hindi_translations")
data class HindiTranslation(
@PrimaryKey
var word: String,
val hindiTranslation: String,
val id: Long = randomNumberGenerator()
)
英文翻译:
@Entity(tableName = "english_translations")
data class EnglishTranslation(
val translations: List<String>,
val examples: List<String>,
val usage: String,
var parentKey: String,
@PrimaryKey
var englishId: Long = randomNumberGenerator(),
)
印地语和英语翻译:
data class HindiAndEnglishTranslation(
@Embedded val hindi: HindiTranslation,
@Relation(
parentColumn = "word",
entityColumn = "parentKey"
)
val englishTranslations: List<EnglishTranslation>
)
Dao 测试代码:
@Test
fun getAllTranslations() = runBlockingTest {
// Given - inserting a few hindi and corresponding english translations
database.getTranslationDao().insertHindiTranslation(hindi1)
database.getTranslationDao().insertEnglishTranslation(english1)
database.getTranslationDao().insertHindiTranslation(hindi2)
database.getTranslationDao().insertEnglishTranslation(english22)
database.getTranslationDao().insertEnglishTranslation(english32)
database.getTranslationDao().insertEnglishTranslation(english42)
// When - all translations in database are fetched
val translations = database.getTranslationDao().getAllTranslations()
// Then - the loaded translations contain the expected values
assertThat(translations, equalTo((listOf(hindiAndEnglish1, hindiAndEnglish2))))
}
我得到的错误:
java.lang.AssertionError:
Expected: <HindiAndEnglishTranslation(hindi=HindiTranslation(word=hot, hindiTranslation=गरम, id=6050220789869782304), englishTranslations=[EnglishTranslation(translations=[having a high degree of heat or a high temperature, (of food) containing or consisting of pungent spices or peppers which produce a burning sensation when tasted, passionately enthusiastic, eager, or excited], examples=[it was hot inside the hall, a very hot dish cooked with green chili, the idea had been nurtured in his hot imagination], usage=Adjective, parentKey=hot, englishId=133595475665305056)])>
but: was <HindiAndEnglishTranslation(hindi=HindiTranslation(word=hot, hindiTranslation=गरम, id=6050220789869782304), englishTranslations=[EnglishTranslation(translations=[having a high degree of heat or a high temperature, (of food) containing or consisting of pungent spices or peppers which produce a burning sensation when tasted, passionately enthusiastic, eager, or excited], examples=[it was hot inside the hall, a very hot dish cooked with green chili, the idea had been nurtured in his hot imagination], usage=Adjective, parentKey=hot, englishId=133595475665305056)])>
我做错了什么?
【问题讨论】:
标签: java android kotlin junit hamcrest