2012-04-18 3 views
5

私は永続性のためにmongoid(3.4.7)を使用して、レール3.1アプリケーションの機能テストにrspec-rails(2.8.1)を使用しています。匿名コントローラのrspec-rails documentationと同じ方法で、私のApplicationControllerでMongoid :: Errors :: DocumentNotFoundエラーのテストrescue_fromを試しています。私は次のテストを実行したときしかし... なぜRSpecの機能テストでMongoid :: Errors :: DocumentNotFoundを呼び出せないのですか?

require "spec_helper" 

class ApplicationController < ActionController::Base 

    rescue_from Mongoid::Errors::DocumentNotFound, :with => :access_denied 

private 

    def access_denied 
    redirect_to "/401.html" 
    end 
end 

describe ApplicationController do 
    controller do 
    def index 
     raise Mongoid::Errors::DocumentNotFound 
    end 
    end 

    describe "handling AccessDenied exceptions" do 
    it "redirects to the /401.html page" do 
     get :index 
     response.should redirect_to("/401.html") 
    end 
    end 
end 

は、私は、次の予期しないエラー

1) ApplicationController handling AccessDenied exceptions redirects to the /401.html page 
    Failure/Error: raise Mongoid::Errors::DocumentNotFound 
    ArgumentError: 
     wrong number of arguments (0 for 2) 
    # ./spec/controllers/application_controller_spec.rb:18:in `exception' 
    # ./spec/controllers/application_controller_spec.rb:18:in `raise' 
    # ./spec/controllers/application_controller_spec.rb:18:in `index' 
    # ./spec/controllers/application_controller_spec.rb:24:in `block (3 levels) in <top (required)>' 

なぜもらいますか?このmongoidエラーをどのようにして発生させることができますか?

答えて

10

モンゴイドのdocumentation for the exceptionは、初期化する必要があることを示しています。修正された作業コードは次のとおりです。

require "spec_helper" 

class SomeBogusClass; end 

class ApplicationController < ActionController::Base 

    rescue_from Mongoid::Errors::DocumentNotFound, :with => :access_denied 

private 

    def access_denied 
    redirect_to "/401.html" 
    end 
end 

describe ApplicationController do 
    controller do 
    def index 
     raise Mongoid::Errors::DocumentNotFound.new SomeBogusClass, {} 
    end 
    end 

    describe "handling AccessDenied exceptions" do 
    it "redirects to the /401.html page" do 
     get :index 
     response.should redirect_to("/401.html") 
    end 
    end 
end 
関連する問題