require 'rspec_ext' require "#{__FILE__.dirname}/helper" describe "Error handling" do with_environment :test before :all do @dir = "#{File.expand_path(File.dirname(__FILE__))}/error_handling_spec" $LOAD_PATH << @dir AbstractController = Crystal::AbstractController crystal.before :environment do crystal.config.test_error_template = crystal.config.development_error_template! end crystal.after :environment do crystal.conveyors.web do |web| web.use Crystal::Processors::ControllerErrorHandling, :content web.use Crystal::Processors::ControllerCaller, :content end end end after :all do $LOAD_PATH.delete @dir remove_constants %w( DisplayErrorWithFormat DiferrentErrorHandlingSpec AbstractController ) end it "should correctly display error messages in :development (with correct format)" do class ::DisplayErrorWithFormat inherit AbstractController def method raise params.error end end config.environment = :development silence_logger! error = StandardError.new('some error') ccall( DisplayErrorWithFormat, :method, {:error => error, :format => :html} ).content.should =~ /html.+some error/m ccall( DisplayErrorWithFormat, :method, {:error => error, :format => :js} ).content.should =~ /some error/ ccall( DisplayErrorWithFormat, :method, {:error => error, :format => :non_existing_mime} ).content.should =~ /some error/ end describe "should be different in :test, :development and :production" do before :all do class ::DiferrentErrorHandlingSpec inherit AbstractController def a; end def b raise 'error in remote' end def c; end end end it "in :production errors shouldn't be shown to user" it "in development errors should be catched" do config.environment = :development silence_logger! # with view ccall(DiferrentErrorHandlingSpec, :a).content.should == 'a' ccall(DiferrentErrorHandlingSpec, :b).content.should =~ /error in remote/ ccall(DiferrentErrorHandlingSpec, :c).content.should =~ /error in template/ end it "should not catch errors in :test environment" do config.environment = :test # with render ccall(DiferrentErrorHandlingSpec, :a).content.should == 'a' lambda{ccall(DiferrentErrorHandlingSpec, :b)}.should raise_error(/error in remote/) lambda{ccall(DiferrentErrorHandlingSpec, :c)}.should raise_error(/error in template/) end end end