APIs on Rails revised in 2017 — Chapter 3

Book from 2014 by Abraham Kuri Vargas, revised in 2017

Chapter 3 - Presenting the users

3.1.1 First user tests

I gone well through the book up to listing 3.4 where the specs for User model are written. The generator created the tests for me with first line like require ‘rails_helper’ while I have the file spec/spec_helper.rb. This was giving me an error. The file spec/spec_helper.rb is a residue of RSpec 2.x while files generated by RSpec 3.x will require ‘rails_helper’.

As described in RSpec docs, the solution is simple. I have just renamed the file spec/spec_helper.rb to spec/rails_helper.rb. I have also changed the previous occurrence of require ‘spec_helper’ to require ‘rails_helper’ in lib/spec/api_constraints_spec.rb.

My specs for User model differs a little before do … end block

They are all green at this point.

3.1.2 Improving validation tests

The next red tests we get while refactoring specs with gem ‘shoulda-matchers’ while they should be green. Solution is again simple. You can do it as in shoulda-matchers docs by including some configuration in spec/rails_helper.rb or like in this Sitepoint’s tutorial. I went with the latter so my rails_helper.rb file looks like this:

The shoulda-matchers methods are working now but soon after I have another glitch and reds where it should be greens. The case is validate_uniqueness_of(:email) assertion. I won’t be prescribing explanation of this glitch here, but if you’re curious you can check this GitHub issue with shoulda-matchers/devise. If you’re not curious, you can just chain .ignoring_case_sensitivity method to previous failing method. So now user_spec.rb looks like this:

and is giving me green specs again.

3.2 Building users endpoints

The next two glitches comes soon after in listing 3.6. First we need to enclose id: @user.id in params hash. Check this SO question for details. We now get rid of the first error and this spec in my case look like this:

Notice modified line 12

Second glitch to deal with is to include Devise::Test::ControllerHelpers module in rails_helper.rb file. For details check this SO question. My RSpec.configure block in rails_helper.rb file now includes this line config.include Devise::Test::ControllerHelpers, type: :controller.

At this point my controller tests are green.

3.2.2 Creating users

In Listing 3.9, as before in listing 3.6, we need to enclose second argument to post methods, which are user: @user_attributes in params hash, so the lines in before blocks will look like this post :create, params: { user: @user_attributes }, format: :json. This change came with Rails 5. Check this RSpec doc file for details. The users_controller_spec.rb differs a little from the original in the book (the only changes are in lines 17 and 32):

3.2.3 Updating users & 3.2.4 Destroying users

Listings 3.11 and 3.12 have the analogous issues as listings 3.6 and 3.9, so I’ll save some space and won’t include another gists for them.

3.3 Integrating Sabisu

In part 3.3 we are suppose to install sabisu gem. Unfortunately it’s currently (as of this writing) not supported by Rails 5. Here is the discussion about that. In short, Author of the book suggests here to skip this section, or to try postman. I went with the postman.

At the end of this chapter I have 21 green tests.

Head straight to revised chapter 4