Say, for instance, I have this simple function in my domain layer:
function canCreateNewUsers (principal: User): boolean {
return principal.isSuperAdmin || principal.isAdmin // || ... a bunch of conditions
}
And this function has already been unit tested in my domain tests.
Now say I have another layer, the service layer, that has a function which creates a user:
// service layer
function createUser (principal: User, email: string) {
// TODO
}
which, in full, would look something such as:
// service layer
function createUser (principal: User, email: string) {
const exists = await userRepository.checkExistsUserWithEmail(email)
if (exists) {
// return Error
}
if (!canCreateNewUsers(principal) {
// return Error
}
const newUser = createUserWithEmail(email)
await userRepository.insertUser(newUser)
return newUser
}
Now, my question is, how would I unit test this service layer function properly? Right now, I am considering two options:
- Stub the
canCreateNewUsers
function.
PROS:
I don't need to make more (duplicate, because canCreateNewUsers
is already tested) tests.
CONS:
I can't think of any major cons, besides having to rename canCreateNewUsers
in the test file, if it ever gets renamed.
Also, isn't it weird to stub domain functions/objects?
- Do not stub the
canCreateNewUsers
.
PROS:
My tests don't care about implementation details.
CONS:
I will end up writing the same tests I did for canCreateNewUsers
, but now for createUsers
. Which is okay, if you consider these two to be different units?
However, if I ever change canCreateNewUsers
(by adding or removing conditions, for example), I will have to mimic the changes in both of the domain and service tests.
What would you guys do in this situation? Thanks.
(Note: I am using fakes for the repository bit)