Skip to content

Commit cbd02dd

Browse files
Test fixtures and install generator updates (#6)
* separate rails examples by group * Add support to load Rails fixtures * Update README
1 parent 87d3a20 commit cbd02dd

File tree

14 files changed

+238
-117
lines changed

14 files changed

+238
-117
lines changed

README.md

Lines changed: 61 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ Do things like:
1212
* use factory_bot to setup data
1313
* create scenario files used for specific tests
1414

15+
Has examples of setting up state with:
16+
* factory_bot
17+
* rails test fixtures
18+
* scenarios
19+
* custom commands
20+
1521
This gem is based off https://github.com/konvenit/cypress-on-rails
1622

1723
## Getting started
@@ -64,66 +70,94 @@ cd spec
6470
yarn run cypress open
6571
```
6672

67-
### Example of using scenarios
68-
69-
Scenarios are named `before` blocks that you can reference in your test.
73+
### Example of using factory bot
74+
You can run your [factory_bot](https://github.com/thoughtbot/factory_bot) directly as well
7075

71-
You define a scenario in the `spec/cypress/app_commands/scenarios` directory:
7276
```ruby
7377
# spec/cypress/app_commands/scenarios/basic.rb
74-
Profile.create name: "Cypress Hill"
78+
require 'cypress_dev/smart_factory_wrapper'
7579

76-
# or if you have factory_bot enabled in your cypress_helper
77-
CypressDev::SmartFactoryWrapper.create(:profile, name: "Cypress Hill")
80+
CypressDev::SmartFactoryWrapper.configure(
81+
always_reload: !Rails.configuration.cache_classes,
82+
factory: FactoryBot,
83+
files: Dir['./spec/factories/**/*.rb']
84+
)
7885
```
7986

80-
Then reference the scenario in your test:
8187
```js
82-
// spec/cypress/integrations/scenario_example_spec.js
88+
// spec/cypress/integrations/simple_spec.js
8389
describe('My First Test', function() {
8490
it('visit root', function() {
8591
// This calls to the backend to prepare the application state
86-
cy.appScenario('basic')
92+
cy.appFactories([
93+
['create_list', 'post', 10],
94+
['create', 'post', {title: 'Hello World'} ]
95+
])
8796

88-
cy.visit('/profiles')
97+
// Visit the application under test
98+
cy.visit('/')
8999

90-
cy.contains("Cypress Hill")
100+
cy.contains("Hello World")
91101
})
92102
})
93103
```
94104

95-
### Example of using factory bot
96-
You can run your [factory_bot](https://github.com/thoughtbot/factory_bot) directly as well
97-
105+
### Example of loading rails test fixtures
98106
```ruby
99-
# spec/cypress/app_commands/scenarios/basic.rb
100-
require 'cypress_dev/smart_factory_wrapper'
107+
# spec/cypress/app_commands/activerecord_fixtures.rb
108+
require "active_record/fixtures"
101109

102-
CypressDev::SmartFactoryWrapper.configure(
103-
always_reload: !Rails.configuration.cache_classes,
104-
factory: FactoryBot,
105-
files: Dir['./spec/factories/**/*.rb']
106-
)
110+
fixtures_dir = ActiveRecord::Tasks::DatabaseTasks.fixtures_path
111+
fixture_files = Dir["#{fixtures_dir}/**/*.yml"].map { |f| f[(fixtures_dir.size + 1)..-5] }
112+
113+
logger.debug "loading fixtures: { dir: #{fixtures_dir}, files: #{fixture_files} }"
114+
ActiveRecord::FixtureSet.reset_cache
115+
ActiveRecord::FixtureSet.create_fixtures(fixtures_dir, fixture_files)
107116
```
108117

109118
```js
110119
// spec/cypress/integrations/simple_spec.js
111120
describe('My First Test', function() {
112121
it('visit root', function() {
113122
// This calls to the backend to prepare the application state
114-
cy.appFactories([
115-
['create_list', 'post', 10],
116-
['create', 'post', {title: 'Hello World'} ]
117-
])
123+
cy.appFixtures()
118124

119-
// The application unter test is available at SERVER_PORT
125+
// Visit the application under test
120126
cy.visit('/')
121127

122128
cy.contains("Hello World")
123129
})
124130
})
125131
```
126132

133+
### Example of using scenarios
134+
135+
Scenarios are named `before` blocks that you can reference in your test.
136+
137+
You define a scenario in the `spec/cypress/app_commands/scenarios` directory:
138+
```ruby
139+
# spec/cypress/app_commands/scenarios/basic.rb
140+
Profile.create name: "Cypress Hill"
141+
142+
# or if you have factory_bot enabled in your cypress_helper
143+
CypressDev::SmartFactoryWrapper.create(:profile, name: "Cypress Hill")
144+
```
145+
146+
Then reference the scenario in your test:
147+
```js
148+
// spec/cypress/integrations/scenario_example_spec.js
149+
describe('My First Test', function() {
150+
it('visit root', function() {
151+
// This calls to the backend to prepare the application state
152+
cy.appScenario('basic')
153+
154+
cy.visit('/profiles')
155+
156+
cy.contains("Cypress Hill")
157+
})
158+
})
159+
```
160+
127161
### Example of using app commands
128162

129163
create a ruby file in `spec/cypress/app_commands` directory:

lib/generators/cypress_dev/install_generator.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,9 @@ def install_cypress
3333
def add_initial_files
3434
template "config/initializers/cypress_dev.rb.erb", "config/initializers/cypress_dev.rb"
3535
copy_file "spec/cypress/cypress_helper.rb", "#{options.cypress_folder}/cypress_helper.rb"
36-
copy_file "spec/cypress/integration/on_rails_spec.js", "#{options.cypress_folder}/integration/on_rails_spec.js"
3736
copy_file "spec/cypress/support/on-rails.js", "#{options.cypress_folder}/support/on-rails.js"
38-
copy_file "spec/cypress/app_commands/scenarios/basic.rb", "#{options.cypress_folder}/app_commands/scenarios/basic.rb"
39-
copy_file "spec/cypress/app_commands/clean.rb", "#{options.cypress_folder}/app_commands/clean.rb"
40-
copy_file "spec/cypress/app_commands/eval.rb", "#{options.cypress_folder}/app_commands/eval.rb"
41-
copy_file "spec/cypress/app_commands/factory_bot.rb", "#{options.cypress_folder}/app_commands/factory_bot.rb"
37+
directory 'spec/cypress/app_commands', "#{options.cypress_folder}/app_commands"
38+
directory 'spec/cypress/integration/rails_examples', "#{options.cypress_folder}/integration/rails_examples"
4239
end
4340

4441
def update_files
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# you can delete this file if you don't use Rails Test Fixtures
2+
3+
fixtures_dir = command_options.try(:[], 'fixtures_dir')
4+
fixture_files = command_options.try(:[], 'fixtures')
5+
6+
if defined?(ActiveRecord)
7+
require "active_record/fixtures"
8+
9+
fixtures_dir ||= ActiveRecord::Tasks::DatabaseTasks.fixtures_path
10+
fixture_files ||= Dir["#{fixtures_dir}/**/*.yml"].map { |f| f[(fixtures_dir.size + 1)..-5] }
11+
12+
logger.debug "loading fixtures: { dir: #{fixtures_dir}, files: #{fixture_files} }"
13+
ActiveRecord::FixtureSet.reset_cache
14+
ActiveRecord::FixtureSet.create_fixtures(fixtures_dir, fixture_files)
15+
else # this else part can be removed
16+
logger.error "Looks like activerecord_fixtures has to be modified to suite your need"
17+
Post.create(title: 'MyCypressFixtures')
18+
Post.create(title: 'MyCypressFixtures2')
19+
Post.create(title: 'MyRailsFixtures')
20+
Post.create(title: 'MyRailsFixtures2')
21+
end

lib/generators/cypress_dev/templates/spec/cypress/integration/on_rails_spec.js

Lines changed: 0 additions & 77 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
describe('Rails Other examples', function() {
2+
it('cypress eval', function() {
3+
cy.app('clean') // have a look at cypress/app_commands/clean.rb
4+
cy.appEval("Post.create(title: 'Hello Eval')")
5+
6+
cy.visit('/')
7+
cy.get('table').find('tbody').should(($tbody) => {
8+
expect($tbody).not.to.contain('Multi Command')
9+
expect($tbody).to.contain('Hello Eval')
10+
})
11+
})
12+
13+
it('runs multiple commands', function() {
14+
cy.appCommands([{ name: 'clean' },
15+
{ name: 'scenarios/basic' },
16+
{ name: 'eval', options: "Post.create(title: 'Multi Command')" }])
17+
cy.visit('/')
18+
19+
cy.get('table').find('tbody').should(($tbody) => {
20+
expect($tbody).not.to.contain('Hello Eval')
21+
expect($tbody).to.contain('Multi Command')
22+
expect($tbody).to.contain('I am a Postman')
23+
})
24+
})
25+
})
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
describe('Rails using factory bot examples', function() {
2+
beforeEach(() => {
3+
cy.app('clean') // have a look at cypress/app_commands/clean.rb
4+
})
5+
6+
it('using single factory bot', function() {
7+
cy.appFactories([
8+
['create', 'post', {title: 'Good bye Mars'} ]
9+
])
10+
cy.visit('/')
11+
cy.get('table').find('tbody').should(($tbody) => {
12+
// clean should of removed these from other tests
13+
expect($tbody).not.to.contain('Hello World')
14+
15+
expect($tbody).to.contain('Good bye Mars')
16+
})
17+
})
18+
19+
it('using multiple factory bot', function() {
20+
cy.appFactories([
21+
['create_list', 'post', 10],
22+
['create', 'post', {title: 'Hello World'} ]
23+
])
24+
cy.visit('/')
25+
cy.get('table').find('tbody').should(($tbody) => {
26+
// clean should of removed these from other tests
27+
expect($tbody).to.contain('Hello World')
28+
expect($tbody).not.to.contain('Good bye Mars')
29+
})
30+
})
31+
})
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
describe('Rails using rails fixtures examples', function() {
2+
beforeEach(() => {
3+
cy.app('clean') // have a look at cypress/app_commands/clean.rb
4+
})
5+
6+
it('loading all fixtures', function() {
7+
cy.appFixtures()
8+
cy.visit('/')
9+
cy.get('table').find('tbody').should(($tbody) => {
10+
expect($tbody).to.contain('MyRailsFixtures')
11+
expect($tbody).to.contain('MyRailsFixtures2')
12+
})
13+
})
14+
15+
it('using single rails fixtures', function() {
16+
cy.appFixtures({fixtures: ['posts']})
17+
cy.visit('/')
18+
cy.get('table').find('tbody').should(($tbody) => {
19+
expect($tbody).to.contain('MyRailsFixtures')
20+
expect($tbody).to.contain('MyRailsFixtures2')
21+
})
22+
})
23+
24+
it('loading another folder of fixtures', function() {
25+
cy.appFixtures({fixtures_dir: 'test/cypress_fixtures' })
26+
cy.visit('/')
27+
cy.get('table').find('tbody').should(($tbody) => {
28+
expect($tbody).to.contain('MyCypressFixtures')
29+
expect($tbody).to.contain('MyCypressFixtures2')
30+
})
31+
})
32+
})
33+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
describe('Rails using scenarios examples', function() {
2+
beforeEach(() => {
3+
cy.app('clean') // have a look at cypress/app_commands/clean.rb
4+
})
5+
6+
it('setup basic scenario', function() {
7+
cy.appScenario('basic')
8+
cy.visit('/')
9+
cy.get('table').find('tbody').should(($tbody) => {
10+
// clean should of removed these from other tests
11+
expect($tbody).not.to.contain('Good bye Mars')
12+
expect($tbody).not.to.contain('Hello World')
13+
14+
expect($tbody).to.contain('I am a Postman')
15+
})
16+
})
17+
18+
// uncomment these if you want to see what happens
19+
// it('example of missing scenario failure', function() {
20+
// cy.appScenario('missing')
21+
// })
22+
//
23+
// it('example of missing app failure', function() {
24+
// cy.app('run_me')
25+
// })
26+
})

lib/generators/cypress_dev/templates/spec/cypress/support/on-rails.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ Cypress.Commands.add('appEval', function (code) {
2525
Cypress.Commands.add('appFactories', function (options) {
2626
cy.app('factory_bot', options)
2727
});
28+
29+
Cypress.Commands.add('appFixtures', function (options) {
30+
cy.app('activerecord_fixtures', options)
31+
});
2832
// CypressDev: end
2933

3034
// The next is optional

0 commit comments

Comments
 (0)