这是我的用例代码界面-
interface ItemUseCase {
fun performItemAction(action: ItemAction): LiveData<Result>
}
这里是实现-
class ItemUseCaseImpl(
private val dataRepository: DataRepository
) : ItemUseCase {
override fun performItemAction(action: ItemAction) = liveData {
emit(Result.Loading)
emit(dataRepository.performDataAction(action))
}
}
我是编写单元测试的新手。所以我需要帮助-
1)我可以在用例中测试的全部内容>
2)我也尝试在下面编写一些单元测试,但是如何将LiveData放入thenReturn
这是我的单元测试代码-
class ItemUseCaseImplTest {
@Rule
@JvmField
val rule = InstantTaskExecutorRule()
@Mock
private lateinit var itemUseCase: ItemUseCase
@Mock
private lateinit var dataRepository: DataRepository
@Before
fun setUp() {
dataRepository = mockk()
itemUseCase = ItemUseCaseImpl(dataRepository)
}
@After
fun tearDown() {
}
@Test
fun performItemAction() {
runBlocking {
whenever(
itemUseCase.performItemAction(
ItemAction.RegisterUser(“xyz”, "20")
)
)
.thenReturn(???)
}
}
}
这里是我的用例代码接口-ItemUseCase接口{fun performItemAction(action:ItemAction):LiveData