Answer the question
In order to leave comments, you need to log in
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}));
}
}
db.save
with 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
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 questionAsk a Question
731 491 924 answers to any question