As far as I understand the only way to mock structure dependencies in Go is to use interfaces. So my question is: In cases when my struct has methods which perform some actions (when structure is not just a model storing data), should I always define the properties as interfaces in order to properly mock & test it?
Simple example:
type UserService struct {
userRepository UserRepository
}
func (us *UserService) MaleUsers() []User {
all := us.userRepository.FindAll()
maleUsers := []User{}
for _, u := range all {
if u.gender == "male" {
maleUsers = append(maleUsers, u)
}
}
return maleUsers
}
Imagine we have user service which has a dependency: repository. Service has method which gets all users and then filters them by some criteria. By the way filtering logic could go in a separate dependency to avoid filtering in service method (SRP).
Btw. I came from Java world. If this approach of structuring applications is not ideomatic in Go please let me know.