I'm mantaining a Rails app.
Controller old code:
@articles = Articles.unpublished.find(complicated_conditions)
I'm changing it to something like this:
if logged_user.has_a_specific_permission
@articles = Articles.unpublished.find(complicated_conditions)
else
@articles = Articles.unpublished.find(another_hash_of_complicated_conditions)
end
I want to write a spec for my change but I don't think testing the conditions is the best way to test the feature (but please correct me if I'm wrong).
So in my spec I wanted to have something like:
login_as(user_with_specific_permission)
permitted = stub('permitted article')
prohibited = stub('prohibited article')
Articles.stub!(:unpublished).and_return([permitted, prohibited])
get :index
assigns[:articles].should eql([permitted])
, to test if the conditions are filtering the results as expected, but this doesn't work because I can't call .find with conditions in an Array.
So what's the way to test this feature?
tl;dr: I want to stub the first part of a method chain but I can't return an Array, and I don't know what I should return.