嵌套模型测试:找不到表'*'错误。

时间:2013-04-21 01:25:28

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

我正在尝试针对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'

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

1 个答案:

答案 0 :(得分:1)

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