Skip to content

Instantly share code, notes, and snippets.

@ShaishavGandhi
Last active December 18, 2017 00:08
Show Gist options
  • Save ShaishavGandhi/1595d23de745f9de8bcd1453f1e2db02 to your computer and use it in GitHub Desktop.
Save ShaishavGandhi/1595d23de745f9de8bcd1453f1e2db02 to your computer and use it in GitHub Desktop.
@Test fun testBlogsReturnsError() {
val testObserver = TestObserver<List<Blog>>()
val path = "/blogs"
// Mock a response with status 200 and sample JSON output
val mockReponse = MockReponse()
.setResponseCode(200)
.throttleBody(1024, 1, TimeUnit.SECONDS) // Simulate SocketTimeout
.setBody(getJson("json/blog/blogs.json"))
// Enqueue request
mockServer.enqueue(mockResponse)
// Call the API
blogRepository.blogs().subscribe(testObserver)
testObserver.awaitTerminalEvent(2, TimeUnit.SECONDS)
// No values
testObserver.assertNoValues()
// One error recorded
assertEquals(1, testObserver.errorCount())
// Get the request that was just made
val request = mockServer.takeRequest()
// Make sure we made the request to the required path
assertEquals(path, request.path)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment