2014-10-08 48 views
1

我正在測試我的控制器。當我運行測試時,在「應編輯用戶」,出現此錯誤。在rails中測試 - 表單中的第一個參數不能包含零或爲空

1)錯誤: 的UsersControllerTest#test_should_edit_user: ::的ActionView ::模板錯誤:在表格第一個參數不能包含零或爲空

我想怎麼解決這個錯誤

user_controller_test

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

users_cont輥

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 

edit.html.erb

<%= link_to "Desativar conta", desactivate_account_path %> 
<%= link_to "Alterar Senha", edit_password_path, class: 'button' %> 

<%= form_for @user do |f| %> 
<% if @user.errors.any? %> 
    <div id="error_explanation"> 
    <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2> 

    <ul> 
    <% @user.errors.full_messages.each do |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 %> 

回答

0

有兩件事情:

首先,你的代碼,正是因爲它是工作,試試這個:

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

注意sp elling成功,兩個「c」s

那是因爲你運行測試的那一刻沒有session

而這帶來了第二件事:你在初始化session散列?你應該在你的控制器的某個地方有類似session = params[:user]的東西。

相關問題