红宝石导轨功能测试问题

时间:2010-08-27 21:28:14

标签: ruby-on-rails ruby unit-testing

我的功能测试遇到以下问题。

mrbernz:mylife bernardleung $ ruby​​ test / functional / forums_controller_test.rb .....

1)错误: test_should_create_forum(ForumsControllerTest): ActiveRecord的:: StatementInvalid:Mysql的:: BadFieldError:在 '字段列表': '3名ID':未知列INSERT INTO rolesidid:3 name)VALUES(707834473,“主持人“)

错误的堆栈跟踪指向如下......

/Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in gem_original_require' /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in require' /Library/Ruby/Gems/1.8/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:158:in require' /Library/Ruby/Gems/1.8/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:265:in require_or_load' /Library/Ruby/Gems/1.8/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:224:in depend_on' /Library/Ruby/Gems/1.8/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:136:in require_dependency' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/fixtures.rb:853:in try_to_load_dependency' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/fixtures.rb:868:in require_fixture_classes' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/fixtures.rb:865:in each' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/fixtures.rb:865:in require_fixture_classes' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/fixtures.rb:848:in fixtures' ./test/functional/../test_helper.rb:35 test/functional/forums_controller_test.rb:1:in require' 测试/功能/ forums_controller_test.rb:1 无法加载roles_user,底层导致无法加载此类文件 - roles_user

/Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in gem_original_require' /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in require' /Library/Ruby/Gems/1.8/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:158:in require' /Library/Ruby/Gems/1.8/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:265:in require_or_load' /Library/Ruby/Gems/1.8/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:224:in depend_on' /Library/Ruby/Gems/1.8/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:136:in require_dependency' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/fixtures.rb:853:in try_to_load_dependency' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/fixtures.rb:868:in require_fixture_classes' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/fixtures.rb:865:in each' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/fixtures.rb:865:in require_fixture_classes' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/fixtures.rb:848:in`fixtures' 测试/功能/ forums_controller_test.rb:4

我的forums_controller_test.rb看起来如下(最后几行)

require File.dirname(__FILE__) + '/../test_helper'

class ForumsControllerTest < ActionController::TestCase
  fixtures :forums, :users, :roles, :roles_users

  test "should get index" do
    get :index
    assert_response :success
    assert_not_nil assigns(:forums)
  end

我不明白为什么它抱怨'roles_user',因为我没有在任何地方要求它,我要求或声明的是'roles_users',这是我有的表格!

mysql> show tables;
+-----------------------+
| Tables_in_mylife_test |
+-----------------------+
| articles              |
| categories            |
| forums                |
| pages                 |
| posts                 |
| roles                 |
| roles_users           |
| schema_migrations     |
| topics                |
| users                 |
+-----------------------+
10 rows in set (0.15 sec)

帮助?!

1 个答案:

答案 0 :(得分:0)

从ForumsControllerTest中的fixture符号声明中删除:roles_users。 通过指定:roles_users作为fixture它正在尝试加载一个名为RolesUser的模型。我假设它是一个has_and_belongs_to_many连接表。

确保您的users.yml灯具具有:

user:
 roles: moderator

并且您的roles.yml fixture具有:

moderator:
 name: 'Moderator'

并且iduser.yml文件中未设置roles.yml个值