【问题标题】:Spec tests looking for table names as `module_name_table_name`规范测试寻找表名作为 `module_name_table_name`
【发布时间】:2014-08-21 09:50:36
【问题描述】:

我已经设置了一个 Rails-api 应用程序,其中有一个V1 命名空间之外的用户。 Player 代表 API 的用户。以前,当所有模型都在模块V1 内时,一切都很好。现在,当我运行测试时,我得到:

    V1::PlayersController GET players returns all players
     Failure/Error: FactoryGirl.create :player, user_id: 1
     ActiveRecord::StatementInvalid:
       Could not find table 'v1_players'

这是一个常见的错误吗?我唯一能想到的就是我在迁移中犯了一个错误。它们是标准的,没有提到 v1,所以我无法想象 ActiveRecord 为何期待这个名称。

这是规范,以防有帮助:

describe V1::PlayersController, :type => :controller do 

    describe "GET players" do
        it "returns all players" do 
            FactoryGirl.create :player, user_id: 1
            FactoryGirl.create :player, user_id: 2

            get :index, :format => :json

            expect(response.status).to eq 200

            body = JSON.parse(response.body)
            player_user_ids = body.map { |m| m["user_id"] }

            expect(player_user_ids).to match_array(["1",
                                               "2"])
        end
    end
end

还有Player工厂:

FactoryGirl.define do
  factory :player, class: V1::Player do
    user_id 5
  end
end

谢谢。

【问题讨论】:

    标签: ruby-on-rails rspec


    【解决方案1】:

    根据 ActiveRecord 文档,我认为这是预期的行为。我已经对表进行了迁移以符合要求。我还了解到,您可以使用参数 module_name::class_name 而不仅仅是类名来运行 rails 生成器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-27
      • 2012-02-06
      • 2017-04-09
      相关资源
      最近更新 更多