2014-10-22 55 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>' 

文件和行那裏的行動源於。