|
| 1 | +# Setting up associations with the correct data |
| 2 | + |
| 3 | +You cannot access associations directly from Cypress like you can do with ruby tests. |
| 4 | +So setting up associations has to be done differently from within Cypress. |
| 5 | + |
| 6 | +There are a few ways you can setup associations with the correct data using Cypress and FactoryBot. |
| 7 | +1. Setting the foreign keys |
| 8 | +2. Using transient attributes |
| 9 | +3. Using Nested Attributes |
| 10 | +4. Combination of the above depending on your situation |
| 11 | + |
| 12 | +Assuming you have the following models |
| 13 | + |
| 14 | +```rb |
| 15 | +class Post < ApplicationRecord |
| 16 | + belongs_to :author |
| 17 | + accepts_nested_attributes_for :author |
| 18 | +end |
| 19 | + |
| 20 | +class Author < ApplicationRecord |
| 21 | + has_many :posts |
| 22 | + accepts_nested_attributes_for :posts |
| 23 | +end |
| 24 | +``` |
| 25 | + |
| 26 | +You can do the following: |
| 27 | + |
| 28 | +## 1. Setting the foreign keys |
| 29 | + |
| 30 | +factories.rb |
| 31 | +```rb |
| 32 | +FactoryBot.define do |
| 33 | + factory :author do |
| 34 | + name { 'Taylor' } |
| 35 | + end |
| 36 | + |
| 37 | + factory :post do |
| 38 | + title { 'Cypress on Rails is Awesome' } |
| 39 | + author_id { create(:author).id } |
| 40 | + end |
| 41 | +end |
| 42 | +``` |
| 43 | + |
| 44 | +then in Cypress |
| 45 | +```js |
| 46 | +// example with overriding the defaults |
| 47 | +cy.appFactories([['create', 'author', { name: 'James' }]]).then((records) => { |
| 48 | + cy.appFactories([['create', 'post', { title: 'Cypress is cool', author_id: records[0].id }]] |
| 49 | +}); |
| 50 | + |
| 51 | +// example without overriding anything |
| 52 | +cy.appFactories([['create', 'author']]).then((records) => { |
| 53 | + cy.appFactories([['create', 'post', { author_id: records[0].id }]] |
| 54 | +}); |
| 55 | +``` |
| 56 | +
|
| 57 | +## 2. Using transient attributes |
| 58 | +
|
| 59 | +```rb |
| 60 | +FactoryBot.define do |
| 61 | + factory :author do |
| 62 | + name { 'Taylor' } |
| 63 | + end |
| 64 | + |
| 65 | + factory :post do |
| 66 | + transient do |
| 67 | + author_name { 'Taylor' } |
| 68 | + end |
| 69 | + title { 'Cypress on Rails is Awesome' } |
| 70 | + author { create(:author, name: author_name ) } |
| 71 | + end |
| 72 | +end |
| 73 | +``` |
| 74 | +
|
| 75 | +then in Cypress |
| 76 | +```js |
| 77 | +// example with overriding the defaults |
| 78 | +cy.appFactories([['create', 'post', { title: 'Cypress is cool', author_name: 'James' }]] |
| 79 | + |
| 80 | +// example without overriding |
| 81 | +cy.appFactories([['create', 'post']] |
| 82 | +``` |
| 83 | +
|
| 84 | +## 3. Using Nested Attributes |
| 85 | +
|
| 86 | +```rb |
| 87 | +FactoryBot.define do |
| 88 | + factory :author do |
| 89 | + name { 'Taylor' } |
| 90 | + end |
| 91 | + |
| 92 | + factory :post do |
| 93 | + title { 'Cypress on Rails is Awesome' } |
| 94 | + author_attributes { { name: 'Taylor' } } |
| 95 | + end |
| 96 | +end |
| 97 | +``` |
| 98 | +
|
| 99 | +then in Cypress |
| 100 | +```js |
| 101 | +// example with overriding the defaults |
| 102 | +cy.appFactories([['create', 'post', { title: 'Cypress is cool', author_attributes: { name: 'James' } }]] |
| 103 | + |
| 104 | +// example without overriding |
| 105 | +cy.appFactories([['create', 'post']] |
| 106 | + |
| 107 | +// example of creating author with multiple posts |
| 108 | +cy.appFactories([['create', 'author', { name: 'James', posts_attributes: [{ name: 'Cypress is cool' }, {name: 'Rails is awesome' }] ]] |
| 109 | +``` |
0 commit comments