Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--color
20 changes: 20 additions & 0 deletions models/automobile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require_relative 'Vehicle'

class Automobile < Vehicle

attr_reader :color, :make, :model, :year

def initialize(color, make, model, year)
@color = color
@make = make
@model = model
@year = year
end

def update(args)
@color = args.fetch(:color) if args[:color]
@make = args.fetch(:make) if args[:make]
@model = args.fetch(:model) if args[:model]
@year = args.fetch(:year) if args[:year]
end
end
9 changes: 9 additions & 0 deletions models/motorcycle.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require_relative 'vehicle'

class Motorcycle < Vehicle

def self.number_of_wheels
2
end

end
13 changes: 13 additions & 0 deletions models/vehicle.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Vehicle

@@vehicles_made ||= 0

def initialize
@@vehicles_made += 1
end

def self.number_of_wheels
4
end

end
45 changes: 45 additions & 0 deletions spec/automobile_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require 'spec_helper'

describe Automobile do

let(:auto) { Automobile.new("Yellow", "Honda", "Accord", "1988")}

it "returns the number of wheels for its class" do
Automobile.number_of_wheels.should eq(4)
end

it "has a color" do
auto.color.should eq("Yellow")
end

it "has a make" do
auto.make.should eq("Honda")
end

it "has a model" do
auto.model.should eq("Accord")
end

it "has a year" do
auto.year.should eq("1988")
end

it "can have one of its attributes updated by hash" do
auto.update(color: "Blue")
auto.color.should eq("Blue")
end

it "can have many of its attributes updated by hash" do
auto.update(color: "Red", year: "1993")
auto.color.should eq("Red")
auto.year.should eq("1993")
end

it "can have all of its attributes updated by hash" do
auto.update(color: "Green", model: "Camry", make: "Toyota", year: "2003")
auto.color.should eq("Green")
auto.make.should eq("Toyota")
auto.model.should eq("Camry")
auto.year.should eq("2003")
end
end
13 changes: 13 additions & 0 deletions spec/motorcycle_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'spec_helper'

describe Motorcycle do

it "does not have 4 tires" do
Motorcycle.number_of_wheels.should_not eq(4)
end

it "has 2 tires" do
Vehicle.number_of_wheels.should eq(4)
Motorcycle.number_of_wheels.should eq(2)
end
end
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'rspec'
require_relative '../models/automobile'
require_relative '../models/vehicle'
require_relative '../models/motorcycle'
8 changes: 8 additions & 0 deletions spec/vehicle_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'spec_helper'

describe Vehicle do

it "tracks the number of vehicles created" do
Motorcycle.new.should change{@@vehicles_made}.by(1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's start by getting the correct test.

You have added a test that checks a global variable @@vehicles_made --- not the Vehicle's class variable @@vehicles_made

So, our test should reference that. In additional, the "should change" requires a bit of a different syntax

describe Vehicle do 
    it "tracks the number of vehicles created" do
      expect {
        Motorcycle.new
      } .to change{Vehicle.vehicle_count}.by(1)
    end
end

Then, to get this to pass, we add a method to Vehicle

    def self.vehicle_count
      @@vehicles_made
    end

end
end