Skip to content

Instantly share code, notes, and snippets.

@iboss-ptk
Last active July 8, 2018 05:52
Show Gist options
  • Select an option

  • Save iboss-ptk/f0139c34eb7c243f2a54986071da94a8 to your computer and use it in GitHub Desktop.

Select an option

Save iboss-ptk/f0139c34eb7c243f2a54986071da94a8 to your computer and use it in GitHub Desktop.
class NotificationServiceTest : Spek({
describe(".notify") {
context("when message creation fails") {
// constants
val messagingServiceId = "message-service-id"
val errorCode = 9
val errorMessage = "error message"
val restClient = mock<TwilioRestClient>()
val secretRepository = mock<SecretRepository> {
on { getTwilloMessagingServiceId() } doReturn messagingServiceId
}
val notificationService = NotificationService(restClient, secretRepository)
val notificationRequest = NotificationRequest("+66", "839992233", "text")
// We create a creator that returns a mocked `MessageCreator`
// and now we can determine to behavior of function `create`
fun creator(phoneNumber: PhoneNumber, messagingServiceId: String, text: String): MessageCreator =
mock {
on { create(restClient) } doReturn Message.fromJson(
"""
{
"error_code": $errorCode,
"error_message": "$errorMessage",
"status": "failed"
}
""".trimIndent(), jacksonObjectMapper())
}
// notice that I use `::creator` to refer to `creator` as a value that can be passed
val result = notificationService.notify(notificationRequest, ::creator)
it("returns `NotificationFailed` with error code and error message") {
assertThat(result).isEqualTo(NotificationFailed(errorCode, errorMessage))
}
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment