2

I have a class having a void method without arguments and would like to know if that kind of methods is testable with unit tests? If yes then what should be tested?

Context:

I have a viewcontroller and would like to present an actionsheet with a cancel button.

class LoginVC: UIViewController {
    func showLoginError() {
            let alertController: UIAlertController = UIAlertController.init(title: "Erreur", message: "Nous ne sommes pas parvenu à vous connecter, vérifiez vos informations.", preferredStyle: .alert)
            let closeAction = UIAlertAction.init(title: "Ok", style: .cancel, handler: { (action) in
                self.userAddressField.becomeFirstResponder()
            })
            alertController.addAction(closeAction)
            self.present(alertController, animated: true, completion: nil)
        }
}
Blazej SLEBODA
  • 8,936
  • 7
  • 53
  • 93
  • Please add more context. What language / framework are you using ? Why do you thing there is something special testing with XCode ? – glenfant Aug 04 '17 at 08:57

1 Answers1

0

You can test anything that leads to observable behavior or that has effects on objects that you can control.

In other words: most likely, the intention of a void method is to change same state within an object/data store/... so in that sense, you can either:

  • simply check the corresponding state before/after calling the method
  • use some mocking framework to ensure that an expected call on a mock given to the code under test did take place

Ideally, you prefer tests of the first category. For more insights, see here or there. Different languages, but giving the theoretical background that universally applies.

GhostCat
  • 137,827
  • 25
  • 176
  • 248