如何使用 Mongoid 修复此 RSpec 错误? “无法将符号转换为整数”
我正在为我的控制器编写测试。它们很简单,但是这个错误一直弹出。这是我的控制器
def show
id=params[:id]
@user=User.find(:first,id)
end
我的测试
before(:each) do
@user = Fabricate(:user)
sign_in @user
end
...
it "should be successful" do
get "show", :id => @user
response.should be_success
end
和错误消息
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)>'
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)>'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的控制器就是错误所在。 find 方法仅自动返回第一个结果(在代码中相当于
User.where(:id => params[:id]).first
)。尝试删除 :first 符号并简单地传入 id (User.find(id)
)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)
)您的问题可能与
@user
有关,从您发布的示例中不清楚其在规范上下文中的值。您应该传递一个整数记录 id 作为 的值get 的 params 参数,例如:id => 1.
.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
.