spec/ballast/concerns/errors_handling_spec.rb in ballast-1.9.3 vs spec/ballast/concerns/errors_handling_spec.rb in ballast-2.0.0

- old
+ new

@@ -4,67 +4,73 @@ # require "spec_helper" describe Ballast::Concerns::ErrorsHandling do - class ErrorsHandlingMockClass < OpenStruct - include Ballast::Concerns::Ajax + class ErrorsHandlingMockClass + include Ballast::Concerns::AjaxHandling include Ballast::Concerns::ErrorsHandling - def initialize(attrs) - @operation = OpenStruct.new(attrs.delete(:operation)) - super(attrs) + def request + OpenStruct.new(format: "json") end + + def performed? + + end + + def render(*args) + + end end - subject{ ErrorsHandlingMockClass.new(response: OpenStruct.new(headers: {}), headers: {}, params: {}, performed?: false) } + subject{ ErrorsHandlingMockClass.new } describe "#handle_error" do it "should handle a custom error" do - error = {status: "STATUS", error: "ERROR"} + error = {status: "STATUS", error: "ERROR", title: "TITLE"} expect(subject).to receive(:send_or_render_error).with("LAYOUT", nil) - subject.handle_error(error, "LAYOUT", "TITLE") - + subject.handle_error(error, layout: "LAYOUT", title: "OTHER TITLE") expect(subject.instance_variable_get(:@error)).to eq(error) - expect(subject.instance_variable_get(:@error_code)).to eq("STATUS") - expect(subject.instance_variable_get(:@error_title)).to eq("TITLE") - expect(subject.instance_variable_get(:@error_message)).to eq("ERROR") + + error = {status: "STATUS", error: "ERROR"} + expect(subject).to receive(:send_or_render_error).with("LAYOUT", nil) + subject.handle_error(error, layout: "LAYOUT", title: "OTHER TITLE") + expect(subject.instance_variable_get(:@error)).to eq(error.merge({title: "OTHER TITLE"})) end it "should handle a debug error" do + error = Lazier::Exceptions::Debug.new("MESSAGE") expect(subject).to receive(:send_or_render_error) - subject.handle_error(Lazier::Exceptions::Debug.new("MESSAGE")) - expect(subject.instance_variable_get(:@error_code)).to eq(503) - expect(subject.instance_variable_get(:@error_title)).to eq("Debug") - expect(subject.instance_variable_get(:@error_message)).to be_nil + subject.handle_error(error) + expect(subject.instance_variable_get(:@error)).to eq({status: 503, title: "Debug", error: "MESSAGE", exception: error}) end it "should handle every other error" do + error = RuntimeError.new("ERROR") expect(subject).to receive(:send_or_render_error) - subject.handle_error(RuntimeError.new("ERROR")) - expect(subject.instance_variable_get(:@error_code)).to eq(500) - expect(subject.instance_variable_get(:@error_title)).to eq("Error - RuntimeError") - expect(subject.instance_variable_get(:@error_message)).to be_nil + subject.handle_error(error) + expect(subject.instance_variable_get(:@error)).to eq({status: 500, title: "Error - RuntimeError", error: "ERROR", exception: error}) end it "should render an AJAX error" do expect_any_instance_of(RuntimeError).to receive(:backtrace).and_return(["A", "B"]) - allow(subject).to receive(:request).and_return(OpenStruct.new(format: :json)) - expect(subject).to receive(:is_ajax?).exactly(2).and_return(true, false) - - expect(subject).to receive(:send_ajax).with({status: 500, error: "ERROR", data: {type: "Error - RuntimeError", backtrace: ["A", "B"]}}.with_indifferent_access, {format: :json}) - expect(subject).to receive(:send_ajax).with({status: :forbidden, error: "ERROR", data: {type: "TITLE"}}.with_indifferent_access, {format: :json}) + expect(subject).to receive(:ajax_request?).exactly(2).and_return(true, false) - subject.handle_error(RuntimeError.new("ERROR"), "LAYOUT") + expect(Ballast::AjaxResponse).to receive(:new).with({status: 500, error: "ERROR", data: {description: "Error - RuntimeError", backtrace: ["A", "B"]}, transport: subject}).and_call_original + expect(Ballast::AjaxResponse).to receive(:new).with({status: :forbidden, error: "ERROR", data: {description: "TITLE", backtrace: nil}, transport: subject}).and_call_original + + subject.handle_error(RuntimeError.new("ERROR")) subject.instance_variable_set(:@error, nil) - subject.handle_error({title: "TITLE", status: :forbidden, error: "ERROR"}, :json) + subject.handle_error({title: "TITLE", status: :forbidden, error: "ERROR"}, format: :json) end it "should render a HTML error" do - allow(subject).to receive(:request).and_return(OpenStruct.new(format: :html)) + expect(subject).to receive(:ajax_request?).and_return(false) + expect(subject).to receive(:request).and_return(OpenStruct.new(format: "HTML")) expect(subject).to receive(:render).with(html: "", status: 500, layout: "LAYOUT", formats: [:html]) - subject.handle_error(RuntimeError.new("ERROR"), "LAYOUT") + subject.handle_error(RuntimeError.new("ERROR"), layout: "LAYOUT") end end end