Advertisement
Guest User

oystercard.rb

a guest
Aug 15th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.08 KB | None | 0 0
  1. class Oystercard
  2.  
  3.   MAXIMUM_BALANCE = 90
  4.  
  5.   attr_reader :balance, :top_up
  6.   attr_accessor :in_journey
  7.  
  8.   def initialize
  9.     @balance = 0
  10.     @in_journey = false
  11.   end
  12.  
  13.   def top_up(amount)
  14.     raise "Error. You cannot top up in excess of £#{MAXIMUM_BALANCE}." if amount + @balance > MAXIMUM_BALANCE
  15.     @balance += amount
  16.   end
  17.  
  18.   def deduct(amount)
  19.     @balance -= amount
  20.   end
  21.  
  22.   def in_journey?
  23.     @in_journey = false
  24.   end
  25.  
  26.   def touch_in
  27.     @in_journey = true
  28.   end
  29.  
  30.   def touch_out
  31.     @in_journey = false
  32.   end
  33.  
  34. end
  35.  
  36. #RSPEC TEST BELOW
  37.  
  38. require 'oystercard'
  39.  
  40. describe Oystercard do
  41.  
  42.   context "when initializing" do
  43.     it "has an initial balance of 0" do
  44.       expect(subject.balance).to eq(0)
  45.     end
  46.   end
  47.  
  48.   context "when topping up @balance" do
  49.     it "#top_up takes an amount argument" do
  50.       expect(subject).to respond_to(:top_up).with(1).argument
  51.     end
  52.  
  53.     it "#top_up increments the balance" do
  54.       expect{subject.top_up(10)}.to change{subject.balance}.by(10)
  55.     end
  56.  
  57.     it "#top_up throws an exception if balance exceed £90" do
  58.       maximum_balance = Oystercard::MAXIMUM_BALANCE
  59.       subject.top_up(maximum_balance)
  60.       expect{ subject.top_up(1) }.to raise_error("Error. You cannot top up in excess of £#{maximum_balance}.")
  61.     end
  62.   end
  63.  
  64.   context "when deducting @balance" do
  65.     it "#deduct decrements the balance by the amount specified" do
  66.       expect{subject.deduct(10)}.to change{subject.balance}.by(-10)
  67.     end
  68.   end
  69.  
  70.   context "when touching in and touching out" do
  71.     it "#in_journey? returns if a journey is in progress" do
  72.       expect(subject).not_to be_in_journey
  73.     end
  74. ####--------------[THIS EXAMPLES SEEMS TO FAIL]-------------
  75.     it "#touch_in returns that a journey is in progress - true " do
  76.       subject.touch_in
  77.       expect(subject).to be_in_journey
  78.     end
  79. ####---------------------------------------------------------
  80.     it "#touch_out returns that a journey is no longer in progress - false" do
  81.       subject.touch_out
  82.       expect(subject).not_to be_in_journey
  83.     end
  84.   end
  85. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement