How to fix this RSpec error with Mongoid? "can't convert Symbol into Integer"
I'm writing tests for my controller. They are very simple, but this error has kept popping up. This is my controller
def show
id=params[:id]
@user=User.find(:first,id)
end
My test
before(:each) do
@user = Fabricate(:user)
sign_in @user
end
...
it "should be successful" do
开发者_运维百科get "show", :id => @user
response.should be_success
end
And the error message
1) UsersController GET 'show' for the logged in user should be successful
Failure/Error: get "show", :id => @user
TypeError:
can't convert Symbol into Integer
# ./app/controllers/users_controller.rb:6:in `show'
# ./spec/controllers/users_controller_spec.rb:31:in `block (4 levels) in <top (required)>'
your controller is where the mistake is. The find method automatically only returns the first result (it is equivalent in code to User.where(:id => params[:id]).first
). Try removing the :first symbol and simply pass in id (User.find(id)
)
get "show", :id => @user
Your problem here is likely with @user
, whose value in the context of your spec is not clear from the example you've posted. You should be passing an integer record id as the value for the params argument to get, for example :id => 1
.
精彩评论