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