0

我一直试图让 FactoryGirl 关联在我的控制器中工作。

保存了许多“想法”实例,这导致“将所有想法分配为@ideas”规范失败。我不确定究竟是为什么。对于此设置,我不希望只有一个实例保留在数据库中吗?如果我一次又一次地运行规范,额外实例的数量每次都会翻倍。我的理解是使用“Idea.create!” 在我的规范中,如下所示将为测试创建一个对象,然后将其从数据库中删除以供下次运行。

我有一个独特的电子邮件序列,使用纳秒来避免重复的电子邮件。使用 FactoryGirl 序列计数器时,我不断收到电子邮件已存在错误,这可能与此问题直接相关。我怎样才能更有效地解决这个问题?

摘自 /controllers/ideas_controller_spec.rb:

  valid_attributes = FactoryGirl.attributes_for(:idea)

  describe "GET index" do
    it "assigns all ideas as @ideas" do
      idea = Idea.create! valid_attributes
      get :index, {}, valid_session
      assigns(:ideas).should eq([idea])
    end
  end

工厂/idea.rb:

FactoryGirl.define do
  factory :idea do
    brief 'Valid brief for an idea'
    phase 1
    active true
    industry 'Technology'
    user
  end
end

工厂/用户.rb

FactoryGirl.define do
  factory :user do
    sequence(:email) {
      |n| time = Time.new
      "person#{time.usec}@example.com"
    }
    password               '12345678'
    password_confirmation  '12345678'
  end
end

规范输出(在运行“bundle exec rake db:test:prepare”后立即):

1) IdeasController GET index assigns all ideas as @ideas
     Failure/Error: assigns(:ideas).should eq([idea])

       expected: [#<Idea id: 6, phase: 1, brief: "Valid brief for an idea", image: nil, active: true, industry: "Technology", created_at: "2013-07-14 17:40:57", updated_at: "2013-07-14 17:40:57", user_id: nil>]
            got: #<ActiveRecord::Relation [#<Idea id: 1, phase: 1, brief: "Valid brief for an idea", image: nil, active: true, industry: "Technology", created_at: "2013-07-14 17:40:57", updated_at: "2013-07-14 17:40:57", user_id: 3>, #<Idea id: 2, phase: 1, brief: "Valid brief for an idea", image: nil, active: true, industry: "Technology", created_at: "2013-07-14 17:40:57", updated_at: "2013-07-14 17:40:57", user_id: 5>, #<Idea id: 3, phase: 1, brief: "Valid brief for an idea", image: nil, active: true, industry: "Technology", created_at: "2013-07-14 17:40:57", updated_at: "2013-07-14 17:40:57", user_id: 9>, #<Idea id: 4, phase: 1, brief: "Valid brief for an idea", image: nil, active: true, industry: "Technology", created_at: "2013-07-14 17:40:57", updated_at: "2013-07-14 17:40:57", user_id: 11>, #<Idea id: 5, phase: 1, brief: "Valid brief for an idea", image: nil, active: true, industry: "Technology", created_at: "2013-07-14 17:40:57", updated_at: "2013-07-14 17:40:57", user_id: 14>, #<Idea id: 6, phase: 1, brief: "Valid brief for an idea", image: nil, active: true, industry: "Technology", created_at: "2013-07-14 17:40:57", updated_at: "2013-07-14 17:40:57", user_id: nil>]>

       (compared using ==)

       Diff:
       @@ -1,2 +1,7 @@
       -[#<Idea id: 6, phase: 1, brief: "Valid brief for an idea", image: nil, active: true, industry: "Technology", created_at: "2013-07-14 17:40:57", updated_at: "2013-07-14 17:40:57", user_id: nil>]
       +[#<Idea id: 1, phase: 1, brief: "Valid brief for an idea", image: nil, active: true, industry: "Technology", created_at: "2013-07-14 17:40:57", updated_at: "2013-07-14 17:40:57", user_id: 3>,
       + #<Idea id: 2, phase: 1, brief: "Valid brief for an idea", image: nil, active: true, industry: "Technology", created_at: "2013-07-14 17:40:57", updated_at: "2013-07-14 17:40:57", user_id: 5>,
       + #<Idea id: 3, phase: 1, brief: "Valid brief for an idea", image: nil, active: true, industry: "Technology", created_at: "2013-07-14 17:40:57", updated_at: "2013-07-14 17:40:57", user_id: 9>,
       + #<Idea id: 4, phase: 1, brief: "Valid brief for an idea", image: nil, active: true, industry: "Technology", created_at: "2013-07-14 17:40:57", updated_at: "2013-07-14 17:40:57", user_id: 11>,
       + #<Idea id: 5, phase: 1, brief: "Valid brief for an idea", image: nil, active: true, industry: "Technology", created_at: "2013-07-14 17:40:57", updated_at: "2013-07-14 17:40:57", user_id: 14>,
       + #<Idea id: 6, phase: 1, brief: "Valid brief for an idea", image: nil, active: true, industry: "Technology", created_at: "2013-07-14 17:40:57", updated_at: "2013-07-14 17:40:57", user_id: nil>]

     # ./spec/controllers/ideas_controller_spec.rb:37:in `block (3 levels) in <top (required)>'
4

2 回答 2

0

我猜你已经为 rspec 关闭了事务,这会导致数据库像你所观察到的那样增长。

请注意,从 Rails 4.0 开始,您还有一个问题,ActiveRecord它返回的是 aRelation而不是 a Array,因此您需要to_a在将其与 a 进行比较之前使用该方法Array

于 2013-07-14T19:59:45.947 回答
0

您实际上并没有真正使用 Factorygirl 序列。序列背后的想法是,每次调用工厂时,变量nin都会增加。sequence(:email) { |n| .. }这意味着使用它就足够了

    sequence(:email) { |n| "person#{n}@example.com" }

或者当你想使用时间时,你可以写:

    email { "person#{Time.new.usec}@example.com" }

这里的方括号意味着里面的代码是在调用工厂时执行的,而不是在没有方括号的情况下执行的更早。

但这只是一个注释,这不应该产生问题。

正如您所说,问题可能是数据库中保留了太多的对象实例。为此,一个简单的解决方案是使用数据库清理器 gem,它会在每个测试用例之后从数据库中删除添加的实例。

于 2013-07-15T19:54:00.363 回答