Last active
August 3, 2020 22:43
-
-
Save mathias21/0c7f30b208267ebf362011d3d9b7b7bc to your computer and use it in GitHub Desktop.
KtorEasy controller testing: controller's testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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