gpt4 book ai didi

ruby-on-rails-3 - ActiveRecord::RecordNotFound:找不到 id= 的用户

转载 作者:行者123 更新时间:2023-11-28 20:20:55 24 4
gpt4 key购买 nike

我正在使用 rspec 测试我的应用程序,我读到我必须在测试环境中创建一个用户,但不知道如何......这是代码:

require 'spec_helper'

describe CarsController do
describe "GET 'new'" do
it "should be successful" do
visit new_user_car_path(:user_id=>"28")#also try (current_user) but nothing
response.should be_success
end
end
end

当我运行它时,我收到了这条消息

Failure/Error: visit new_user_car_path(:user_id=>"28")
ActiveRecord::RecordNotFound:
Couldn't find User with id=28
# ./app/controllers/cars_controller.rb:3:in `new'
# ./spec/controllers/tanking_logs_controller_spec.rb:6:in `block (3 levels) in <top (required)>'

如果您需要更多代码,请告诉我

编辑。这个我试过了

require 'spec_helper'

describe CarsController do
describe "GET 'new'" do
it "should be successful" do
#user = User.create(...)
@user = {:email => "user@example.com", :password => "foobar", :password_confirmation => "foobar" }
visit new_user_car_path(@user)
response.should be_success
end
end
end

现在我得到了这个错误:

 No route matches {:action=>"new", :controller=>"cars", :email=>"user@example.com", :password=>"foobar", :password_confirmation=>"foobar"}

最佳答案

您的测试数据库中没有 ID 为 28 的用户。您需要为您的测试数据库设定种子并使用您已知存在的用户 ID。

或者,按需创建新用户:

describe "GET 'new'" do
it "should be successful" do
user = User.create(...)
visit new_user_car_path(user)#also try (current_user) but nothing
response.should be_success
end
end

关于ruby-on-rails-3 - ActiveRecord::RecordNotFound:找不到 id= 的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11783825/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com