Skip to content

Instantly share code, notes, and snippets.

@mathias21
Last active August 3, 2020 22:43
Show Gist options
  • Save mathias21/0c7f30b208267ebf362011d3d9b7b7bc to your computer and use it in GitHub Desktop.
Save mathias21/0c7f30b208267ebf362011d3d9b7b7bc to your computer and use it in GitHub Desktop.
KtorEasy controller testing: controller's testing
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class CreateUserTest : BaseControllerTest() {
private val userApi: UserApi = mockk()
private val controller: RegistrationController by lazy { RegistrationControllerImp() }
private val userId = 11
init {
startInjection(
module {
single(override = true) { userApi }
}
)
}
@BeforeEach
override fun before() {
super.before()
clearMocks(userApi)
}
@Test
fun `when creating user with correct information and user not taken, we return a valid ResponseUser`() {
val postUser = givenAValidPostUserBody()
val createdUser = givenAnUser(userId)
// Arrange
coEvery { userApi.getUserByUsername(any()) } returns null
coEvery { userApi.createUser(any()) } returns createdUser
runBlocking {
// call to subject under test (Act)
val responseUser = controller.createUser(postUser)
// Assert
assertThat(responseUser.id).isEqualTo(userId)
assertThat(responseUser.name).isEqualTo(postUser.name)
assertThat(responseUser.secondname).isEqualTo(postUser.secondname)
assertThat(responseUser.username).isEqualTo(createdUser.username)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment