【问题标题】:How can I setup and use factory_girl_rails properly?如何正确设置和使用 factory_girl_rails?
【发布时间】:2012-03-29 14:16:55
【问题描述】:

我认为我在使用 Rails 配置 FactoryGirl 时遇到了问题。我最初关注ASCIIcasts #275: how i test,但 rake 给了我NameError: uninitialized constant ...

我错过了什么吗?是否有可能某些配置文件有误?我对 RSpec 和 Rails 还是很陌生。

我正在使用 Rails 3.2.2 + Mongoid + RSpec + factory_girl_rails。

错误:

Failures:

  1) User should save user with valid required fields
     Failure/Error: let(:user) { FactoryGirl.build(:valid_user) }
     NameError:
       uninitialized constant ValidUser
     # ./spec/models/user_spec.rb:4:in `block (2 levels) in <top (required)>'
     # ./spec/models/user_spec.rb:7:in `block (2 levels) in <top (required)>'

spec/factories.rb

FactoryGirl.define do
  factory :valid_user do
    name     'somename'
    email    'a@b.com'
    password 'somepassword'
  end
end

spec/models/user_spec.rb

require 'spec_helper'

describe User do
  let(:user) { FactoryGirl.build(:valid_user) }

  it "should save user with valid required fields" do
    user.should be_valid
  end
end

spec/spec_helper.rb

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'

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

RSpec.configure do |config|
  config.infer_base_class_for_anonymous_controllers = false

  config.include FactoryGirl::Syntax::Methods
end

【问题讨论】:

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


    【解决方案1】:

    输出整个错误,或者至少输出整个第一句话通常很有帮助——你甚至没有告诉我们缺失的常量是什么!

    更新:感谢整个错误。当您定义工厂 :valid_user 时,Factory Girl 将自动假定它用于名为 ValidUser 的模型。为了解决这个问题,您可以将您的工厂命名为 :user(假设您有一个 User 模型),或者您可以尝试:

    FactoryGirl.define do
      factory :valid_user, :class => User do
        name     'somename'
        email    'a@b.com'
        password 'somepassword'
      end
    end
    

    或者,如果你想拥有几个不同类型的用户工厂,你可以使用:

    FactoryGirl.define do
      factory :user do
        # set some attrs
      end
    
      factory :valid_user, :parent => :user do
        name     'somename'
        email    'a@b.com'
        password 'somepassword'
      end
    
      factory :invalid_user, :parent => :user do
        # some other attrs
      end
    end
    

    【讨论】:

    • 感谢您的回答。我添加了整个错误消息。显然,Factory.build 很快就会被弃用。 (弃用警告:Factory.build 已弃用;请改用 FactoryGirl.build。)
    • 哇,我真傻!我应该想知道工厂女孩应该如何猜测模型!非常感谢。
    【解决方案2】:

    你可以这样声明工厂............

     Factory.define :organization do |g|
       g.name 'Test Organization'
       g.phone_number '5345234561'
       g.website_url 'www.testorg.com'
       g.city 'chichago '
       g.association :state
    
     end
    

    并像这样在组织规范中使用它.....

    require 'spec_helper'
    
     describe Organization do
        before :each do
         @state = Factory :state
         @organization = Factory :organization ,:state => @state
        end
    
      it "should be invalid without a name" do
        @organization.name = nil
        @organization.should_not be_valid
     end
    
    end
    

    享受吧!!!!!!!!!!!!!!!!

    【讨论】:

    • 我完全按照你说的做了,但问题仍然存在。 :( 我认为问题出在我的配置文件上。你能解释一下如何设置 factory_girl_rails 吗?我应该更改哪些配置文件?
    猜你喜欢
    • 2015-04-12
    • 2015-07-20
    • 1970-01-01
    • 2019-02-04
    • 2014-06-11
    • 2023-03-24
    • 2021-01-15
    • 2021-07-10
    • 2014-03-02
    相关资源
    最近更新 更多