计数; 找到id = Minitest的用户
我正在关注Michael Hartl的Ruby on Rails教程,我不确定为什么在根据教程应该通过时我会收到此错误:
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 '
我的最小:
要求’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教程进行了比较,看起来相同
用户控制器:
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
。 我的数据库为空,没有用户。 我目前正在测试输入无效参数以便注册不会添加其他用户。
我的UserControllerTest:
要求’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
显示为特定用户呈现页面,因此您需要将其传递给id参数。 将测试更改为:
test "should get show" do user = User.create get :show, id: user.id assert_response :success end
仅供参考,错误消息的细分:
1) Error:
错误
UsersControllerTest#test_should_get_show:
在类UserControllerTest
测试test_should_get_show
ActiveRecord::RecordNotFound: Couldn't find User with 'id'=
数据库不包含具有空id
User
对象
app/controllers/users_controller.rb:7:in `show'
直接导致错误的文件和行
test/controllers/users_controller_test.rb:10:in `block in '
文件和行所源自的行。