Skip to content

Commit 6e0b9b3

Browse files
Documenting how to setup Factory Associations (#100)
1 parent 69357df commit 6e0b9b3

File tree

3 files changed

+115
-11
lines changed

3 files changed

+115
-11
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## Unreleased
2+
3+
### Tasks
4+
* Documenting how to setup Factory Associations [PR 100](https://github.com/shakacode/cypress-on-rails/pull/100)
5+
16
## [1.12.0]
27
[Compare]: https://github.com/shakacode/cypress-on-rails/compare/v1.11.0...v1.12.0
38

README.md

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,6 @@ node_modules/.bin/cypress run
132132

133133
You can run your [factory_bot](https://github.com/thoughtbot/factory_bot) directly as well
134134

135-
```ruby
136-
# spec/cypress/app_commands/factory_bot.rb
137-
require 'cypress_on_rails/smart_factory_wrapper'
138-
139-
CypressOnRails::SmartFactoryWrapper.configure(
140-
always_reload: !Rails.configuration.cache_classes,
141-
factory: FactoryBot,
142-
files: Dir['./spec/factories/**/*.rb']
143-
)
144-
```
145-
146135
```js
147136
// spec/cypress/integrations/simple_spec.js
148137
describe('My First Test', function() {
@@ -166,6 +155,7 @@ describe('My First Test', function() {
166155
})
167156
})
168157
```
158+
You can check the [association Docs](https://github.com/shakacode/cypress-on-rails/blob/master/docs/factory_bot_associations.md) on more ways to setup association with the correct data.
169159

170160
In some cases, using static Cypress fixtures may not provide sufficient flexibility when mocking HTTP response bodies - it's possible to use `FactoryBot.build` to generate Ruby hashes that can then be used as mock JSON responses:
171161
```ruby

docs/factory_bot_associations.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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

Comments
 (0)