2008-11-24 67 views
3

我想測試我的應用程序中有一個方法,但是我不知道如何單元測試一種被僞造的方法,請看看這個:在Rails中防止僞造的測試控制器方法

def index 
    @alumnos = Alumno.paginate :per_page => 20, 
     :page => params[:page], :order => :nombre 

    respond_to do |format| 
     format.html # index.html.erb 
     format.xml { render :xml => @alumnos } 
    end 
    end 

在index.html.erb我有這樣一行:

<%= javascript_tag "window._token = '#{form_authenticity_token}'" %> 

所以,當我嘗試用功能測試,以測試它,它似乎會話沒有祕密工作會導致測試失敗,有沒有人有經驗測試這種方法?

回答

1

這裏是一個解決方案(或替代方法): 在控制器/ application.rb中

if RAILS_ENV =='test' 
    protect_from_forgery :secret => 'write ur secret in config/environment.rb' 

    else 

protect\_from_forgery 

    end