Nested models testing : Could not find table '*' Error.

依然范特西╮ 提交于 2019-12-13 01:30:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!