Rspec-rails: Scaffold generator generates gets error on rails4.

Created on 26 Jul 2016  Â·  16Comments  Â·  Source: rspec/rspec-rails

➜  testrspec git:(master) rake routes
    Prefix Verb   URI Pattern                Controller#Action
    robots GET    /robots(.:format)          robots#index
           POST   /robots(.:format)          robots#create
 new_robot GET    /robots/new(.:format)      robots#new
edit_robot GET    /robots/:id/edit(.:format) robots#edit
     robot GET    /robots/:id(.:format)      robots#show
           PATCH  /robots/:id(.:format)      robots#update
           PUT    /robots/:id(.:format)      robots#update
           DELETE /robots/:id(.:format)      robots#destroy
1) RobotsController GET #show assigns the requested robot as @robot
     Failure/Error: get :show, params: {id: robot.to_param}

     ActionController::UrlGenerationError:
       No route matches {:action=>"show", :controller=>"robots", :params=>{:id=>"1"}}
     # ./spec/controllers/robots_controller_spec.rb:52:in `block (3 levels) in <top (required)>'

source

Needs reproduction case

Most helpful comment

It turns out that the test should read

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

Note that the get line has had params: {id: user.to_param} changed to just id: user.to_param. Was this changed at some point and the scaffold files not updated?

All 16 comments

"params:" should REMOVE

@JonRowe Closed for what?

Hi @qx, the rspec-rails github issues isn't a place to ask for help, so we tend to close such issues.

It looks like your not generating your route properly, can you paste a snippet of your spec and not just the error? Are you using Rails 5 and thus are you using the rails-controller-testing gem?

Restored comment from @qx on an unrelated issue:

@JonRowe rails 4.2.x get this error
and 5.0.0 get below error

  1) RobotsController PUT #update with invalid params re-renders the 'edit' template
     Failure/Error: expect(response).to render_template("edit")
       expecting <"edit"> but was a redirect to <http://test.host/robots/1>
     # ./spec/controllers/robots_controller_spec.rb:141:in `block (4 levels) in <top (required)>'

All not work

@qx please limit your comments to this thread

In order for us to help you we need the following:

  • Your Rails version
  • Confirm you are using rails-controller-testing if you are using Rails 5
  • A snippet showing your spec

or if you think is is a bug and you're not asking for help, can you please explain better what the problem is, it's not clear from what you've posted so far what problem you are having. A minimal reproduction case (no other dependencies other than Rails and RSpec) showing the issue is very useful in such cases.

@JonRowe I think it's not help, it's problem of scaffold generates, the source is just helloworld for test this suit, for Giving you the most detail about this problem, not for help!

Rails about 4.2x 5.0x all not work.
And Document seems not mention about '~3.5.0' is fit for rails 5+,

Can you please create a new repo, run rails new and then run the scaffold command you are having trouble with, and explain the problem with it, otherwise I will close this as a duplicate of #1669

2016-07-26 9 01 09
2016-07-26 9 01 47
2016-07-26 9 02 42

Hi @qx please use code snippets rather than screenshots, and please paste the contents of your spec file.

require 'rails_helper'

# This spec was generated by rspec-rails when you ran the scaffold generator.
# It demonstrates how one might use RSpec to specify the controller code that
# was generated by Rails when you ran the scaffold generator.
#
# It assumes that the implementation code is generated by the rails scaffold
# generator.  If you are using any extension libraries to generate different
# controller code, this generated spec may or may not pass.
#
# It only uses APIs available in rails and/or rspec-rails.  There are a number
# of tools you can use to make these specs even more expressive, but we're
# sticking to rails and rspec-rails APIs to keep things simple and stable.
#
# Compared to earlier versions of this generator, there is very limited use of
# stubs and message expectations in this spec.  Stubs are only used when there
# is no simpler way to get a handle on the object needed for the example.
# Message expectations are only used when there is no simpler way to specify
# that an instance is receiving a specific message.

RSpec.describe UsersController, type: :controller do

  # This should return the minimal set of attributes required to create a valid
  # User. As you add validations to User, be sure to
  # adjust the attributes here as well.
  let(:valid_attributes) {
    {firsname: "iuoiui", lastname: "234we"}
  }

  let(:invalid_attributes) {
    {firsname: "iuoiui"}

  }

  # This should return the minimal set of values that should be in the session
  # in order to pass any filters (e.g. authentication) defined in
  # UsersController. Be sure to keep this updated too.
  let(:valid_session) { {} }

  describe "GET #index" do
    it "assigns all users as @users" do
      user = User.create! valid_attributes
      get :index, params: {}, session: valid_session
      expect(assigns(:users)).to eq([user])
    end
  end

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

  describe "GET #new" do
    it "assigns a new user as @user" do
      get :new, params: {}, session: valid_session
      expect(assigns(:user)).to be_a_new(User)
    end
  end

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

  describe "POST #create" do
    context "with valid params" do
      it "creates a new User" do
        expect {
          post :create, params: {user: valid_attributes}, session: valid_session
        }.to change(User, :count).by(1)
      end

      it "assigns a newly created user as @user" do
        post :create, params: {user: valid_attributes}, session: valid_session
        expect(assigns(:user)).to be_a(User)
        expect(assigns(:user)).to be_persisted
      end

      it "redirects to the created user" do
        post :create, params: {user: valid_attributes}, session: valid_session
        expect(response).to redirect_to(User.last)
      end
    end

    context "with invalid params" do
      it "assigns a newly created but unsaved user as @user" do
        post :create, params: {user: invalid_attributes}, session: valid_session
        expect(assigns(:user)).to be_a_new(User)
      end

      it "re-renders the 'new' template" do
        post :create, params: {user: invalid_attributes}, session: valid_session
        expect(response).to render_template("new")
      end
    end
  end

  describe "PUT #update" do
    context "with valid params" do
      let(:new_attributes) {
        {firsname: "rer", lastname: "ffff"}

      }

      it "updates the requested user" do
        user = User.create! valid_attributes
        put :update, params: {id: user.to_param, user: new_attributes}, session: valid_session
        user.reload
        {lastname: "ffffwerf"}

      end

      it "assigns the requested user as @user" do
        user = User.create! valid_attributes
        put :update, params: {id: user.to_param, user: valid_attributes}, session: valid_session
        expect(assigns(:user)).to eq(user)
      end

      it "redirects to the user" do
        user = User.create! valid_attributes
        put :update, params: {id: user.to_param, user: valid_attributes}, session: valid_session
        expect(response).to redirect_to(user)
      end
    end

    context "with invalid params" do
      it "assigns the user as @user" do
        user = User.create! valid_attributes
        put :update, params: {id: user.to_param, user: invalid_attributes}, session: valid_session
        expect(assigns(:user)).to eq(user)
      end

      it "re-renders the 'edit' template" do
        user = User.create! valid_attributes
        put :update, params: {id: user.to_param, user: invalid_attributes}, session: valid_session
        expect(response).to render_template("edit")
      end
    end
  end

  describe "DELETE #destroy" do
    it "destroys the requested user" do
      user = User.create! valid_attributes
      expect {
        delete :destroy, params: {id: user.to_param}, session: valid_session
      }.to change(User, :count).by(-1)
    end

    it "redirects to the users list" do
      user = User.create! valid_attributes
      delete :destroy, params: {id: user.to_param}, session: valid_session
      expect(response).to redirect_to(users_url)
    end
  end

end

Rails 4 Test for rspec scaffold repo
And I have pasted Rails 5.0 test case on github, If you like to check.

Is there any news on this? I found this after encountering the same problem. I have reproduced with the test case that @qx has provided:

$ rspec spec/controllers/users_controller_spec.rb:49
Run options: include {:locations=>{"./spec/controllers/users_controller_spec.rb"=>[49]}}
F

Failures:

  1) UsersController GET #show assigns the requested user as @user
     Failure/Error: get :show, params: {id: user.to_param}, session: valid_session

     ActionController::UrlGenerationError:
       No route matches {:action=>"show", :controller=>"users", :params=>{:id=>"1"}, :session=>{}}
     # ./spec/controllers/users_controller_spec.rb:51:in `block (3 levels) in <top (required)>'

Finished in 0.01257 seconds (files took 1.45 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/controllers/users_controller_spec.rb:49 # UsersController GET #show assigns the requested user as @user

and

$ rake routes|grep show
     user GET    /users/:id(.:format)      users#show

so a GET route matching {:action=>"show", :controller=>"users", :params=>{:id=>"1"}, :session=>{}} does exist.

This is a spec test generated by rails generate scaffold and the only change is to the valid_attributes so that the test is not marked pending.

  • Ruby 2.3.3
  • Rails 4.2.7

It turns out that the test should read

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

Note that the get line has had params: {id: user.to_param} changed to just id: user.to_param. Was this changed at some point and the scaffold files not updated?

@jrmhaig I think the problem is older versions of rails has the keyless setup you outline. Rails 5 favors keys along with the data: https://github.com/rails/rails/pull/18323/

the problem is older versions of rails has the keyless setup you outline

Right.

Note that the get line has had params: {id: user.to_param} changed to just id: user.to_param. Was this changed at some point and the scaffold files not updated?

The scaffold generates proper controller specs now, it has been fixed in this pull request.

Was this page helpful?
0 / 5 - 0 ratings