271780 (1) [Avatar] Offline
Rails 4 in Action is still relevant to Rails 5.1 and RSpec 3.6.0.beta2. Just wanted to share some issues that I've encountered so far, and how to resolve them.

First of all, the Test Driven Development (TDD) approach presented in the book is an optimistic one. It's a bit of a jump to assume that the testing framework "just works". In fact, with Rails 5.1 and RSpec 3.6.0.beta2 updates, it doesn't quite, as presented in the book. And one needs something to test in order to fix the testing framework. So how to proceed?

In Chapter 1, the authors lean on Rails' scaffold generator to provide a marvelous overview of the Rails framework. So rather than begin Chapter 2 by being dismissive of it, a more honest approach might be to recognize the scaffold generator as a valuable diagnostic tool. In fact, if rspec-rails is added to the Gemfile and bundle update run prior to generating the scaffolding, then Rails not only builds an application but an RSpec testing framework as well! So let the reader be advised: keep a scaffold-generated app around when starting fresh. In addition to boiler-plate code (like that in Listing 3.15, which might have been pulled from a hat, had it not been previously encountered in Chapter 1), it offers a model of current practices to compare against.

Cut to the chase: In a Rails 5.1, the form_for method has been deprecated in favor of form_with (as evidenced by examination of the scaffolding!), so Listing 3.15 should now begin with:

<%= form_with(model: project) do |f| %>

instead of:

<%= form_for(@project) do |f| %>

With this update, getting the test code presented in Chapter 3, Section 1.1 to work requires one more small change. The issue is that Rails 5.1 evidently no longer provides default form field ID attributes. So let's add them manually: In Listing 3.15 again, replace:

<%= f.text_field :name %>


<%= f.text_field :description %>


<%= f.text_field :name, id: :project_name %>


<%= f.text_field :name, id: :project_description %>

respectively. Now RSpec/Capybara feature tests should work as expected!

Until the book is updated, hopefully this will encourage a reader or two to forge ahead with Rails 4 in Action and the Rails 5.1/RSpec 3.6.0.beta2 framework. Good luck, and enjoy!

PS - One more gotcha! When the rails or bundle commands start failing, kill any running spring processes. Spring tries to preload apps, but unfortunately persists even after those apps have moved or disappeared. By killing the spring process, you're just telling Rails and/or Bundler to start fresh.