我正在使用MVP架构开发应用程序。我正在尝试使用MockWebServer测试我的应用程序的Interactors。好吧,我有这个测试:
@RunWith(RobolectricTestRunner::class)
@Config(constants = BuildConfig::class, manifest = "src/main/AndroidManifest.xml", packageName = "br.com.simplepass.simplepassnew", sdk = intArrayOf(23))
class LoginInteractorImplTest {
lateinit var mLoginInteractor : LoginInteractor
lateinit var mServer: MockWebServer
@Before
fun setUp(){
mLoginInteractor = LoginInteractorImpl()
mServer = MockWebServer()
mServer.start()
}
@Test
fun loginTest(){
mServer.url("http://192.168.0.10:8080/login")
val testSubscriber = TestSubscriber.create<User>()
mLoginInteractor.login("31991889992", "lala").subscribe(testSubscriber)
testSubscriber.assertNoErrors()
// testSubscriber.assertCompleted()
}
@After
fun tearDown(){
mServer.shutdown()
}
}
但是,当我取消注释TestSubscriber上的assertCompleted时,我总是得到assertionError ...我知道TestSubscriber可以工作,因为我在其他测试中使用它。
这是我的ApiCall:
@GET("login")
fun login() : Observable<User>
我的NetModule:
@Module
class NetModule(val mBaseUrl: String) {
@Provides
@Singleton
fun provideHttpCache(application: Application): Cache {
val cacheSize = 10 * 1024 * 1024
return Cache(application.cacheDir, cacheSize.toLong())
}
@Provides
@Singleton
fun provideOkhttpClient(cache: Cache) : OkHttpClient {
val client = OkHttpClient.Builder()
val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY
client.addInterceptor(interceptor)
return client.cache(cache).build()
}
@Provides
@Singleton
fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit {
return Retrofit.Builder()
.baseUrl(mBaseUrl)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(okHttpClient)
.build()
}
}
我的基本网址(那里没有后端服务器......可以是任何东西):
<string name="api_base_url">http://192.168.0.12:8080</string>
那么,我错过了什么?这段代码应该正常工作......
欢迎任何帮助!
修改
所以,我把代码更改为:
mLoginInteractor = LoginInteractorImpl()
mServer = MockWebServer()
mServer.enqueue(MockResponse()
.setResponseCode(200)
.setBody(Gson().toJson(User(1, "991889992", "Leandro", "123"))))
mServer.start()
val client = OkHttpClient.Builder()
val cacheSize = 10 * 1024 * 1024
client.cache(Cache(application.cacheDir, cacheSize.toLong())).build()
mLoginInteractor.setRetrofit(Retrofit.Builder()
.baseUrl(mServer.url("/"))
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(client.cache(Cache(application.cacheDir, cacheSize.toLong())).build())
.build())
而且:
val testSubscriber = TestSubscriber.create<User>()
mLoginInteractor.login("31991889992", "lala").subscribe(testSubscriber)
testSubscriber.assertNoErrors()
testSubscriber.assertReceivedOnNext(listOf(User(1, "991889992", "Leandro", "123")))
testSubscriber.assertCompleted()
但我仍然会收到此错误:
Number of items does not match. Provided: 1 Actual: 0.
Provided values: [User(id=1, phoneNumber=991889992, name=Leandro, password=123)]
Actual values: []
答案 0 :(得分:5)
这里有几件事情要发生。首先,MockWebServer.url()
针对模拟服务器的基本网址解析给定的网址,它不会设置网址。如果要设置网址,则需要将其传递给start()
方法。通常,您将改造配置为调用服务器的端点 -
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(server.url("/"))
// Other builder methods.
.build();
其次,要从模拟Web服务器获取响应,您需要将预期的响应排入MockResponse
。否则它不知道要发回什么。在提出请求之前,请执行以下操作 -
server.enqueue(new MockResponse().setBody("Success!"));
您需要构建响应以反映预期的响应。
有关更多示例,请参阅README。