2012-04-18 6 views
5

मैं rspec-rails (2.8.1) का उपयोग कर रहा हूं, रेलवे का कार्यात्मक परीक्षण करने के लिए मैंगॉयड (3.4.7) का उपयोग करते हुए मैकॉइड (3.4.7) का उपयोग कर रहा हूं। मैं Mongoid :: त्रुटियों :: DocumentNotFound त्रुटियों के लिए परीक्षण अनुप्रयोग की कोशिश कर रहा हूं, उसी तरह से मेरे अनुप्रयोग नियंत्रक में rspec-rails documentation अज्ञात नियंत्रकों के लिए सुझाव देता है कि यह किया जा सकता है। लेकिन जब मैं निम्नलिखित परीक्षण चलाने ...मैं आरएसपीईसी कार्यात्मक परीक्षण में Mongoid :: त्रुटियों :: 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)>' 

क्यों मिलता? मैं इस मोंगोइड त्रुटि को कैसे बढ़ा सकता हूं?

उत्तर

10

Mongoid's 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 
संबंधित मुद्दे