2014-10-22 97 views
0

我关注的迈克尔·哈特尔的Ruby on Rails的教程中,我不知道根据教程的一切时,应传递为什么我收到此错误:伯爵;发现用户使用id = MINITEST

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>' 

我MINITEST:

需要 '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: "[email protected]", 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 
+1

您发布的测试结果很好。一个失败的是'UsersControllerTest'。如果你附上它,我们可以尝试帮助你。很可能你请求的用户路径不正确或缺少身份证。 – makhan 2014-10-22 06:17:21

+0

@makhan我会再看看谢谢! – Liondancer 2014-10-22 06:20:46

+0

@makhan我想我把代码放在项目的错误部分。感谢您指出这一点! – Liondancer 2014-10-22 06:24:46

回答

1

展会呈现针对特定用户的网页,所以你需要通过它的ID PARAM。测试更改为:

test "should get show" do 
    user = User.create 
    get :show, id: user.id 
    assert_response :success 
    end 

FYI,错误消息的一小击穿:

1) Error: 

错误

UsersControllerTest#test_should_get_show: 

在测试test_should_get_showUserControllerTest

ActiveRecord::RecordNotFound: Couldn't find User with 'id'= 

数据库不包含User对象空id

app/controllers/users_controller.rb:7:in `show' 

文件和行直接导致该错误

test/controllers/users_controller_test.rb:10:in `block in <class:UsersControllerTest>' 

文件和行那里的行动源于。