问题
I'm trying to run RSpec against a working
large codebase (I'm relatively new to Rails), but it fails on this point; My bet that it has something to do with the FactoryGirl definitions.
Overview of the model :
class User < ActiveRecord::Base
# ...
has_many :friends, :conditions => {:approved => true}
has_many :friendships, :class_name => "User", :source => :friend, :through => :friends
# ...
The method to test :
# 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
The FactoryGirl factories :
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
The Spec :
# specs/models/user_spec.rb
it "Adds friends" do
@current_user.add_friend(@current_user.id, @friend_1.id).should be_valid
end
The Error :
ActiveRecord::StatementInvalid: Could not find table 'friends'
Any feedback is highly welcome, Thanks.
回答1:
You may need to run rake db:migrate RAILS_ENV=test
.
来源:https://stackoverflow.com/questions/16127062/nested-models-testing-could-not-find-table-error