The girl is hot who works in a factory (factory_girl in rails instead of fixture)

Source: Internet
Author: User

Factory Girl is a tool dedicated to constructing simulated test data to perfectly replace fixture.
The disadvantage of fixture is obvious. It is often necessary to deal with various associations and dependencies. If the test requires a large amount of data, you must manually create the data yourself. These solutions are available in factory girl. Association can be used to generate a large amount of data to solve the association between objects. sequence can solve the problem of generating a large amount of test data.

Add factory girl
#Gemfilegroup :development, :test do  gem 'factory_girl_rails'end

It is best to modify spec_helper.rb and comment out the fixture_path line to disable fixture.

# /spec/spec_helper.rb# config.fixture_path = "#{::Rails.root}/spec/fixtures"
Factory Girl usage principles

1. Each model defines a factory.
2. If the model is relatively small, put all factory definitions in a file spec/factories. RB.
If multiple models are copied, each model creates a file [model_names] S. Rb in the spec/factories/directory.

Three common expressions in rspec
User = Factory (: User) # equivalent to new + save! User = factory. Create (: User) # Same as above, is full write user = factory. Build (: User,: username => 'camel ') # Only new does not save
Example
# Spec/factories. rbfactorygirl. define do Factory: User do sequence (: email) {| n | "email # {n} @ factory.com"} password "888888" password_confirmation "888888" End Factory: post do title "hello" association: user # Association: Author,: factory =>: user # associates with another user and defines an alias. Endend
Call methods in rspec
post = Factory(:post)

In fact, the above Code is equivalent:

user = User.newuser.email = "test@example.com"user.save!post = Post.newpost.title = "Hello"post.user = userpost.save!

For more information, see: https://github.com/thoughtbot/factory_girl

> Original article. Please reprint it. Reprinted Please note: Reprinted from Ruby fans, thank you!
> Original article URL: summary of how factory girl works

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.