'Swift unit test cannot change the data to test the function
I am applying unit test in viewController. Call the viewController and viewModel and try to change value to test different validation. However, when i change the value in unit test, the function "validateSalutation" in viewController cannot print the update value (i can directly change the value inside function, but cannot do it in unit test).
I am wondering how to do a unit test like these..... also the unit test for private method. Please Help
import XCTest
@testable myProject
class RegFormViewControllerTests: XCTestCase {
var formEditviewModel: RegFormViewModel!
var formEditController: RegFormViewController!
override func setUp() {
formEditviewModel = RegFormViewModel()
formEditController = RegFormViewController(
viewModel: formEditviewModel
)
}
func test_isSalutationValid() {
formEditviewModel.salutationInputViewModel.inputValue = "Miss"
// I can print the value "Miss" here, but in the function "validateSalution", i cannot see the updated value
var result = formEditController.validateSalutation()
XCTAssertFalse(result)
}
}
class RegFormViewController: UIViewController {
var formEditviewModel: RegFormViewModel!
func validateSalutation() -> Bool {
var isValid = false
let salutation = formEditviewModel.salutationInputViewModel.inputValue
print(salutation) // it is empty also when i change the value in unit test call this function
if (!salutation.isEmpty) {
isValid = true
}
return isValid
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
