【问题标题】:FactoryGirl associations, controller attributes, extraneous instancesFactoryGirl 关联、控制器属性、无关实例
【发布时间】:2013-07-14 18:05:50
【问题描述】:

我一直试图让 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)>'

【问题讨论】:

    标签: ruby-on-rails rspec controller factory-bot rspec-rails


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

      或者当你想用的时候你可以写:

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

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多