I have a Batch model that belongs_to User. The user should see only their own instances of the parties.
For the index action, here is what I did:
Batch#index
context "GET index" do it "should get only users batches" do FactoryGirl.create(:batch) batch = FactoryGirl.create(:batch) batch2 = FactoryGirl.create(:batch) subject.current_user.batches << batch get "index" assigns(:batches).should == subject.current_user.batches assigns(:batches).should_not include(batch2) end end
For the create action, here is what I did:
Batch#create
context "POST create" do it "should save a users batch into current_user" do batch = subject.current_user.batches.build(name: 'bla') put :create, batch subject.current_user.batches.should include(batch) end it "should save a batch from other user into current_user" do batch = subject.current_user.batches.build(name: 'bla') batch2 = FactoryGirl.create(:batch) put :create, batch subject.current_user.batches.should_not include(batch2) end end
However, I am not sure how to test this behavior in the show action. That's what I'm doing:
Batch#show
context "GET show/:id" do it "should show batches from user" do batch_params = FactoryGirl.build(:batch) batch = subject.current_user.batches.create(batch_params) get :show, id: batch.id response.should redirect_to(batch) end it "should not show batches from other users" do batch = subject.current_user.batches.create(name: 'bla') batch2 = FactoryGirl.create(:batch) get :show, id: batch2.id response.should redirect_to(:batches) end end
I get the following crashes:
Failures: 1) BatchesController GET show/:id should not show batches from other users Failure/Error: response.should redirect_to(:batches) Expected response to be a <:redirect>, but was <200>
What am I doing wrong? How to check this view action behavior?
source share