Ruby5 #3 – Rspec stubbing named_scope in a controller
I found the
stub_chain method very helpful when stubbing out objects for Rspec controller testing.
My index action is calling 2 named_scopes and then doing a find.
def index @exercise_logs = ExerciseLog.this_user(current_user).past.all end
When testing controllers your just trying to stub out a value for @exercise_logs not simulating an actual call to your model. So we'll use the stub_chain method to stub out our named_scopes like this.
it "assigns all exercise_logs as @exercise_logs" do ExerciseLog.stub_chain(:this_user,:past).and_return([mock(ExerciseLog),mock(ExerciseLog)]) get :index response.should be_success end