【问题标题】:Nested models testing : Could not find table '*' Error.嵌套模型测试:找不到表“*”错误。
【发布时间】:2013-04-21 01:25:28
【问题描述】:

我正在尝试针对 working 大型代码库运行 RSpec(我对 Rails 比较陌生),但在这一点上失败了;我敢打赌,这与 FactoryGirl 的定义有关。

模型概述:

class User < ActiveRecord::Base
  # ...
  has_many :friends, :conditions => {:approved => true}
  has_many :friendships, :class_name => "User", :source => :friend, :through => :friends
  # ...

测试方法:

# models/user.rb
def add_friend(user_id, friend_id)
  @friendship = self.friends.new({:user_id => user_id, :friend_id => friend_id})
  return false unless @friendship.save
end

FactoryGirl 工厂:

FactoryGirl.define do
  factory :user, :class => User do |f|
    # ...
  end

  factory :friend, :class => Friend do |f|
    f.user_id     { Faker::Base.regexify(/\d{1,3}/)}
    f.friend_id   { Faker::Base.regexify(/\d{1,3}/)}
    # ...
  end
end

规范:

# specs/models/user_spec.rb
it "Adds friends" do
  @current_user.add_friend(@current_user.id, @friend_1.id).should be_valid
end

错误:

ActiveRecord::StatementInvalid: Could not find table 'friends'

非常欢迎任何反馈,谢谢。

【问题讨论】:

  • 你跑rake db:migrate RAILS_ENV=test了吗?
  • 几率是多少...现在一切都通过了(除了 1 次测试,但这很好)!我想我已经很久没有迁移测试数据库了:)
  • (能否请您添加您的答案,以便我可以将帖子标记为已解决?)
  • 总是小事,对吧?我刚刚添加了答案。 :-)

标签: ruby-on-rails ruby ruby-on-rails-3 rspec factory-bot


【解决方案1】:

您可能需要运行rake db:migrate RAILS_ENV=test

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多