2014-01-24 111 views
0

我有一個ApplicationController中在公共區域我的Rails控制器測試未繼承的ApplicationController方法

def current_user 
    session[:user] 
end 

一個給定的方法和另一個控制器

ObjetosController < ApplicationController 

其需要訪問這樣的方法,和我在運行測試時遇到了一個可怕的錯誤(耙測試:函數):

NameError: undefined local variable or method `current_user' for

我正在使用Ruby 2.0.0和Rails 4.0。我一直在谷歌和StackOverflow幾個小時,並沒有發現任何關於它。 也許有人可以給我一個提示或幫助我尋找答案嗎?

如果它有幫助,如果我把垃圾放在ApplicationController裏面,並運行測試,它不會抱怨!這就像有人裝載該類別的另一版本...

這裏我ObjetosController代碼:

class ObjetosController < ApplicationController 
    before_action :set_objeto, only: [:show, :edit, :update, :destroy] 

    # GET /objetos 
    # GET /objetos.json 
    def index 
    @objetos = Objeto.all 
    end 

    # GET /objetos/1 
    # GET /objetos/1.json 
    def show 
    end 

    # GET /objetos/new 
    def new 
    @objeto = Objeto.new 
    end 

    # GET /objetos/1/edit 
    def edit 
    end 

    # POST /objetos 
    # POST /objetos.json 
    def create 
    @objeto = Objeto.new(objeto_params) 

    respond_to do |format| 
     if @objeto.save 
     format.html { redirect_to @objeto, notice: 'Objeto was successfully created.' } 
     format.json { render action: 'show', status: :created, location: @objeto } 
     else 
     format.html { render action: 'new' } 
     format.json { render json: @objeto.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /objetos/1 
    # PATCH/PUT /objetos/1.json 
    def update 
    current_user 
    respond_to do |format| 
     if @objeto.update(objeto_params) 
     format.html { redirect_to @objeto, notice: 'Objeto was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render action: 'edit' } 
     format.json { render json: @objeto.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /objetos/1 
    # DELETE /objetos/1.json 
    def destroy 
    @objeto.destroy 
    respond_to do |format| 
     format.html { redirect_to objetos_url } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_objeto 
     @objeto = Objeto.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def objeto_params 
     params.require(:objeto).permit(:nombre, :codigo_sgej) 
    end 
end 
+0

把 – ryudice

+0

我已經把它添加到我原來的職位您的objetoscontroller代碼。就像Rails生成它,我沒有添加任何東西(除了current_user,出於調試原因...) – luispablo

回答

0

解決它。

在我的ApplicationController類中,我實例化了爲每個環境動態定義的一些類。我還使用了每個環境定義的一些常量。

由於此應用尚未發佈到臨時或生產,我只有在config/environments/development.rb文件中定義了此類值。我將它們添加到config/environments/test.rb,現在它可以工作!

我失去了時間這個問題,我與軌真的失望(或Ruby?),不顯示在所有任何錯誤...

相關問題