How to do it...

  1.  Create a new test class:
class MyTest {
}
  1. Create a mocked instance of the RegistrationApi interface as the test-class property:
class MyTest {
private val api = mock<RegistrationApi>()
}
  1. Add a class property of the RegistrationFormController type:
class MyTest {
private val api = mock<RegistrationApi>()
private var registrationFormController =
RegistrationFormController(api = api)

}

  1. Create the test method to check whether checkIfEmailCanBeRegistered() behaves correctly for an invalid email address occurrence: 
class MyTest {
private val api = mock<RegistrationApi>()
private lateinit var registrationFormController: RegistrationFormController

@Before
fun setup() {
registrationFormController = RegistrationFormController(api = api)
}

@Test
fun `email shouldn't be registered if it's not valid`() {

// given
assertNotNull(registrationFormController)

whenever(api.isEmailAddressAvailable(anyString())) doReturn(true)
// when
registrationFormController.currentEmailAddress = "Hilary"
// then
assertFalse(registrationFormController.checkIfEmailCanBeRegistered())

}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.34.169