2012-04-18 12 views
5

Używam rspec-rails (2.8.1) do testowania funkcjonalnego aplikacji rails 3.1 przy użyciu mongoid (3.4.7) dla trwałości. Próbuję test rescue_from dla Mongoid :: Errors :: DocumentNotFound błędów w moim ApplicationController w taki sam sposób, jak rspec-rails documentation dla anonimowych kontrolerów sugeruje, że można to zrobić. Ale kiedy uruchomić następujący test ...dlaczego nie mogę podnieść Mongoid :: Errors :: DocumentNotFound w testach funkcjonalnych 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 

uzyskać następujące nieoczekiwany błąd

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

Dlaczego? Jak mogę podnieść ten mongoid error?

Odpowiedz

10

Mongoid's documentation for the exception pokazuje, że musi zostać zainicjalizowany. Poprawiony, działający kod jest następujący:

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 
Powiązane problemy