Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Study Notes-- Rspec+factory_girl for complex Model testing

Shulou Source: shulou.com Published: 2022-06-03 04:25:35 11月25日 Update

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

Tags: Model node test association one-to-one code way structure complex good key keyword action reference leaf situation data database organization data Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Docker Huawei macOS vpn