Noah Clark
Noah Clark

Reputation: 8131

Rspec Test Won't Pass using assigns()

Here is my current users_controller_spec.rb file

require 'spec_helper'

describe UsersController do
  render_views
  .
  .
  .
  describe "success" do

   before(:each) do
    @attr = { :name => "New User", :email => "[email protected]",
              :password => "foobar", :password_confirmation => "foobar" }
  end

  it "should create a user" do
    lambda do
      post :create, :user => @attr
    end.should change(User, :count).by(1)
  end

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

When I run this I get the following:

 Failures:

1) UsersController POST 'create' success should redirect to the user show page
 Failure/Error: response.should redirect_to(user_path(user))
 ActionController::RoutingError:
   No route matches {:action=>"show", :controller=>"users"}
 # ./spec/controllers/users_controller_spec.rb:95:in `block (4 levels) in <top (required)>'

Which leads me to believe that :user isn't an actual object. How can I test this and how can I change :user into an object that user_path understands.

Thanks in advance for any help.

UPDATED:

def create
  @title = "Sign up"
  @user = User.new(params[:user])
  if @user.save
    redirect_to @user, :notice => "Signed Up!"
  else
    @title = "Sign up"
    render "new"
  end
end

When I run the following:

it "should redirect to the user show page" do
  post :create, :user => @attr
  user = assigns(:user)
  user.should_not be_blank
  puts "user errors are: #{user.errors.full_messages.inspect}" unless user.is_valid?
  user.should be_valid
  response.should redirect_to(user_path(user))
end

I get:

1) UsersController POST 'create' success should redirect to the user show page
     Failure/Error: user.should_not be_blank
       expected blank? to return false, got true
     # ./spec/controllers/users_controller_spec.rb:94:in `block (4 levels) in <top (required)>'

Upvotes: 3

Views: 4157

Answers (3)

Feech
Feech

Reputation: 4102

Try using assigns with brackets instead of parenthesis.

assigns[:user]

(RSpec docs)


EDIT

One pattern I always use when testing controller actions is to ensure that a variable required by the associated view(s) is assigned. For example,

it "should assign an @user variable" do
  post :create, :user => @attr
  assigns[:user].should_not be_nil
  assigns[:user].should be_kind_of(User)
end

Upvotes: 4

Taryn East
Taryn East

Reputation: 27747

You've got a typo

response.should redirect_to(user_path(:user))

should be

response.should redirect_to(user_path(user))

Edit: Try checking that the user is valid with:

it "should redirect to the user show page" do
  post :create, :user => @attr
  user = assigns(:user)
  user.should_not be_blank
  puts "user errors are: #{user.errors.full_messages.inspect}" unless user.is_valid?
  user.should be_valid
  response.should redirect_to(user_path(user))
end

I know it works on the previous test case... but still worth checking extensively at least once here. You can remove all that guff when you're certain.

Upvotes: 2

Simon Bagreev
Simon Bagreev

Reputation: 2859

In your test:

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

Your @attr hash is different from @user. You are creating user with attributes of @attr, and asserting that it should redirect to @user show (which is different from one created). Change to:

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

Upvotes: 2

Related Questions