简体   繁体   中英

How to test controller with Rspec - #show action

I have a Batch model that belongs_to a User. User should only see their own Batches instances.

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'm not sure how to test this behaviour in the show action. Here is 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'm getting the following failures:

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>
     # ./spec/controllers/batches_controller_spec.rb:66:in `block (3 levels) in <top (required)>'

  2) BatchesController GET show/:id should show batches from user
     Failure/Error: batch = subject.current_user.batches.create(batch_params)
     NoMethodError:
       undefined method `stringify_keys' for #<Batch:0x00000005d0ef80>
     # ./spec/controllers/batches_controller_spec.rb:58:in `block (3 levels) in <top (required)>'

What am I doing wrong? How should I test this behaviour of view action?

get :show, id: batch.id

将不会重定向它将呈现显示,因此响应代码200,您可以检查

response.should render_template :show 

The first failure, "should not show batches from other users," looks like it might reflect an actual problem in your controller. Did you also test this in the browser to confirm the problem is in your test and not in the actual code?

On the other test, I'm not quite sure what the problem is exactly, but I would probably build the batch like this instead:

batch = FactoryGirl.create(:batch, user: subject.current_user)

Give that a try and see if it doesn't resolve it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM