Study Notes-- Rspec+factory_girl for complex Model testing
This article mainly introduces how to use Rspec+factory_girl to test complex models.
First of all, I introduce the model mechanism class Node that I use.
Belongs_to: parent,:class_name = > Node
Has_many: children,:class_name = > Node
: foreign_key = >: parent_id
End
End can see that I use a self-correlation table. I associate this table by myself: parent_id. Now I introduce how to simulate such a simulation structure with factory_girl. First, I introduce an one-to-one model structure from a leaf node like a root node. The structure code is as follows:
Factory: node do
Title "XXXXX"
Factory: node_leaf,: class = >: node doassociation: parent,:factory = >: node
End
End
With the value of association, we make an one-to-one association between node and node_leaf, and then we build a root node to give an one-to-many FactoryGirl.define do to the leaf node.
Factory: node do
Title "XXXXX"
Factory: node_root,:class = >: node do
After_create do | node |
Node.children