This is a base line guide to setup a solid rails project in no time. I’m going to install rails, rspec and factory bot. I will use only the latest and the greatest versions on this guide. Lets do this.
Install Rails
The first line will install rails 6 beta and the second will create a rails application, the options are for:
- -T: skips default rails testing framework, I’m going to use Rspec.
- -d postgresql: informs rails to use postgres as database.
Install Rspec
Change the Gemfile, adding the gems on the group development and test, like this:
Then run in the terminal:
The output should look like:
To complete the installation, we have to inform rspec to use capybara so add this line on spec_helper:
Also, I want configure rspec-rails to use chrome headless so we test javascript too.
on spec/rails_helper.rb uncomment that line:
Convenience configurations(optional)
This configurations add some convenience to our work-flow, feel free to change anything.
Better Rspec Output
add this line to .rspec file to get a more readable output:
Configure Generators
I’m basically disabling some tests we don’t need by default so we can create by hand if it is appropriate.
FactoryBot Helpers
Add this line on rails_helper.rb
Generate Binstubs
Finally run this command to generate binstubs for rspec so can run our specs with bin/rspec instead of the verbose bundle exec rspec:
Smoke Test
Lets see if everything works as expected. At moment as I write this rspec-rails hasn’t a generator for system specs on the latest release. So I will do it by hand.
To run the tests:
if everything is fine you should see this screen on your terminal:
FactoryBot
FactoryBot is already installed, but is good to test if everything works as expected, to do this lets create a simple model:
Our simple model spec should look like:
If its working we should see this:
Great factorybot is working.
Shoulda Matchers
As the final step lets install shoulda matchers
Add this to Gemfile
On terminal run:
Add on spec/rails_helper.rb
The last line here is a temporary fix to an issue with rails 6 for more details see
alter spec/models/post_spec.rb to make it look like this:
This test will fail unless we add this validation on post model:
If we run rspec again this should be the output:
Thats conclude our setup. As a last step I will remove the post model used for testing the setup with:
On part 2 we going to lint code with Rubocop and use SimpleCov to gather code coverage.
additional links: