What's the best way to test methods like the following using mocha gem?
def can_create?(user)
opened? && (owner?(user) || member?(user))
end
Is a best practice to test "AND" and "OR" conditions? Should I create many tests to cover all possibilities or one expects options to cover all?
I'm using rails 4 + test unit.
When i have tests only with &&, for instance:
def can_update?(user)
opened? && owner?(user)
end
I'm testing:
group = Group.new
user = User.new
group.expects(:opened?).returns(true)
group.expects(:owner?).returns(true)
assert group.can_update?(user)
The problem is when I have "OR" conditions. With the first method ( can_create? ) I can't do that:
group = Group.new
user = User.new
group.expects(:opened?).returns(true)
group.expects(:owner?).returns(true)
group.expects(:member?).returns(true)
assert group.can_create?(user)
Because ( owner? and member? methods can be true/false ).
My first guess is to write several tests:
Then you can refactor you tests to have something cleaner, so you want to use stubs instead of expects to avoid the OR problem:
after all, it's not so long for this example. If you have lots and lots of conditions, you might want something like Aupajo. But I think you'll end to write your own, since you will have some specifics needs.
regards