Upcase: Test-Driven Rails

Test-Driven Rails Development using RSpec and Capybara

Upcase Test-Driven Rails

Code for this course is here. Keep in mind that this tutorial is based on an old version of Rails and as such I had to make modifications as I went along.

Video 1: Why Test

Goals

Business goals

  • behaves as the user expects
  • behaves as the owner expects
  • it actually works

Code goals

  • Easy for others to understand
  • conveys intent
  • Acts as documentation (acceptance tests)
  • easy to refactor

Process goals

  • written quickly
  • Guides the design deciions
  • addresses only what's necessary
  • Establishes trust

Red/green refactor

  • refactor shouldn't take very long. Refactoring should be continous
  • outside-in development (black box)
    • Acceptance testing
      • Don't want to test every single state
  • Unit tests
    • isolates one component to test all of the permutations
  • Write a failing acceptance test
    • see it fail
    • then get to a point where you need to write a unit test
      • watch it fail
      • make it pass and work your way back up until you get everything passing

Video 2: setting up the app and initial test

Rails Generate Commands

  • rails g to see a list of generator commands
  • rails g rspec:install install rspec

Issues Resolved

  • ERROR: Failed to build gem native extension. nokogiri

Ran into an error where a gem native extension wouldn't build on my distro (Ubuntu 18.04). I had already installed Ruby using Snap, and then followed the instructions on the Nokogiri site to finish installing it. I installed the following dependencies.

sudo apt-get install build-essential patch zlib1g-dev liblzma-dev
gem install nokogiri

However, I subsequently removed Ruby installed via snap and started using RVM and as such the above steps might not have been needed. Installing using RVM also fixed issues with Command rails not found and cannot load such file -- rubygems.rb

  • cannot load such file -- ../../spec/rails_helper

This issue was fixed by added the lines --require spec_helper and --require rails_helper to .rspec

  • Test not finding element

I missed the need to use visit base_path to navigate to the homepage in the rspec test and thus it was failing

Video 3: creating the first todo

Video 4: signing in and todo ownership

  • NameError: uninitialized constant Features

Attempting to add rspec support helpers resulted in this error. It turned out that all that needed to happen was to uncomment the following line from spec/rails_helper.rb:

Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }

  • undefined method 'each' for nil:NilClass

Was trying to each over a model collection but it bombed out since before_filter was changed to before_action in Rails 5.

  • No route matches [POST] "/session/new"

I missed adding :create to the routes.rb resource for session.

  • Rubymin debugging issues: ExecJS::RuntimeUnavailable

When trying to run Rspec tests using Rubymine, I was running into the ExecJS error. To fix it, I uncommented the line, gem 'mini_racer', platforms: :ruby, in the Rails gem file