One common case being the app has users and groups, only a user who is an admin of the group can add/remove users. I might have a controller action that goes “@group.add_member if current_user.is_admin”
It seems testing this behavior should be part of the model unit tests., because it’s a single piece of code responsible for a single task. But I’m not sure how best to include the fact that this method’s correctness depends on the controller. I was thinking:
- One option is to redefine the model method to take an additional param for current_user, but
that seems a bit ugly. - Another option is to test just “add_user” in the model and check ‘is_admin’ behavior in the
controller test instead, though I was thinking this is a model concern. - Yet another option is to have this method be part of the User model
instead of Group, so to be invoked as: “current_user.add_member_to_group” <– sounds like the
responsibility for that functionality should be in the Group model
though.
Thoughts?
Option 2 is fine.
Logic is safe:
the model is responsible for it’s business
the controller is the conductor