使用RxJava "java.lang.AssertionError: Values at position 0 differ; expected"的机器人电测试用例失败



我的测试用例在测试我的房间数据库时失败。这是充当房间数据库和我的存储库类之间的中间类的类......

class Cache @Inject constructor(var newsDb:NewsDb  ,var newsDbMapper: NewsDbMApperImpl) : DataStore {
override fun getNews(): Observable<List<News>> {
return newsDb.newsDao().getNews().toObservable().map {
it.map {
newsEntity ->
newsDbMapper.mapEntityToNews(newsEntity)
}
}
}
fun getNewsByTitle(title:String) :Observable<News> {
return newsDb.newsDao().getNewsByTitle(title).map {t ->
newsDbMapper.mapEntityToNews(t)
}
}
fun saveNews(list:List<News>):Completable{
return    Completable.defer{
var listNews = list.map {
newsDbMapper.mapNewsToEntity(it)
}
newsDb.newsDao().insertNews(listNews)
Completable.complete()
}
}
}

此类的测试用例:

@RunWith(RobolectricTestRunner::class)
class CacheTest {
@get:Rule
var instantTaskExecutorRule = InstantTaskExecutorRule()
private lateinit var newsDB: NewsDb
private lateinit var newsDbMapper: NewsDbMApperImpl
private lateinit var cache: Cache
@Before
fun initDatabase() {
val context = ApplicationProvider.getApplicationContext<Context>()
newsDB = Room.inMemoryDatabaseBuilder(context, NewsDb::class.java)
.allowMainThreadQueries()
.build()
newsDbMapper = NewsDbMApperImpl()
cache = Cache(newsDB, newsDbMapper)
}
@After
@Throws(IOException::class)
fun closeDatabase() {
newsDB.close()
}
@Test
fun insertNewsSavesData() {
val newsList = listOf(ProjectDataFactory.makeNews())
val testObserver = cache.saveNews(newsList).test()
testObserver.assertComplete()
}
@Test
fun getNewsReturnsData() {
val newsList = arrayListOf(ProjectDataFactory.makeNews())
cache.saveNews(newsList).test()
val testObserver = cache.getNews().test()
testObserver.await(5,TimeUnit.SECONDS)
testObserver.assertValue(newsList)
}
@Test
fun getNewsByTitleReturnsCorrectData() {
val news = ProjectDataFactory.makeNews()
val title = news.title
val newsList = listOf(news)
cache.saveNews(newsList).test()
val testObserver = cache.getNewsByTitle(title).test()
testObserver.assertValue(news)
}

}

但是我得到一个类似的错误:

java.lang.AssertionError: expected: [com.example.newreaderapplication.datastore.model.News@6017ad4a] (class: ArrayList) but was: [com.example.newreaderapplication.datastore.model.News@27e113e9] (class: ArrayList) (latch = 1, values = 1, errors = 0, completions = 0, timeout!)
Expected :[com.example.newreaderapplication.datastore.model.News@6017ad4a] (class: ArrayList) 
Actual   :[com.example.newreaderapplication.datastore.model.News@27e113e9] (class: ArrayList) (latch = 1, values = 1, errors = 0, completions = 0, timeout!)

任何建议都会有所帮助。

你得到了两个不同的List对象,所以equals((在尝试比较它们时返回false。即使您的内容相同。尝试逐项比较列表和列表内容的大小

相关内容

  • 没有找到相关文章

最新更新