红宝石轨道测试 - 表X没有列名为Y

红宝石轨道测试 - 表X没有列名为Y

问题描述:

我想写一些基本的Rails测试代码,使用默认的轨道测试框架。我的应用程序是一个简单的论坛,在这里用户可以发布主题,发表评论等红宝石轨道测试 - 表X没有列名为Y

我试图测试论坛(即线程)控制器,那就是:

forums_controller_test.rb

require 'test_helper' 

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

我使用的夹具和我下面这个教程: https://www.youtube.com/watch?v=0wIta0fITzc

这里是我所有的测试数据中:

comments.yml

comm_one: 
    body: MyText 
    forum_id: 1 

comm_two: 
    body: MyText 
    forum_id: 2 

forums.yml

for_one: 
    title: MyString 
    body: MyText 
    user_id: 1 

for_two: 
    title: MyString 
    body: MyText 
    user_id: 2 

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".

不知道您是否需要查看我的迁移文件,但是如果您需要任何其他信息,请告诉我。

我将不胜感激任何意见。

感谢

注:我使用的是色器件宝石为我的用户认证。这也用于生成用户表。

我认为在表中有一个问题,用户不会有一个user_id列,而只是一个id,如果另一个模型有一个用户,或者属于一个用户,那么该模型将有一个user_id列来获取相应的用户。 看看文档,以了解您的数据库中必须具有的结构:http://guides.rubyonrails.org/association_basics.html

+1

谢谢你,我改变了,USER_ID ID,而且这导致我解决了这个问题(解决了其他一些问题之后) – User59

我设法解决这个问题,首先遵循Tisamu关于用id替换user_id的建议。然后我添加用户电子邮件到用户文件,使其如此:

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