FactoryGirl使用

 

FactoryGirl源代码

https://github.com/thoughtbot/factory_girl_rails

 

factory_girl 

factory_girl is a fixtures replacement with a straightforward definition syntax, support for multiple build strategies (saved instances, unsaved instances, attribute hashes, and stubbed objects), and support for multiple factories for the same class (user,admin_user, and so on), including factory inheritance.

Rails

factory_girl_rails provides Rails integration for factory_girl. All Rails-specific features are only compatible with Rails 3.

Currently, automatic factory definition loading is the only Rails-specific feature.

Download

Github: http://github.com/thoughtbot/factory_girl_rails/tree/master

Gem:

gem install factory_girl_rails 

Configuration

Add factory_girl_rails to your Gemfile:

gem 'factory_girl_rails' 

Generators for factories will automatically substitute fixture (and maybe any other fixture_replacement you set). If you want disable this feature, add the following to your application.rb file:

config.generators do |g| g.factory_girl false end 

Default factories directory is test/factories, or spec/factories if test_framework generator is set to :rspec; change this behavior with:

config.generators do |g| g.factory_girl dir: 'custom/dir/for/factories' end 

If you use factory_girl for fixture replacement, ensure that factory_girl_rails is available in the development group. If it's not, Rails will generate standard yml files instead of factory files.

factory_girl takes an option suffix: 'some_suffix' to generate factories as "modelname_some_suffix.rb"

 

例子:

 

修改Gemfile,添加factory-girl-rails

group :test do 
  gem 'capybara','1.1.2'
  gem 'factory_girl_rails','4.1.0'
end

执行bundle install进行安装

 

新建spec/factories.rb,添加如下内容

FactoryGirl.define do 
  factory :user do
    name "Michael Roshen"
    email "[email protected]"
    password "foobar"
    password_confirmation "foobar"
  end
end

 

let(:user) { FactoryGirl.create(:user) } 定义user

 

spec/request/user_pages_spec.rb

require 'spec_helper'

describe "UserPages" do
  subject { page }
  describe "signup page" do
    before { visit signup_path }
    it { should have_selector('h1', text: "Sign Up") }
    it { should have_selector("title", text: full_title("Sign Up"))}
  end

  describe "profile page" do
    let(:user) { FactoryGirl.create(:user)}
    before { visit user_path(user)}
    it { should have_selector('h1', text: user.name)}
    it { should have_selector('title', text: user.name)}
  end

end

 

Rspec默认使用fixture,如果使用factorygril,则需要将默认配置注释掉

 

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"

end

你可能感兴趣的:(let,factorygirl)