Scaffold and behold!
Let's scaffold out something to see what we get for free with rails, rspec, and factories
Run your scaffold command
rails g scaffold Cat name:string breed:string bio:string
Look at all the tests that got generated!
new file: spec/controllers/cats_controller_spec.rb
new file: spec/factories/cats.rb
new file: spec/helpers/cats_helper_spec.rb
new file: spec/models/cat_spec.rb
new file: spec/requests/cats_spec.rb
new file: spec/routing/cats_routing_spec.rb
new file: spec/views/cats/edit.html.erb_spec.rb
new file: spec/views/cats/index.html.erb_spec.rb
new file: spec/views/cats/new.html.erb_spec.rb
new file: spec/views/cats/show.html.erb_spec.rb
What's in a scaffold...
We automagically get tests for:
- routing
- requests
- models
- views
- controllers
Run your tests
First run rake db:migrate
, then rspec
or bundle exec rspec
to see our tests run.
rake db:migrate
bundle exec rspec
Check out your console output...
Randomized with seed 19139
........**....***********..
Pending: (Failures listed here are expected and do not affect your suite's status)
1) CatsHelper add some examples to (or delete) /Users/ni_lo/Code/talks/rspec/reading_meow/spec/helpers/cats_helper_spec.rb
# Not yet implemented
# ./spec/helpers/cats_helper_spec.rb:14
# more output from pended/skipped tests ....
Top 10 slowest examples (1.14 seconds, 97.7% of total time):
cats/edit renders the edit cat form
0.56707 seconds ./spec/views/cats/edit.html.erb_spec.rb:12
Cats GET /cats works! (now write some real specs)
0.49378 seconds ./spec/requests/cats_spec.rb:5
# more output about our slowest tests ...
Finished in 1.17 seconds (files took 4.65 seconds to load)
27 examples, 0 failures, 13 pending
Randomized with seed 19139
Clean up
To reverse your scaffolding, you can run...
rails d scaffold Cat name:string breed:string bio:string
Awesome. Now let's actually start building out our app with tests!