Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Study Notes-- Rspec+factory_girl for complex Model testing

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report