首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rpsec与工厂女孩不合作

Rpsec与工厂女孩不合作
EN

Stack Overflow用户
提问于 2013-12-24 16:28:13
回答 2查看 1.1K关注 0票数 0

我有点想把我的头发拔出来。我正在尝试使用rspec和工厂女孩(Ubuntu13.10和Rails 4)进行测试。似乎Rspec没有看到任何工厂女孩的东西。这是我的spec_helper.rb:

代码语言:javascript
复制
    # 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:

代码语言:javascript
复制
    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

而我失败的规范:

代码语言:javascript
复制
    #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时,会得到以下错误:

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

救命!(请)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-12-24 16:40:54

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

考虑到您编写它的方式,我认为您应该使用这样的局部变量:

代码语言:javascript
复制
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中包含了FactoryGirl,您不需要在FactoryGirl中为所有内容加上前缀,您可以这样称呼它:

exercise = create(:lifting_exercise)

我希望这能帮上忙!

-Chad

票数 2
EN

Stack Overflow用户

发布于 2013-12-24 16:41:37

你的问题不是RSpec和FactoryGirl在一起玩得不好。

let不是示例的方法组的一部分。注意,letit块中。这应该能行

代码语言:javascript
复制
#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的别名。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20763960

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档