2012-04-18 21 views
5

Sto usando rspec-rails (2.8.1) per testare funzionale un'applicazione di rails 3.1 che utilizza mongoid (3.4.7) per la persistenza. Sto provando test rescue_from per gli errori Mongoid :: Errors :: DocumentNotFound nel mio ApplicationController nello stesso modo in cui lo rspec-rails documentation per i controller anonimi suggerisce che potrebbe essere fatto. Ma quando ho eseguito il seguente test ...perché non posso aumentare Mongoid :: Errors :: DocumentNotFound nel test funzionale RSpec?

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 

ottengo il seguente errore imprevisto

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)>' 

Perché? Come posso aumentare questo errore mongoid?

risposta

10

Mongoid's documentation for the exception mostra che deve essere inizializzato. Il codice corretto e funzionante è il seguente:

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 
Problemi correlati