On Thu, May 19, 2011 at 1:18 PM, S Ahmed <sahmed1020 at gmail.com> wrote:
> 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
>
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> 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
=> ''test at example.com''}
Hope that fixes it.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://rubyforge.org/pipermail/rspec-users/attachments/20110519/05d55c81/attachment.html>