我正在创建一个测试来测试我的类Map器是否正确工作。Map器是正确的,但不知何故,assertEquals没有正确识别出我的实际和预期是相等的,它说它指向不同的对象,所以它不相等。
@RunWith(Parameterized::class)
class PaginationToPresentationMapperTest(
private val given: PaginationDomainModel,
private val expected: PaginationPresentationModel
) {
companion object {
@JvmStatic
@Parameters(name = "Given {0} then {1}")
fun data(): Collection<Array<*>> = listOf(
testCase(
nextCursor = null,
prevCursor = null
),
testCase(
nextCursor = "next_id",
prevCursor = null
),
testCase(
nextCursor = null,
prevCursor = "prev_id"
)
)
private fun testCase(
nextCursor: String?,
prevCursor: String?
) = arrayOf(
PaginationDomainModel(
nextCursor = nextCursor,
prevCursor = prevCursor
),
PaginationPresentationModel(
nextCursor = nextCursor,
prevCursor = prevCursor
)
)
}
private lateinit var classUnderTest: PaginationToPresentationMapper
@Before
fun setUp() {
classUnderTest = PaginationToPresentationMapper()
}
@Test
fun `When toPresentation`() {
// given
// when
val actual = classUnderTest.toPresentation(given)
// then
assertEquals(expected, actual)
}
}
不合格:
expected:<link.limecode.newsfeed.presentation.model.pagination.PaginationPresentationModel@7bd7d6d6> but was:<link.limecode.newsfeed.presentation.model.pagination.PaginationPresentationModel@50029372>
Expected :link.limecode.newsfeed.presentation.model.pagination.PaginationPresentationModel@7bd7d6d6
Actual :link.limecode.newsfeed.presentation.model.pagination.PaginationPresentationModel@50029372
1条答案
按热度按时间tcomlyy61#
您需要覆盖
PaginationPresentationModel
的equals
方法。默认情况下,equals
方法只比较示例相等性,因此具有相同字段值的两个不同对象将不相等。如果你使用
data class
,Kotlin会自动为你生成一个基于值的equals。