问题描述
我正在关注迈克尔·哈特尔(Michael Hartl)的Ruby on Rails教程,但不确定根据该教程应通过的一切时为什么会出现此错误:
I am following Michael Hartl's Ruby on Rails tutorial and I am not sure why I am getting this Error when according to the tutorial everything should pass:
1) Error:
UsersControllerTest#test_should_get_show:
ActiveRecord::RecordNotFound: Couldn't find User with 'id'=
app/controllers/users_controller.rb:7:in `show'
test/controllers/users_controller_test.rb:10:in `block in <class:UsersControllerTest>'
我的最小测试:
需要"test_helper"
require 'test_helper'
class UsersSignupTest < ActionDispatch::IntegrationTest
# add invalid information and test that the User.count never changes
# also test that the sign up path is visited after invalid sign up
test "invalid signup information" do
# visit the signup path using get
get signup_path
assert_no_difference "User.count" do
post users_path, user: { name: "", email: "user@invalid", password: "foo", password_confirmation: "bar"}
end
assert_template "users/new"
end
end
我将我的users_controller与官方的github教程进行了比较,它看起来相同
I compared my users_controller to the official github tutorial and it looks the same
用户控制器:
class UsersController < ApplicationController
def new
@user = User.new
end
def show
@user = User.find(params[:id])
end
def create
# strong parameters
@user = User.new(user_params)
if @user.save
# handle save
else
render 'new'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
end
我真的不明白为什么还要搜索id
.我的数据库是空的,没有用户.我目前正在测试输入无效的注册参数不会添加其他用户.
I dont really understand why id
is being searched for as well. My database is empty with no users. I am currently testing that inputing invalid parameters for sign up will not add another user.
我的UserControllerTest:
my UserControllerTest:
需要"test_helper"
require 'test_helper'
class UsersControllerTest < ActionController::TestCase
test "should get new" do
get :new
assert_response :success
end
test "should get show" do
get :show
assert_response :success
end
end
推荐答案
Show呈现特定用户的页面,因此您需要向其传递id参数.将测试更改为:
Show renders a page for specific user, so you need to pass it the id param. Change the test to:
test "should get show" do
user = User.create
get :show, id: user.id
assert_response :success
end
仅供参考,错误消息的细目分类:
FYI, A small breakdown of the error message:
1) Error:
错误
UsersControllerTest#test_should_get_show:
在测试test_should_get_show
中的类UserControllerTest
ActiveRecord::RecordNotFound: Couldn't find User with 'id'=
数据库不包含带有空id
app/controllers/users_controller.rb:7:in `show'
直接导致错误的文件和行
File and line that directly caused the error
test/controllers/users_controller_test.rb:10:in `block in <class:UsersControllerTest>'
操作源自的文件和行.
这篇关于数数;查找id = Minitest的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!