我有一个属于用户的批处理模型.用户应该只能看到自己的Batches实例.
对于索引操作,这是我做的:
批#指数
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
对于创建动作,这是我做的:
批#创建
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
但是,我不确定如何在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
我遇到了以下失败:
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)>'
我究竟做错了什么?我该如何测试视图操作的这种行为?
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。