0

我开始我的仪器测试来检查我的活动。当按下 Activity 上的按钮然后调用 http 请求。所以我测试点击

@Test
   fun click_checkRequest() {
       mockServer.enqueue(
           MockResponse()
                    .setResponseCode(200)
       )

       myContainer.click()
       val request = mockServer.takeRequest();
       Assert.assertEquals("POST", request.method)
       assertThat(request.path, CoreMatchers.containsString("/event?"))
       assertThat(
           request.body.readUtf8(), CoreMatchers.containsString( """type":1""")
       )
   }

这里日志:

11-29 11:06:26.952 D/OkHttp  (15697): --> POST http://127.0.0.1:8081/event?table_token=11&device_id=111111 http/1.1
11-29 11:06:26.952 D/OkHttp  (15697): Content-Type: application/json; charset=UTF-8
11-29 11:06:26.952 D/OkHttp  (15697): Content-Length: 10
11-29 11:06:26.953 D/OkHttp  (15697): {"type":1}
11-29 11:06:26.953 D/OkHttp  (15697): --> END POST (10-byte body)
11-29 11:06:26.968 I/MockWebServer(15697): MockWebServer[8081] received request: POST /event?table_token=my_token&user=my_user&device_id=my_device HTTP/1.1 and responded: HTTP/1.1 200 OK
11-29 11:06:26.969 D/OkHttp  (15697): <-- 200 OK http://127.0.0.1:8081/event?table_token=my_token&user=my_user&device_id=my_device (16ms)
11-29 11:06:26.969 D/OkHttp  (15697): Content-Length: 0
11-29 11:06:26.969 D/OkHttp  (15697): <-- END HTTP (0-byte body)

它工作正常。请求的正文内容“type”:1”。因此测试通过了。很好。

但我的活动(创建时))在后台定期(每 5 秒)启动下一个 http 请求:

11-29 11:59:35.843 D/OkHttp  ( 1116): --> GET http://127.0.0.1:8081/event?orgn=17 http/1.1
11-29 11:59:35.843 D/OkHttp  ( 1116): --> END GET
11-29 11:59:35.862 I/MockWebServer( 1116): MockWebServer[8081] received request: GET /event?orgn=17 HTTP/1.1 and responded: HTTP/1.1 200 OK
11-29 11:59:35.863 D/OkHttp  ( 1116): <-- 200 OK http://127.0.0.1:8081/event?orgn=17 (20ms)
11-29 11:59:35.863 D/OkHttp  ( 1116): Content-Length: 0
11-29 11:59:35.863 D/OkHttp  ( 1116): <-- END HTTP (0-byte body)

11-29 11:59:35.940 D/OkHttp  ( 1116): --> POST http://127.0.0.1:8081/event?table_token=11&device_id=111111 http/1.1
11-29 11:59:35.940 D/OkHttp  ( 1116): Content-Type: application/json; charset=UTF-8
11-29 11:59:35.940 D/OkHttp  ( 1116): Content-Length: 10
11-29 11:59:35.940 D/OkHttp  ( 1116): {"type":1}
11-29 11:59:35.940 D/OkHttp  ( 1116): --> END POST (10-byte body)

如您所见,在我的测试运行 POST之前GET http://127.0.0.1:8081/event?orgn=17开始 ,结果状态 200 返回不是我的测试的 http 请求。http://127.0.0.1:8081/event?table_token=11&device_id=111111

结果我的测试失败了。是否可以准确地为我的测试返回 http 响应代码 = 200 ?

4

1 回答 1

0

我找到了解决方案(感谢@Blundell)

  @Test
    fun click_checkRequest()() {
        //stub response
        // first request "/event?orgn=17"  - polling
        mockServer.enqueue(MockResponse().setResponseCode(200))
        // second request "/event?table_token="
        mockServer.enqueue(MockResponse().setResponseCode(200))
        waitressCallContainer.click()
        //  confirm that app made the HTTP requests you were expecting
        val firstRequest = mockServer.takeRequest()
        val secondRequest = mockServer.takeRequest()
        Assert.assertEquals("POST", secondRequest.method)
        assertThat(secondRequest.path, CoreMatchers.containsString("/event?table_token"))
        assertThat(
            secondRequest.body.readUtf8(), CoreMatchers.containsString(
                """type":${Type.CALL_WAITRESS.ordinal}"""
            )
        )
    }
于 2019-12-15T15:19:55.350 回答