Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Oystercard
- MAXIMUM_BALANCE = 90
- attr_reader :balance, :top_up
- attr_accessor :in_journey
- def initialize
- @balance = 0
- @in_journey = false
- end
- def top_up(amount)
- raise "Error. You cannot top up in excess of £#{MAXIMUM_BALANCE}." if amount + @balance > MAXIMUM_BALANCE
- @balance += amount
- end
- def deduct(amount)
- @balance -= amount
- end
- def in_journey?
- @in_journey = false
- end
- def touch_in
- @in_journey = true
- end
- def touch_out
- @in_journey = false
- end
- end
- #RSPEC TEST BELOW
- require 'oystercard'
- describe Oystercard do
- context "when initializing" do
- it "has an initial balance of 0" do
- expect(subject.balance).to eq(0)
- end
- end
- context "when topping up @balance" do
- it "#top_up takes an amount argument" do
- expect(subject).to respond_to(:top_up).with(1).argument
- end
- it "#top_up increments the balance" do
- expect{subject.top_up(10)}.to change{subject.balance}.by(10)
- end
- it "#top_up throws an exception if balance exceed £90" do
- maximum_balance = Oystercard::MAXIMUM_BALANCE
- subject.top_up(maximum_balance)
- expect{ subject.top_up(1) }.to raise_error("Error. You cannot top up in excess of £#{maximum_balance}.")
- end
- end
- context "when deducting @balance" do
- it "#deduct decrements the balance by the amount specified" do
- expect{subject.deduct(10)}.to change{subject.balance}.by(-10)
- end
- end
- context "when touching in and touching out" do
- it "#in_journey? returns if a journey is in progress" do
- expect(subject).not_to be_in_journey
- end
- ####--------------[THIS EXAMPLES SEEMS TO FAIL]-------------
- it "#touch_in returns that a journey is in progress - true " do
- subject.touch_in
- expect(subject).to be_in_journey
- end
- ####---------------------------------------------------------
- it "#touch_out returns that a journey is no longer in progress - false" do
- subject.touch_out
- expect(subject).not_to be_in_journey
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement