【问题标题】:Rpsec and Factory Girl not cooperatingRspec 和 Factory Girl 不合作
【发布时间】:2014-01-12 21:03:24
【问题描述】:

我有点想把头发拉出来。我正在尝试使用 rspec 和 Factory Girl(Ubuntu 13.10 和 Rails 4)进行测试。似乎 Rspec 没有看到任何 Factory Girl 的东西。这是我的 spec_helper.rb:

    # This file is copied to spec/ when you run 'rails generate rspec:install'
    ENV["RAILS_ENV"] ||= 'test'
     require File.expand_path("../../config/environment", __FILE__)
     require 'factory_girl_rails'
     require 'rspec/rails'
     require 'rspec/autorun'
     require 'capybara/rspec'

     Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

     ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

     RSpec.configure do |config|
       config.include FactoryGirl::Syntax::Methods

       config.use_transactional_fixtures = true

       config.infer_base_class_for_anonymous_controllers = false

       config.order = "random"
         config.color_enabled = true

         config.tty = true

         config.formatter = :documentation # :progress, :html, :textmate
     end

factories.rb:

    FactoryGirl.define do
      factory :exercise do
        name 'New Exercise'
        time 3
        reps 7
        weight 12
        weight_unit 'kg'
        factory :aerobic_exercise do
         name 'Jumping Jacks'
          kind 'Cardio/Aerobic'
        end
        factory :bodyweight_exercise do
          name 'Pushups'
          kind 'Bodyweight'
        end
        factory :cardio_exercise do
          name 'Jumping Jacks'
          kind 'Cardio/Aerobic'
        end
        factory :lifting_exercise do
          name 'BB Shoulder Presses'
          kind 'Weight Lifting'
        end
      end
    end

还有我失败的规范:

    #require 'test/spec'
    require 'spec_helper'

    describe Exercise do
      describe 'Exercise properly normalizes values' do
        it 'has weight and weight unit if kind is Weight Lifting' do
          let(:exercise) { FactoryGirl.create(:lifting_exercise) }
         exercise.should be_weight
          exercise.time.should be_nil
        end
        it 'has time but not weight etc. if kind is Cardio' do
          let(:exercise) { FactoryGirl.create(:aerobic_exercise) }
          exercise.should be_time
          exercise.reps.should be_nil
        end
      end
    end

当我运行 rspec 我得到这个错误:

         Failure/Error: let(:exercise) { FactoryGirl.create(:lifting_exercise) }
  NoMethodError:
    undefined method `let' for #    <RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007f2f013b3760>

帮助!(请)

【问题讨论】:

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


    【解决方案1】:

    您的问题不是 RSpec 和 FactoryGirl 不能很好地配合使用。

    let 不属于示例的方法组。请注意,let 位于 it 块内。这应该工作

    #require 'test/spec'
    require 'spec_helper'
    
    describe Exercise do
    
      describe 'Exercise properly normalizes values' do
    
        context 'with a lifting exercise' do
          let(:exercise) { FactoryGirl.create(:lifting_exercise) }
    
          it 'has weight and weight unit if kind is Weight Lifting' do
            exercise.should be_weight
            exercise.time.should be_nil
          end
    
        end
    
        context 'with an aerobic exercise' do
          let(:exercise) { FactoryGirl.create(:aerobic_exercise) }
    
          it 'has time but not weight etc. if kind is Cardio' do
            exercise.should be_time
            exercise.reps.should be_nil
          end
    
        end        
    
      end
    
    end
    

    注意 context 只是describe 的别名。

    【讨论】:

      【解决方案2】:

      let 方法不是来自 FactoryGirl,它来自 Rspec,问题是 let 不应该嵌套在 it 中,它应该在它之外使用。

      鉴于您编写它的方式,我认为您应该只使用这样的局部变量:

      describe Exercise do
        describe 'Exercise properly normalizes values' do
          it 'has weight and weight unit if kind is Weight Lifting' do
            exercise = FactoryGirl.create(:lifting_exercise)
            exercise.should be_weight
            exercise.time.should be_nil
          end
          it 'has time but not weight etc. if kind is Cardio' do
            exercise = FactoryGirl.create(:aerobic_exercise)
            exercise.should be_time
            exercise.reps.should be_nil
          end
        end
      end
      

      此外,鉴于您已将 FactoryGirl::Syntax::Methods 包含在您的 spec_helper 中,您无需在所有内容前加上 FactoryGirl,您可以这样称呼它:

      exercise = create(:lifting_exercise)

      希望对你有帮助!

      -乍得

      【讨论】:

      • 非常感谢。我一开始就使用了与此基本相同的东西,只是我认为我使用了“Factory.create”而不是“FactoryGirl.create”(我想我是从一些过时的教程中学到的??)并且只将其更改为“让' 在我谷歌搜索的东西之后的语法似乎暗示了它。我想我误解了,你的解决方案解决了这个问题。再次感谢!
      • let(:exercise) { FactoryGirl.create(:lifting_exercise) } 如果你把它放在所有 it 方法之前就可以工作
      猜你喜欢
      • 2012-06-12
      • 1970-01-01
      • 2012-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多