# encoding: UTF-8
require 'spec_helper'
describe Correios::Frete::Servico do
before(:each) { @servico = Correios::Frete::Servico.new }
describe "#parse" do
context "when service exists" do
before :each do
@xml = """
41106
15,70
3
3,75
1,99
1,50
S
N
-3
Somente para teste
"""
end
{ :tipo => :pac,
:nome => "PAC",
:descricao => "PAC sem contrato",
:codigo => "41106",
:valor => 15.70,
:prazo_entrega => 3,
:valor_mao_propria => 3.75,
:valor_aviso_recebimento => 1.99,
:valor_valor_declarado => 1.50,
:entrega_domiciliar => true,
:entrega_sabado => false,
:erro => "-3",
:msg_erro => "Somente para teste"
}.each do |attr, value|
it "sets #{attr} to #{value}" do
@servico.parse @xml
expect(@servico.send(attr)).to eq(value)
end
end
end
context "when service does not exist" do
before :each do
@xml = """
99999
0,00
0
0,00
0,00
0,00
-1
Codigo de servico invalido
"""
end
{ :tipo => nil,
:nome => nil,
:codigo => "99999",
:valor => 0.0,
:prazo_entrega => 0,
:valor_mao_propria => 0.0,
:valor_aviso_recebimento => 0.0,
:valor_valor_declarado => 0.0,
:entrega_domiciliar => false,
:entrega_sabado => false,
:erro => "-1",
:msg_erro => "Codigo de servico invalido"
}.each do |attr, value|
it "sets #{attr} to #{value}" do
@servico.parse @xml
expect(@servico.send(attr)).to eq(value)
end
end
end
context "when there is an unexpected error" do
before :each do
@xml = """
99
Input string was not in a correct format.
"""
end
{ :tipo => nil,
:nome => nil,
:codigo => nil,
:valor => 0.0,
:prazo_entrega => 0,
:valor_mao_propria => 0.0,
:valor_aviso_recebimento => 0.0,
:valor_valor_declarado => 0.0,
:entrega_domiciliar => false,
:entrega_sabado => false,
:erro => "99",
:msg_erro => "Input string was not in a correct format."
}.each do |attr, value|
it "sets #{attr} to #{value}" do
@servico.parse @xml
expect(@servico.send(attr)).to eq(value)
end
end
end
end
describe "#success?" do
context "when freight was calculated" do
it "returns true" do
@servico.parse "01013,70"
expect(@servico.success?).to eql true
end
end
context "when freight was not calculated" do
it "returns false" do
@servico.parse "00,00"
expect(@servico.success?).to eql false
end
end
end
describe "#error?" do
context "when freight was not calculated" do
it "returns true" do
@servico.parse "00,00"
expect(@servico.error?).to eql true
end
end
context "when freight was calculated" do
it "returns false" do
@servico.parse "01013,70"
expect(@servico.error?).to eql false
end
end
end
describe ".code_from_type" do
Correios::Frete::Servico::AVAILABLE_SERVICES.each do |code, value|
context "to #{value[:type]} type" do
it "returns #{code} code" do
expect(Correios::Frete::Servico.code_from_type(value[:type])).to eq(code)
end
end
end
context "when type does not exist" do
it "returns nil" do
expect(Correios::Frete::Servico.code_from_type(:nao_existe)).to be_nil
end
end
end
end