require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe Mooamba::Sedex do before :all do @default_parameters = { :from => VALID_ZIP_CODES.first, :to => VALID_ZIP_CODES.last, :weight => 1 } map_parameters_to_file(@default_parameters, "valid") end context "on .calculdate" do it "should use the Sedex service" do Mooamba::Sedex.should_receive(:open).with(/nCdServico\=40010/).and_return(valid_xml_file) Mooamba::Sedex.calculate(@default_parameters) end it "should return the price as #price" do Mooamba::Sedex.calculate(@default_parameters).price.should eql "11,80" end it "should raise Mooamba::InvalidOriginZipCode when there's no origin zip code" do lambda { Mooamba::Sedex.calculate(@default_parameters.except(:from)) }.should raise_error(Mooamba::InvalidOriginZipCode) end it "should raise Mooamba::InvalidDestinationZipCode when there's no destiny zip code" do lambda { Mooamba::Sedex.calculate(@default_parameters.except(:to)) }.should raise_error(Mooamba::InvalidDestinationZipCode) end it "should raise Mooamba::InvalidOriginZipCode when the origin zip code is blank" do lambda { Mooamba::Sedex.calculate(@default_parameters.merge(:from => "")) }.should raise_error(Mooamba::InvalidOriginZipCode) end it "should raise Mooamba::InvalidDestinationZipCode when the destiny zip code is blank" do lambda { Mooamba::Sedex.calculate(@default_parameters.merge(:to => "")) }.should raise_error(Mooamba::InvalidDestinationZipCode) end it "should raise Mooamba::InvalidOriginZipCode when the origin zip code is invalid" do lambda { Mooamba::Sedex.calculate(@default_parameters.merge(:from => INVALID_ZIP_CODES.last)) }.should raise_error(Mooamba::InvalidOriginZipCode) end it "should raise Mooamba::InvalidDestinationZipCode when the destiny zip code is invalid" do lambda { Mooamba::Sedex.calculate(@default_parameters.merge(:to => INVALID_ZIP_CODES.first)) }.should raise_error(Mooamba::InvalidDestinationZipCode) end it "should raise Mooamba::UnavailableAPI when api returns unexpected content" do map_parameters_to_file(@default_parameters, "malformed") lambda { Mooamba::Sedex.calculate(@default_parameters) }.should raise_error(Mooamba::UnavailableAPI) end it "should raise Mooamba::Correios::UnavaibleService when api returns service unavailable" do map_parameters_to_file(@default_parameters, "unavailable_service") lambda { Mooamba::Sedex.calculate(@default_parameters) }.should raise_error(Mooamba::Correios::UnavailableService) end it "should raise Mooamba::Correios::InvalidOriginZipCode when the invalid origin zip code doesn't exist" do map_parameters_to_file(@default_parameters, "invalid_origin") lambda { Mooamba::Sedex.calculate(@default_parameters) }.should raise_error(Mooamba::Correios::InvalidOriginZipCode) end it "should raise Mooamba::Correios::InvalidDestinyZipCode when the invalid destiny zip code doesn't exist" do map_parameters_to_file(@default_parameters, "invalid_destiny") lambda { Mooamba::Sedex.calculate(@default_parameters) }.should raise_error(Mooamba::Correios::InvalidDestinyZipCode) end it "should raise Mooamba::Correios::InvalidWeight when the weight if excessive" do map_parameters_to_file(@default_parameters, "invalid_weight") lambda { Mooamba::Sedex.calculate(@default_parameters) }.should raise_error(Mooamba::Correios::InvalidWeight) end it "should raise Mooamba::Correios::UnavailableServiceForArea when the service is unavailable for the current area" do map_parameters_to_file(@default_parameters, "unavailable_service_for_area") lambda { Mooamba::Sedex.calculate(@default_parameters) }.should raise_error(Mooamba::Correios::UnavailableServiceForArea) end it "should raise Mooamba::Correios::UnknownError when an unknown error happened" do map_parameters_to_file(@default_parameters, "invalid") lambda { Mooamba::Sedex.calculate(@default_parameters) }.should raise_error(Mooamba::Correios::UnknownError) end end end