lundi 21 mars 2016

Is it acceptable to change my code in order to ease testing?

Is it okay to slightly change already written logic in order to ease the testing process, when doing TDD?

Here is an example:

public void onLoginClicked() {
    boolean doLogin = true;
    String userName = view.getUserName();
    if (userName.isEmpty()) {
        view.setEmailFieldErrorMessage();
        doLogin = doLogin ? false : doLogin;
    }

    String password = view.getPassword();
    if (password.isEmpty()) {
        view.setPasswordFieldErrorMessage();
        doLogin = doLogin ? false : doLogin;
    }

    if (!doLogin) {
        return;
    }
    view.setLoginFormDisabled();
    view.setProgressBarVisible();

    attemptLogin(userName, password);
}

Would it be okay if I promoted doLogin to a member variable so that I can assert if its value? There is no way to assert if a method has been invoked in Android unit testing as far as I know (other than Mockito, but I have a different problem), so this would be my only way of knowing if attemptLogin has been invoked.

Another way would be to change the return value from void to boolean but again that would be changing logic in order to serve ease of testing

Aucun commentaire:

Enregistrer un commentaire