Exécution quelques tests et d'obtenir la même erreur sur chaque élément:`(3 niveaux) dans <top (obligatoire)> »
Failures:
1) Register Frozen Pizza should be $5.00 each
Failure/Error: expect(subject.total).to eq 5.00
expected: 5.0
got: {"Frozen Pizza"=>1}
(compared using ==)
# ./register_spec.rb:11:in `block (3 levels) in <top (required)>'
2) Register Corn should be $0.50 each
Failure/Error: expect(subject.total).to eq 0.50
expected: 0.5
got: {"Corn"=>1}
(compared using ==)
# ./register_spec.rb:20:in `block (3 levels) in <top (required)>'
Aucune idée de ce qui cause ces 16 erreurs. Voici une partie du fichier Spec:
require 'register'
require 'timecop'
describe Register do
subject { described_class.new }
describe 'Frozen Pizza' do
it 'should be $5.00 each' do
subject.ring_up('Frozen Pizza', quantity: 1)
expect(subject.total).to eq 5.00
subject.ring_up('Frozen Pizza', quantity: 1)
expect(subject.total).to eq 10.00
end
end
describe 'Corn' do
it 'should be $0.50 each' do
subject.ring_up('Corn', quantity: 1)
expect(subject.total).to eq 0.50
end
it 'should be 5 for $2.00' do
subject.ring_up('Corn', quantity: 5)
expect(subject.total).to eq 2.00
subject.ring_up('Corn', quantity: 1)
expect(subject.total).to eq 2.50
end
end
I Comprise parties de la spécification, mais si je dois je modifier le code complet.
fichier Register.rb:
class Register
attr :items
def initialize
@items = Hash.new(0)
end
def ring_up(item, args)
@items[item] += args[:quantity]
end
def total
total = 0
items.each do |item, quantity|
case item
when 'Frozen Pizza'
total += quantity * 5.00
when 'Corn'
if (quantity % 5 == 0)
total += quantity/5 * 2.00
else
bundles = quantity/5
singles = quantity % 5
total += bundles * 2.00 + singles * 0.50
end
when 'Ground Beef'
total += (quantity * 4.99).round(2)
when /Mayfield Ice Cream/
if (quantity % 2 == 0)
if item =~ /^Chocolate/
if Time.now.strftime("%A") == 'Wednesday'
total += quantity/2 * 5.99
else
total += quantity * 5.99
end
else
total += quantity * 5.99
end
else
if item =~ /^Chocolate/
if Time.now.strftime("%A") == 'Wednesday'
total += (quantity/2) * 5.99 + (quantity % 2) * 5.99
else
total += quantity * 5.99
end
else
total += quantity * 5.99
end
end
when 'Mango'
if Time.now.strftime("%A") == 'Monday'
total += quantity * 0.50
else
total += quantity * 1.00
end
#Additions
when 'Mahi Fillet'
total += quantity * 7.99
when 'Mac & Cheese'
if (quantity % 10 == 0)
total += quantity/10 * 1.00
else
total += quantity/1 * 1.29
end
end
total
end
end
end
C'est un excellent exemple des raisons pour lesquelles les questions de longueur de la méthode! –
A fonctionné parfaitement :) – DillonCarter
@ChrisHeald: et le formatage approprié. Je suis toujours étonné de voir comment les gens font leur vie, juste en étant trop paresseux pour taper Alt + Maj + F. –