U
U
user_of_toster2022-02-09 04:24:52
Software testing
user_of_toster, 2022-02-09 04:24:52

How to write non-brittle unit tests?

Pseudocode:

class UserRepository {
    constructor(private db: ORM) {}

    saveUser(user: Profile) {
        var username = user.username();
        var hashcode = user.getHashCode();
        var bio = user.getBio();
        var birthdate = user.getBirthdate()

        return db.save(new User({username, hashcode, bio, birthdate}));
    }
}

The question is how to test it? Just check that the method is being called db.savewith new User({username, hashcode, bio, birthdate})? I'm afraid that it will turn out to be a useless fragile test that will break when a new User parameter is added. Or is it the norm?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Michael, 2022-02-09
@user_of_toster

The question is should it be tested? Because all this class does is call another method, having previously converted the arguments. In my opinion, this is a classic humble object .
But if you really want to test it, then yes, we have no other option, you need to check that the save method is called (and optionally check that the correct arguments are passed to it). If you check the arguments, then when you add a new field, the test will break - well, it should have broken, right?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question