# encoding: UTF-8 require 'spec_helper' describe Correios::Frete do describe ".new" do context "create with default value of" do before(:each) { @frete = Correios::Frete.new } { :peso => 0.0, :comprimento => 0.0, :altura => 0.0, :largura => 0.0, :diametro => 0.0, :formato => :caixa_pacote, :mao_propria => false, :aviso_recebimento => false, :valor_declarado => 0.0 }.each do |attr, value| it attr do @frete.send(attr).should == value end end end { :cep_origem => "01000-000", :cep_destino => "021222-222", :peso => 0.321, :comprimento => 12.5, :altura => 1.4, :largura => 4.6, :diametro => 5.0, :formato => :rolo_prisma, :mao_propria => true, :aviso_recebimento => true, :valor_declarado => 1.99, :codigo_empresa => "1234567890", :senha => "senha", :web_service => Correios::Frete::WebService.new, :parser => Correios::Frete::Parser.new }.each do |attr, value| context "when #{attr} is supplied" do it "sets #{attr}" do @frete = Correios::Frete.new(attr => value) @frete.send(attr).should == value end end context "when #{attr} is supplied in a block" do it "sets #{attr}" do @frete = Correios::Frete.new { |f| f.send("#{attr}=", value) } @frete.send(attr).should == value end end end end describe "#calcular" do before :each do @web_service = Correios::Frete::WebService.new @parser = Correios::Frete::Parser.new @frete = Correios::Frete.new(:web_service => @web_service, :parser => @parser) end context "to many services" do before :each do @xml = '4110615,7033,700,001,50SN04001017,8013,700,001,50SS0' @servicos = { :pac => Correios::Frete::Servico.new, :sedex => Correios::Frete::Servico.new } @parser.stub(:servicos).and_return(@servicos) @web_service.stub(:request).with(@frete, [:pac, :sedex]).and_return(@xml) end it "returns all services" do @frete.calcular(:pac, :sedex).should == @servicos end end context "to one service" do before :each do @xml = '4001017,8013,700,001,50SS0' @servico = Correios::Frete::Servico.new @parser.stub(:servicos).and_return(:sedex => @servico) @web_service.stub(:request).with(@frete, [:sedex]).and_return(@xml) end it "returns only one service" do @frete.calcular(:sedex).should == @servico end end end end