Mick F
Mick F

Reputation: 7439

Why does my rspec-rails generated spec fail due to a routing exception?

I generated a scaffold with Rails (4.1.16) and Rspec (3.5.1).

It generated this test:

describe "GET #show" do
  it "assigns the requested team as @team" do
    team = Team.create! valid_attributes
    get :show, params: {id: team.to_param}, session: valid_session
    expect(assigns(:team)).to eq(team)
  end
end

Which outputs this error:

TeamsController GET #show assigns the requested team as @team
 Failure/Error: get :show, params: {id: team.to_param}, session: valid_session

 ActionController::UrlGenerationError:
   No route matches {:action=>"show", :controller=>"teams", :params=>{:id=>"82"}, :session=>{}}

If I remove the keys to the parameters to get, i.e.:

get :show, {id: team.to_param}, valid_session

The test passes fine.

Not sure what gem defines the generator template (rspec-rails?) and why I get this error. Help would be appreciated understanding this issue. Thanks.

Upvotes: 2

Views: 171

Answers (1)

Dave Schweisguth
Dave Schweisguth

Reputation: 37627

The generator (rspec:scaffold, which comes with rspec-rails) is generating tests with the syntax required by Rails 5 (see the last section of that blog post), which is not compatible with Rails 4. I think this is a bug in rspec-rails, since rspec-rails 3.5 is otherwise compatible with Rails 4. (I'm using those versions together myself; I just haven't used the generator.)

rspec-rails was changed to use the Rails 5 syntax in rspec-rails 3.5.0.beta4, so one workaround is to use rspec and rspec-rails 3.4 — not so nice since the newer versions have features and fixes which are as useful with Rails 4 as with Rails 5. Another workaround is to manually fix the output of the generator as you did.

Upvotes: 3

Related Questions