嵌套模型测试:找不到表'*'错误. [英] Nested models testing : Could not find table '*' Error.

查看:60
本文介绍了嵌套模型测试:找不到表'*'错误.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

模型概述:

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

The 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'

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

Any feedback is highly welcome, Thanks.

推荐答案

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

这篇关于嵌套模型测试:找不到表'*'错误.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆