在<top(required)=“”> </top>中阻止(3个级别)

时间:2015-03-17 23:45:56

标签: ruby

运行一些测试并在每个项目上获得相同的错误:

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)>'

不知道造成这16个错误的原因。 以下是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

我只包含规范的部分内容,但如果我需要,我将使用完整代码进行编辑。

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

1 个答案:

答案 0 :(得分:4)

很难看到,因为该方法很长且缩进不明确,但您需要将要返回的total移动到total方法的最后一行。改变

    end
    total
  end

  end
end

为:

      end
    end
    total
  end

end