Rails 4 in Action... now for Rails 5! Chapter 2

Continuing on from the chapter 1 runthrough of Rails 4 in Action updated for Rails 5, today I’ll cover the testing frameworks that we originally covered in chapter 2. Testing will once again save our bacon!

Chapter 2 - Testing saves your bacon

Writing your first test

Not a lot has changed with Minitest in the past eighteen months. If you’re using a recent version of Ruby (2.3 or above), your output when running the first version of BaconTest (listing 2.3, page 29) will be slightly different:

  1) Error:
NameError: uninitialized constant BaconTest::Bacon
Did you mean?  BaconTest
  bacon_test.rb:5:in 'test_saved'

Note the added “did you mean?” line. This can come in handy if you accidentally make spelling mistakes, in constants, variables or method names.

Saving bacon

You’ll get a slightly more helpful error message when you deliberately make your test fail (page 30):

1) Failure:
BaconTest#test_saved [bacon_test.rb:11]:
Expected false to be truthy.

But you can still customize the messages that get displayed on failure if the autogenerated ones aren’t to your tastes.

Introducing RSpec

RSpec has had a few changes in the last 18 months, and the most recent version is now 3.5.0. Install that instead, with gem install rspec -v 3.5.0 (page 31). You’ll get updated versions of all the gems RSpec 3.5.0 depends on, as well:

Fetching: rspec-support-3.5.0.gem (100%)
Successfully installed rspec-support-3.5.0
Fetching: rspec-core-3.5.4.gem (100%)
Successfully installed rspec-core-3.5.4
Fetching: diff-lcs-1.3.gem (100%)
Successfully installed diff-lcs-1.3
Fetching: rspec-expectations-3.5.0.gem (100%)
Successfully installed rspec-expectations-3.5.0
Fetching: rspec-mocks-3.5.0.gem (100%)
Successfully installed rspec-mocks-3.5.0
Fetching: rspec-3.5.0.gem (100%)
Successfully installed rspec-3.5.0
6 gems installed

Running the spec

After you’ve created the Bacon class and run your first RSpec test, the output is slightly different because there was a mistake in the book’s output code - our test called Bacon.edible?, not Bacon.new.edible?. The output now looks like this (page 33):

1) Bacon is edible
   Failure/Error: expect(Bacon.edible?).to be(true)

     undefined method 'edible?' for Bacon:Class
   # ./spec/bacon_spec.rb:5:in `block (2 levels) in <top (required)>'

Expiring bacon

When you first run your spec to see if bacon can expire (page 36), the output again slightly differs due to the this-time-unhelpful “did you mean?” output:

1) Bacon can expire
   Failure/Error: self.expired = true

     undefined method 'expired=' for #<Bacon:0x007ffe42149270>
     Did you mean?  expired!
   # ./lib/bacon.rb:7:in `expired!'

And that’s it! Nice and short, as basic Ruby hasn’t really changed, and there isn’t any difference with the little code written while experimenting with testing - just in the content of output messages. Things might get a bit different when Ticketee starts getting built, in chapter 3! Stay tuned!