Skip to content

Instantly share code, notes, and snippets.

@iboss-ptk
Created July 8, 2018 13:57
Show Gist options
  • Select an option

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

Select an option

Save iboss-ptk/67d16cf70d4d174fc0d385ca0b7aec3a 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")
val mockMessageCreator = mock<MessageCreator> {
on { create(restClient) } doReturn Message.fromJson(
"""
{
"error_code": $errorCode,
"error_message": "$errorMessage",
"status": "failed"
}
""".trimIndent(), jacksonObjectMapper())
}
// Mock function and stub the invoke method
val creator = mock<MessageCreatorFactory> {
on { invoke(any(), any(), any()) } doReturn mockMessageCreator
}
val result = notificationService.notify(notificationRequest, creator)
it("returns `NotificationFailed` with error code and error message") {
assertThat(result).isEqualTo(NotificationFailed(errorCode, errorMessage))
}
it("calls `creator` with right phoneNumber, messagingServiceId and text") {
// check if `creator` in being invoked with expected arguments
verify(creator).invoke(PhoneNumber("+66839992233"), messagingServiceId, "text")
}
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment