Rails Test six combat unit test

Source: Internet
Author: User
Tags aliases valid

Replace fixtures with Factory-girl to create analog data 2

In the previous article we introduced Factory-girl, which is a good tool that can be used to replace fixtures in rails to generate mock data.

It is intuitive, easy to read, easy to read and easy to maintain. The most important point is that it is oriented to model, business-oriented, application-oriented, and fixtures analog data is database-oriented. But our unit tests, functional tests, and even future integration tests are all business-oriented, testing from a business perspective to test whether the system meets business needs. So the benefits are obvious, I believe that you will have a little touch after use.

In the previous article we introduced some basic uses, creating a single model of the mock object, fill the value of the property.

Factorygirl.define do
  factory:user_valid,: Class =>: User do
    nickname "nickname"
    Email "ee@123.com"
    password "123"
    password_confirmation "123"
  end 
  Factory:user_invalid_password_do_not_match: Class =>: User do
    nickname "nickname2"
    Email "ee2@123.com" password
    "1232"
    Password_ Confirmation "123"
  end 

This simulates two user objects, one valid and one invalid (because the passwords do not match).

Pass

user = Factorygirl.build (: user_valid)

You can access the data from the Factory-girl simulation, and then use the simulated data in unit and functional tests.

But sometimes we have higher requirements, such as our entities are related, has_many,belongs_to,has_and_belongs_to_many and so on. Is it possible to create a model with its associated entities as well? The answer is: yes.

Let's give a simple relationship. Just take my blog item.

Post and category, a post belongs to a category, and a category contains multiple post.

Class Post < activerecord::base 
  belongs_to:category 
End 
class Category < ActiveRecord::Base 
  Has_many:p OSTs End 

We can do this as follows.

Factorygirl.define  do
  factory:category does
    title "category"
  end 
     
  Factory:category_valid,: Class =>:category do
    title "Categorytitle"
  end 
     
Factorygirl.define does
 factory:p Ost_ Valid_with_category1,: Class =>:P ost do
    title "Post"
    slug "slug
    Summary" summary "
    content" Content "
    category 
  End 
     
  factory:p Ost_valid_with_category2,: Class =>:P ost do
    title" POST "
    slug "slug"
    Summary "summary"
    content "content"
    Association:category, factory =>: Category_ Valid 
  End 
     

It shows that we simulate two category, two posts in two ways, and specify the corresponding category in the post.

Build (:p ost_valid_with_category1). category.title= the "category" Build
     
(:p ost_valid_with_category1). Category.title= "Categorytitle"

We can use it like this to access the category mock object of the post mock object.

There is another way to use alias aliases.

Suppose that our user and post and commenter are the following relationships.

Class User < ActiveRecord::Base 
  Attr_accessible:first_name,: last_name 
  has_many:p osts 
End 
     
class Post < activerecord::base 
  attr_accessible:title 
  belongs_to:author,: class_name => "User",: Foreign_key => "author_id"
End 
     
class Comment < activerecord::base 
  attr_accessible:content 
  belongs_to: Commenter,: class_name => "User": Foreign_key => "commenter_id" End

You can create mock data as follows.

Factory:user, aliases: [: Author,: commenter] do
  first_name    "John"
  last_name     "Doe"
End 
     
Factory:p Ost do
  author 
  # instead 
  of # Association:author, Factory:: User 
  title ' How to read a Book effectively '
end 
     
factory:comment do
  commenter 
  # instead 
  of # Association:commenter, Factory:: User 
  Content "Great article!"
End

Using the alias alias, two aliases are given to the user object, one for post and one for comment. aliases correspond to two properties of post and comment, respectively.

This article is from the "It architects in the breakout" blog, please be sure to keep this source http://virusswb.blog.51cto.com/115214/1076695

See more highlights of this column: http://www.bianceng.cn/Programming/project/

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.