swiftuinavigationcontrollerxctestxctestcase

Unit Testing a view was dismissed in XCTestCase


I have an app using the VIPER pattern.

My LoginViewController is presented, some actions take place and the view is dismissed.

I'd like to assert that when LoginPresenter invokes dismissViewController within my LoginRouter, the view is dismissed.

I have a UITest that covers this behave however I have a code coverage issue within my CI Pipeline and a UITest is not enough to cut it.

I'd like to assert on the behaviour in code.

  import UIKit

final class LoginRouter {

    private var delegate: LoginRouterDelegate?
    private let view: UIViewController

    init(_ delegate: LoginRouterDelegate?, view: UIViewController) {
        self.delegate = delegate
        self.view = view
    }
}

extension LoginRouter: LoginRouterType {

    func dismissViewController() {
        view.dismiss(animated: false, completion: nil)
    }
}

Solution

  • Use the DismissalVerifier from ViewControllerPresentationSpy. On the README description, scroll down to "How do I test dismissing a modal?"