gpt4 book ai didi

android - 如何测试在 Android MVP Clean Architecture 中注入(inject)存储库的 sharedpreference

转载 作者:行者123 更新时间:2023-11-29 16:45:15 24 4
gpt4 key购买 nike

我在测试数据存储中的共享偏好时遇到了问题。在实际的数据存储中,我实现了三个参数,其中包括 sharedpreference。

在这种情况下,我想存储值,并获取该值。 mock 在这里没有帮助。

mocking 无法传播将由代码使用的实际值。在第二部分。

class FooDataStoreTest : Spek({
given("a foo data store") {

val schedulerRule = TestSchedulerRule()
val service: FooRestService = mock()
val context: Context = mock()
val gson: Gson = mock()
val appFooPreference: SharedPreferences = mock()

var appFooSessionStoreService: AppFooSessionStoreService? = null
var fooStoredLocationService: FooStoredLocationService? = null

beforeEachTest {
appFooSessionStoreService = AppFooSessionStoreService.Builder()
.context(context)
.gson(gson)
.preference(appFooPreference)
.build()
fooStoredLocationService = FooStoredLocationService(appFooSessionStoreService)
}

val longitude = 106.803090
val latitude = -6.244285

on("should get foo service with request longitude $longitude and latitude $latitude") {
it("should return success") {
with(fooStoredLocationService) {
val location = Location()
location.latitude = latitude
location.longitude = longitude

// i want to store location in this
fooStoredLocationService?.saveLastKnownLocation(location)

// and retrieve in below code
val l = fooStoredLocationService?.lastKnownLocation

val dataStore = FooDataStore(service, preference, fooStoredLocationService!!)
service.getFooService(longitude, longitude) willReturnJust
load(FooResponse::class.java, "foo_response.json")

val testObserver = dataStore.getFooService().test()
schedulerRule.testScheduler.advanceTimeBy(2, TimeUnit.SECONDS)
testObserver.assertNoErrors()
testObserver.awaitTerminalEvent()
testObserver.assertComplete()
testObserver.assertValue { actual ->
actual == load(FooResponse::class.java, "foo_response.json")
}
}
}
}

afterEachTest {
appFooSessionStoreService?.clear()
fooStoredLocationService?.clear()
}
}})

这个数据存储看起来像

open class FooDataStore @Inject constructor(private val fooRestService: FooRestService,
private val fooPreference: FooPreference,
private val fooLocation: fooStoredLocationService) : FooRepository {

private val serviceLocation by lazy {
fooLocation.lastKnownLocation
}

override fun getFooService(): Single<FooResponse> {
safeWith(serviceLocation, {
return getFooLocal(it).flatMap({ (code, message, data) ->
if (data != null) {
Single.just(FooResponse(code, message, data))
} else {
restService.getFooService(it.longitude, it.latitude).compose(singleIo())
}
})
})
return Single.error(httpExceptionFactory(GPS_NOT_SATISFYING))
}

实际上我想从这个字段 serviceLocation 中获取值。任何人都有办法为此做一些测试吗?非常欢迎任何建议。

谢谢!

最佳答案

我建议你不要直接依赖SharedPreferences,而是要有一些接口(interface)LocalStorage,这样你就可以使用你的SharedPrefsLocalStorage在代码和 TestLocalStorage 测试中。 SharedPrefsLocalStorage 将在底层使用 SharedPreferencesTestLocalStorage 一些 Map 实现。

举个简单的例子:

// You may add other type, not Int only, or use the String and convert everything to String and back
interface LocalStorage {
fun save(key: String, value: Int)
fun get(key: String): Int?
}

class SharedPrefsLocalStorage(val prefs: SharedPreferences) : LocalStorage {
override fun save(key: String, value: Int) {
with(prefs.edit()){
putInt(key, value)
commit()
}
}
override fun get(key: String): Int? = prefs.getInteger(key)
}

class TestLocalStorage : LocalStorage {
val values = mutableMapOf<String, Any>()

override fun save(key: String, value: Int) {
values[key] = value
}

override fun get(key: String): Int? = map[value] as Int?
}

关于android - 如何测试在 Android MVP Clean Architecture 中注入(inject)存储库的 sharedpreference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48728748/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com