1

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

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

4

1 回答 1

1

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

于 2013-04-21T01:40:46.803 回答