使用Factory Girl和Rspec

5

我已经在Rails4应用程序中设置了Rspec,但是测试返回:

Failure/Error: user = Factory(:user)
     NoMethodError:
       undefined method `Factory' for #<RSpec::Core::ExampleGroup::Nested_4::Nested_1:0x007fa08c0d8a98>

看起来我没有正确地引入FactoryGirl。我尝试了几种变化,但是我无法让它工作。

我的Spec Helper:

# 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 'rspec/rails'
require 'rspec/autorun'
require 'factory_girl_rails'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }


# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

RSpec.configure do |config|
  # ## Mock Framework
  #
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
  #
  # config.mock_with :mocha
  # config.mock_with :flexmock
  # config.mock_with :rr

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true

  # If true, the base class of anonymous controllers will be inferred
  # automatically. This will be the default behavior in future versions of
  # rspec-rails.
  config.infer_base_class_for_anonymous_controllers = false

  # Run specs in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by providing
  # the seed, which is printed after each run.
  #     --seed 1234
  config.order = "random"
  config.include Capybara::DSL, :type => :request
  config.include FactoryGirl::Syntax::Methods
end

Gem文件:

group :development, :test do
  gem 'rspec-rails', '~> 2.0'
  gem 'factory_girl_rails', :require => false  # as per sugestion on SO.  Without the require false also fails
  gem "capybara"
end

我的规格:

require 'spec_helper'

describe "Create Event" do
  describe "Log in and create an event" do
    it "Allows creation of individual events" do
      user = Factory(:user)
      visit "/"
    end
  end
end

在/spec/factories/users.rb文件中

FactoryGirl.define do
  factory :user do |f|
    f.email "provider@example.com"
    f.password "password"
  end
end

我该如何开始使用Factory Girl?
2个回答

7

假设您已经包含了:

config.include FactoryGirl::Syntax::Methods

在你的 RSpec 配置块中,我猜你正在寻找的语法应该是:
user = create(:user)

或者:

user = build(:user)

很好。我已经使用FactoryGirl很长时间了,但我不知道有这种新的更短的语法。 - user740584
这很酷。我本来没想到这个,但从现在开始我会用它了。谢谢。 (可惜我不能选两个答案) - Will
user = build(:user) # 返回一个未保存的User实例user = create(:user) # 返回一个已保存的User实例 - FutoRicky

5
使用FactoryGirl创建用户的步骤如下:
user = FactoryGirl.create(:user)

这将使用工厂中定义的所有内容来创建用户。您还可以在同一时间覆盖任何值或指定其他值。例如:

user = FactoryGirl.create(:user, username: 'username', email: 'different-email@example.com)

或者你可以使用FactoryGirl.build(:user, ...),当你想要使用工厂来构建一个实例但不实际将其保存到数据库中时。


1
啊,是的,就是这样。我一直在跟随 Rails Cast,正如评论所指出的,语法已经有些变化了。谢谢 Graeme。http://railscasts.com/episodes/275-how-i-test?view=comments#comment_158161 - Will

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接