Created
April 13, 2013 22:53
-
-
Save rguerrettaz/5380457 to your computer and use it in GitHub Desktop.
Rspec example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rspec' | |
require_relative '../assessment_week2' | |
describe Vehicle do | |
subject { Vehicle.new(Hash[color: "black", wheels: 2]) } | |
it { should be_instance_of Vehicle } | |
its (:wheels) { should == 2 } | |
its (:color) { should == "black" } | |
it "should drive" do | |
subject.drive | |
subject.status.should eq(:driving) | |
end | |
it "should brake" do | |
subject.brake | |
subject.status.should eq(:stopped) | |
end | |
it "should know if it needs gas" do | |
result = subject.needs_gas? | |
[TrueClass, FalseClass].should include(result.class) | |
end | |
end | |
describe Car do | |
subject { Car.new(Hash[color: "black", wheels: 4]) } | |
it { should be_instance_of Car } | |
its (:wheels) { should == 4 } | |
its (:color) { should == "black" } | |
it "should drive" do | |
subject.drive | |
subject.status.should eq(:driving) | |
end | |
it "should brake" do | |
subject.brake | |
subject.status.should eq(:stopped) | |
end | |
it "should know if it needs gas" do | |
result = subject.needs_gas? | |
[TrueClass, FalseClass].should include(result.class) | |
end | |
end | |
describe Bus do | |
subject { Bus.new(Hash[color: "black", wheels: 12, num_seats: 36, fare: 2]) } | |
it { should be_instance_of Bus } | |
its (:wheels) { should == 12 } | |
its (:color) { should == "black" } | |
its (:num_seats) { should == 36 } | |
its (:fare) { should == 2 } | |
it "should drive if stop_requested is false" do | |
# This doesn't work properly. Needs to be refactored. | |
if subject.stop_requested? | |
subject.drive | |
subject.status.should eq(:driving) | |
else | |
nil | |
end | |
end | |
it "should brake" do | |
subject.brake | |
subject.status.should eq(:stopped) | |
end | |
it "should know if it needs gas" do | |
result = subject.needs_gas? | |
[TrueClass, FalseClass].should include(result.class) | |
end | |
# This doesn't work right either. Not sure why. Guessing current_passengers is | |
# getting overwritten when it is called. | |
it "should admit passengers if money greater than fare" do | |
current_passengers = :passengers.length | |
puts current_passengers | |
subject.admit_passenger("ryan", 3) | |
current_passengers.should_not eq(:passengers.length) | |
end | |
it "should NOT admit passengers if money less than fare" do | |
current_passengers = :passengers.length | |
subject.admit_passenger("ryan", 1) | |
current_passengers.should eq(:passengers.length) | |
end | |
it "should know if stop has been requested" do | |
result = subject.stop_requested? | |
[TrueClass, FalseClass].should include(result.class) | |
end | |
end | |
describe Motorbike do | |
subject { Motorbike.new(Hash[color: "black"]) } | |
it { should be_instance_of Motorbike } | |
its (:wheels) { should == 2 } | |
its (:color) { should == "black" } | |
it "should drive" do | |
subject.drive | |
subject.status.should eq(:driving) | |
subject.speed.should eq(:fast) | |
end | |
it "should brake" do | |
subject.brake | |
subject.status.should eq(:stopped) | |
end | |
it "should know if it needs gas" do | |
result = subject.needs_gas? | |
[TrueClass, FalseClass].should include(result.class) | |
end | |
it "should weave through traffic" do | |
subject.weave_through_traffic | |
subject.status.should eq(:driving_like_a_crazy_person) | |
end | |
end | |
### 1.2 | |
# Refactored Code into Hierarchical structure |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment