2017-11-25 4 views
0

私は、デフォルトのレールテストフレームワークを使っていくつかの基本的なRailsテストコードを書こうとしています。Ruby on rail testing - テーブルXにはYという名前のカラムがありません

require 'test_helper' 

class ForumsControllerTest < ActionController::TestCase 
test "index should be success" do 
    get :index 
    assert_response :success 
end 
end 
forums_controller_test.rb

:私のアプリケーションは(すなわちスレッド)コントローラ、ここではそれは私がフォーラムをテストしようとしていますなどのコメント、ユーザーがスレッドを投稿することができ、簡単なフォーラムのために

です私は器具を使用していますが、このチュートリアルで、次のい

:ここ https://www.youtube.com/watch?v=0wIta0fITzc

はすべての私のフィクスチャファイルは、次のとおりです。

comments.yml

comm_one: 
    body: MyText 
    forum_id: 1 

comm_two: 
    body: MyText 
    forum_id: 2 

for_one: 
    title: MyString 
    body: MyText 
    user_id: 1 

for_two: 
    title: MyString 
    body: MyText 
    user_id: 2 

forums.yml users.yml

user_one: 
    user_id: '1' 

user_two: 
    user_id: '2' 

は私が午前問題は、私はターミナルでrakeを実行すると、私はこのエラーを取得することです:

Error: ForumsControllerTest#test_index_should_be_success: ActiveRecord::Fixture::FixtureError: table "users" has no column named "user_id".

マイグレーションファイルを表示する必要があるかどうかわかりませんが、その他の情報が必要な場合はお知らせください。

アドバイスをいただき、ありがとうございます。

ありがとうございます。

注:私はユーザー認証にdevise gemを使用しています。これは、ユーザーテーブルも生成するために使用されました。

答えて

1

私はテーブルの構造に問題があると思うが、ユーザーがuser_id列を持たず、別のモデルが1つのユーザーを所有しているか、Userに属していれば、そのモデルはuser_id列をクリックして対応するユーザーを取得します。 ドキュメントを見て、データベースに必要な構造を理解してください。http://guides.rubyonrails.org/association_basics.html

+1

おかげで、私は、変更IDにUSER_IDと、この問題を解決するように私を導いた(他のいくつかの問題を解決した後) – User59

1

私は、user_idをidだけに置き換えるというTisamuのアドバイスに従って、まず問題を解決することができました。次に、ユーザーのメールにユーザーのメールを追加して、そうするようにしました。

user_one: 
    id: 1 
    email: '[email protected]' 

user_two: 
    id: 2 
    email: '[email protected]' 

これでコードの問題は解決しました。しかし、私は、これを言っエラーメッセージました:

Error: 
ForumsControllerTest#test_index_should_be_success: 
ActionView::Template::Error: Devise could not find the `Warden::Proxy` instance on your request environment. 
Make sure that your application is loading Devise and Warden as expected and that the `Warden::Manager` middleware is present in your middleware stack. 
If you are seeing this on one of your tests, ensure that your tests are either executing the Rails middleware stack or that your tests are using the `Devise::Test::ControllerHelpers` module to inject the `request.env['warden']` object for you. 
    app/views/forums/index.html.erb:21:in `block in _app_views_forums_index_html_erb___3974819143402431947_37087120' 
    app/views/forums/index.html.erb:15:in `_app_views_forums_index_html_erb___3974819143402431947_37087120' 
    test/controllers/forums_controller_test.rb:6:in `block in <class:ForumsControllerTest>' 

を私は単にこのようにそれを作るために私のテストファイルにDevise::Test::ControllerHelpersを追加することで、これを固定:

require 'test_helper' 

class ForumsControllerTest < ActionController::TestCase 
include Devise::Test::ControllerHelpers # <-- Have to include this 
test "index should be success" do 
    get :index 
    assert_response :success 
end 
end 
関連する問題