Saturday, 15 June 2013

ruby - Testing in rails - First argument in form cannot contain nil or be empty -



ruby - Testing in rails - First argument in form cannot contain nil or be empty -

i'm trying test controller. when run test, in "should edit user", error appears.

1) error: userscontrollertest#test_should_edit_user: actionview::template::error: first argument in form cannot contain nil or empty

i'd how resolve error

user_controller_test

test "should edit user" :edit, id: @user assert_response :sucess end

users_controller

class userscontroller < applicationcontroller def index @users = user.all end def create @user = user.new(user_params) if @user.password == @user.password_confirmation if @user.save upload params[:user][:document] redirect_to root_path else render "new" end else render "new" end end def new @user = user.new end def edit @user = user.find_by_id(session[:remember_token]) end def edit_password @user = user.find_by_id(session[:remember_token]) end def update @user = user.find_by_id(session[:remember_token]) if @user @user.update_attribute(:username, params[:user][:username]) @user.update_attribute(:email, params[:user][:email]) redirect_to root_path else render "edit" end end def updatepassword @usersession = user.find_by_id(session[:remember_token]) @user = user.authenticate(@usersession.username, params[:user][:password]) if @user if params[:user][:password_confirmation] == params[:user][:new_password] if @user.update_attribute(:password, params[:user][:new_password]) redirect_to root_path else redirect_to edit_password_path end else redirect_to edit_password_path end else redirect_to edit_password_path end end def desactivate @user = user.find_by_id(session[:remember_token]) if @user @user.update_attribute(:account_status, false) redirect_to log_out_path else redirect_to root_path end end def destroy @user = user.find(params[:id]) @user.destroy redirect_to(action: "index") end

and edit.html.erb

<%= link_to "desativar conta", desactivate_account_path %> <%= link_to "alterar senha", edit_password_path, class: 'button' %> <%= form_for @user |f| %> <% if @user.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@user.errors.count, "error") %> prohibited user beingness saved:</h2> <ul> <% @user.errors.full_messages.each |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= f.label :username %><br> <%= f.text_field :username %> </div> <div class="field"> <%= f.label :email %><br> <%= f.text_field :email %> </div> <div class="actions"> <%= f.submit 'salvar alteração' %> </div> <% end %>

there 2 things:

first, code work is, seek this:

test "should edit user" session[:remember_token] = @user.id :edit assert_response :success end

note spelling of success, 2 "c"s

that's because moment run test there no session.

and brings sec thing: initialising session hash? should have session = params[:user] somewhere in 1 of controllers.

ruby-on-rails ruby testing

No comments:

Post a Comment