Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documenting how to setup Factory Associations #100

Merged
merged 5 commits into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## Unreleased

### Tasks
* Documenting how to setup Factory Associations [PR 100](https://github.com/shakacode/cypress-on-rails/pull/100)

## [1.12.0]
[Compare]: https://github.com/shakacode/cypress-on-rails/compare/v1.11.0...v1.12.0

Expand Down
12 changes: 1 addition & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,6 @@ node_modules/.bin/cypress run

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

```ruby
# spec/cypress/app_commands/factory_bot.rb
require 'cypress_on_rails/smart_factory_wrapper'

CypressOnRails::SmartFactoryWrapper.configure(
always_reload: !Rails.configuration.cache_classes,
factory: FactoryBot,
files: Dir['./spec/factories/**/*.rb']
)
```

```js
// spec/cypress/integrations/simple_spec.js
describe('My First Test', function() {
Expand All @@ -166,6 +155,7 @@ describe('My First Test', function() {
})
})
```
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.

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:
```ruby
Expand Down
109 changes: 109 additions & 0 deletions docs/factory_bot_associations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# Setting up associations with the correct data

You cannot access associations directly from Cypress like you can do with ruby tests.
So setting up associations has to be done differently from within Cypress.

There are a few ways you can setup associations with the correct data using Cypress and FactoryBot.
1. Setting the foreign keys
2. Using transient attributes
3. Using Nested Attributes
4. Combination of the above depending on your situation

Assuming you have the following models

```rb
class Post < ApplicationRecord
belongs_to :author
accepts_nested_attributes_for :author
end

class Author < ApplicationRecord
has_many :posts
accepts_nested_attributes_for :posts
end
```

You can do the following:

## 1. Setting the foreign keys

factories.rb
```rb
FactoryBot.define do
factory :author do
name { 'Taylor' }
end

factory :post do
title { 'Cypress on Rails is Awesome' }
author_id { create(:author).id }
end
end
```

then in Cypress
```js
// example with overriding the defaults
cy.appFactories([['create', 'author', { name: 'James' }]]).then((records) => {
cy.appFactories([['create', 'post', { title: 'Cypress is cool', author_id: records[0].id }]]
});

// example without overriding anything
cy.appFactories([['create', 'author']]).then((records) => {
cy.appFactories([['create', 'post', { author_id: records[0].id }]]
});
```

## 2. Using transient attributes

```rb
FactoryBot.define do
factory :author do
name { 'Taylor' }
end

factory :post do
transient do
author_name { 'Taylor' }
end
title { 'Cypress on Rails is Awesome' }
author { create(:author, name: author_name ) }
end
end
```

then in Cypress
```js
// example with overriding the defaults
cy.appFactories([['create', 'post', { title: 'Cypress is cool', author_name: 'James' }]]

// example without overriding
cy.appFactories([['create', 'post']]
```

## 3. Using Nested Attributes

```rb
FactoryBot.define do
factory :author do
name { 'Taylor' }
end

factory :post do
title { 'Cypress on Rails is Awesome' }
author_attributes { { name: 'Taylor' } }
end
end
```

then in Cypress
```js
// example with overriding the defaults
cy.appFactories([['create', 'post', { title: 'Cypress is cool', author_attributes: { name: 'James' } }]]

// example without overriding
cy.appFactories([['create', 'post']]

// example of creating author with multiple posts
cy.appFactories([['create', 'author', { name: 'James', posts_attributes: [{ name: 'Cypress is cool' }, {name: 'Rails is awesome' }] ]]
```