Asantoya17
Asantoya17

Reputation: 4725

testing: No route matches {:action=>"show", :controller=>"users", :id=>nil}

I only have 2 weeks learning ruby and ruby on rails. currently my problem is with te routes matches I don't understand so much, now I'm testing with rspec this:

require 'spec_helper'

describe UsersController do

it "should redirect to the user show page" do
post :create, :users => @attr
response.should redirect_to(user_path(assigns(:users)))
end
  describe "signup" do

before { visit new_user_registration_path }

let(:submit) { "Sign up" }

describe "with invalid information" do
  it "should not create a user" do
    expect { click_button submit }.not_to change(User, :count)
  end
end

describe "with valid information" do
  before do
    fill_in "Email", :with=> "[email protected]"
    fill_in "Password", :with=> "foobar"
    #fill_in "password_confirmation", :with=> "foobar"
  end

  it "should create a user" do
    expect { click_button submit }.to change(User, :count).by(1)
  end
end
end
end 

i have this on my UserController

class UsersController < ApplicationController

def new
     @user = User.new
end

def create
    @user = User.new(params[:user])
    if @user.save
        redirect_to user_session_path
    else
    redirect_to new_user_session_path
end

end

def show
    @user = User.find(params[:id])
    #redirect_to @user
end
end

but when I run the test i got this error:

Failures:

1) UsersController should redirect to the user show page
   Failure/Error: response.should redirect_to(user_path(assigns(:users)))
   ActionController::RoutingError:
   No route matches {:action=>"show", :controller=>"users", :id=>nil}
   # ./spec/controllers/user_controller_spec.rb:7

2) UsersController signup with valid information should create a user
   Failure/Error: expect { click_button submit }.to change(User, :count).by(1)
   count should have been changed by 1, but was changed by 0
   # ./spec/controllers/user_controller_spec.rb:29

Upvotes: 0

Views: 1320

Answers (1)

Erez Rabih
Erez Rabih

Reputation: 15808

In your spec you're referring twice to users (plural) instead of user (singular):

it "should redirect to the user show page" do
  post :create, :user => @attr
  response.should redirect_to(user_path(assigns(:user)))
end

Should pass.

UPDATE:

In case @attr is not set you should of course set it properly. Just supply a hash with all attributes and values for the model. Assuming User model has attributes name and address:

@attr = { :name => "SomeUserName", :address => "SomeAddress"}

Upvotes: 2

Related Questions