RSpec / Capybara notes
I didn’t pay much attention to testing in rails until recently. I thought that it takes too much time to finish an app and sometimes feels boring.
But recently I decided to make a small task manager web-app and try to fully test it by using RSpec and Capybara. I know about Minitest but don’t like it for some reasons :)
Here’s what I got in the end:
Setup and problems…
So the first this was installing gems
Then, after rails generate rspec:install
I had to configure the generated rspec config files:
spec/spec_helper.rb
Here I only added expectations.syntax = [:should, :expect]
Most of the configuration goes in spec/rails_helper.rb And in most parts it can just be copied from project to project.
I came across a problem where Selenum couldn’t login to the app when I used js: true
. As it turned out the problem was with configuration of Database cleaner.
The following configuration worked just fine.
Also set config.use_transactional_fixtures = false
More info
Testing
At first it was confusing what exactly should I test. But there’s a list of what can actually be tested with different kind of specs at relishapp.com
controller specs, model specs, …
here’s an example for controllers
Since most of the crud controller specs look the same, here’s an example of how controller spec should look like. If it’s not just a crud, it’s still gonna be something similar.
And here’re the other specs of this app: /spec
Helpful resources:
- https://www.relishapp.com/rspec/rspec-rails/docs/
- Instant Rspec Test Driven Development How-To (68 pages book)