如何使用 Mongoid 修复此 RSpec 错误? “无法将符号转换为整数”

发布于 2024-11-02 08:30:18 字数 735 浏览 0 评论 0原文

我正在为我的控制器编写测试。它们很简单,但是这个错误一直弹出。这是我的控制器

  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你的呼吸 2024-11-09 08:30:18

你的控制器就是错误所在。 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))

心欲静而疯不止 2024-11-09 08:30:18

获取“显示”,:id => @用户

您的问题可能与 @user 有关,从您发布的示例中不清楚其在规范上下文中的值。您应该传递一个整数记录 id 作为 的值get 的 params 参数,例如 :id => 1..

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文