last day (15 days later) » 

8:51 PM
0
Q: iOS swift unit testing - Test method that calls async method and uses DispatchQueue.main.async

Levan KaranadzeI'm trying to work using TDD (iOS/swift 5). I have the following func: func didTapLogin(userName: String?, password: String?) { loginUseCase.login(request: LoginRequest.init(username: username, password: password)) { [weak self] (reult) in DispatchQueue.main.async { //... ...

 
It isn't exactly clear what you are asking. For testing asynchronous code you would use an expectation and fulfil that expectation when some condition is met.
 
I don't have callback, thus I can't fulfill expectation.
 
Ok, but what are you trying to test? Are you writing UI tests or unit tests? (You have tagged unit test). If you are writing a unit test then your test would typically call loginUseCase.login and you would fulfil the expectation in the completion closure. Since didTapLogin doesn't accept a closure it isn't directly testable.
 
I'm writing unit tests. I want to test something like this: sut.didTapLogin(userName: "bla", password: "bla") XCTAssertTrue(<invalid username is handled>)
 
But what do you gain by calling didTapLogin vs calling loginUseCase directly? As written didTapLogin is untestable since it doesn't expose its results. You can use UI testing to check the state of the UI after the action; for example if you call didTapLogin with an invalid username and password and you expect a certain error message to appear in a label then UI testing framework lets you do that, but if didTapLogin is a UI event handler it wouldn't accept the username and password; it would get those values from the UI state directly.
The first tests you need to write is to call loginUseCase directly and check the expected results in its closure.
 
8:51 PM
I'm using MVP+Clean architecture. didTapLogin is a function in Presenter. Presenter should make call using useCase and also handle the response.
protocol LoginPresenter {
func didTapLogin(userName: String?, password: String?)
func didTapRegister()
func didTapRecoverPassword()
}

protocol LoginView: View {

}

class LoginPresenterImpl: LoginPresenter {

private weak var view: LoginView?
private let router: LoginRouter
private let loginUseCase: LoginUseCase

init(view: LoginView, router: LoginRouter, loginUseCase: LoginUseCase) {
self.view = view
self.router = router
self.loginUseCase = loginUseCase
}

func didTapLogin(userName: String?, password: String?) {
thats my presenter
import UIKit

class LoginViewController: BaseViewController {

var presenter: LoginPresenter!

private let configurator = LoginConfiguratorImpl.init()

override func viewDidLoad() {
super.viewDidLoad()
configurator.configure(for: self)
}

@IBAction private func didTapLoginn(_ sender: Any) {
presenter.didTapLogin(userName: "bal", password: "bla")
}
}

extension LoginViewController: LoginView {

}
ant that's controller
 
You need to use the UI testing framework for that since the result is a change in UI state
Or you can provide a mock view.
 
I have Mock classes
class MockLogiView: LoginView {

var messageIsShown = false
var loaderShown = false

func showMessage(_ messsage: Message) {
messageIsShown = true
}

func showLoader() {
loaderShown = true
}

func hideLoader() {
loaderShown = false
}
}
I have mock for router also
and for usecase to
 
Ok then you can use an expectation and an asyncAfter delay in your test. In the delayed block in your test code you check for the expected state of the mock view and fulfil the expectation
 
Let's forget details of my project. it complicates the question
I am just not sure in asyncAfter
I know that it will give me the result
but is it good solution?
 
what else can you do? The function doesn't provide any any of getting the result directly. The expected behaviour is that after some time the login attempt is complete and the view state is updated accordingly. So your test case has to address that; wait some time and verify that the view is in the correct state.
Why isn't it a good solution?
 

last day (15 days later) »