Rspec help for create user test

My user controllers ‘create’ action looks like:

def create
@user = User.new(params[:user])

@user.user_name = params[:user][:user_name]
@user.email = params[:user][:email]

if @user.is_valid?
@user.status = 2
@user.save!

  UserMailer.new_user(@user).deliver

 redirect_to(@user, :notice => "user was created successfully")

else
render :action => ‘new’

end

end

My test is:

describe "POST ‘create’ do
it “should be successful” do

user = mock_model(user)
user.sub(:new).and_return(user)

post 'create'
response.should redirect_to(user)

end

end

error:

Failure/Error: post ‘create’
NoMethodError:
You have a nil object when you didn’t expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]
# ./app/controllers/users_controller.rb:44:in `create’
# ./spec/controllers/users_controller_spec.rb:81

On Thu, May 19, 2011 at 1:18 PM, S Ahmed [email protected] wrote:

 @user.status = 2

user = mock_model(user)

error:


rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users

Hello. nil.[] means the code is trying to call an array getter method
(the_array[the_index]) on nil. In the action,
“params[:user][:user_name]” is
causing the error because there is no “:user” key in “params”. Here is
how
you want to invoke the request:

post :create, :user => {:user_name => ‘foo’, :email =>
[email protected]’}

Hope that fixes it.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs