From 7617310ca000c1f06cfdea49c2e724b4c8d0d250 Mon Sep 17 00:00:00 2001 From: drammopo Date: Mon, 3 Feb 2014 22:22:27 +0200 Subject: [PATCH 01/19] Panda Assignment submission. --- models/panda.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 models/panda.rb diff --git a/models/panda.rb b/models/panda.rb new file mode 100644 index 0000000..e65fa43 --- /dev/null +++ b/models/panda.rb @@ -0,0 +1,13 @@ +class Automobile + attr_accessor :color, :make, :model, :year + def self.wheels? + 4 + end + + def initialize(args) + @color = args[:color] + @make = args[:color] + @model = args[:color] + @year = args[:color] + end +end From 66d4a2359c03ff022a634868901bb32ba967938e Mon Sep 17 00:00:00 2001 From: drammopo Date: Mon, 3 Feb 2014 22:23:58 +0200 Subject: [PATCH 02/19] Corrections to Panda Assignment submission. --- models/panda.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/models/panda.rb b/models/panda.rb index e65fa43..a22bc5a 100644 --- a/models/panda.rb +++ b/models/panda.rb @@ -6,8 +6,8 @@ def self.wheels? def initialize(args) @color = args[:color] - @make = args[:color] - @model = args[:color] - @year = args[:color] + @make = args[:make] + @model = args[:model] + @year = args[:year] end end From 86755bae43d5b8dce7aed037cdd32c128cbfa9f6 Mon Sep 17 00:00:00 2001 From: drammopo Date: Mon, 3 Feb 2014 22:37:35 +0200 Subject: [PATCH 03/19] Tiger Assignment submission. --- models/tiger.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 models/tiger.rb diff --git a/models/tiger.rb b/models/tiger.rb new file mode 100644 index 0000000..2d6e90a --- /dev/null +++ b/models/tiger.rb @@ -0,0 +1,25 @@ +class Vehicle + def tires + end +end + +class Automobile < Vehicle + + attr_accessor :color, :make, :model, :year + def self.wheels? + 4 + end + + def initialize(args) + @color = args[:color] + @make = args[:make] + @model = args[:model] + @year = args[:year] + end +end + +class Motorcycle < Vehicle + def tires + return 2 + end +end From 4344aacda624e9c2179bc8ab064b732c10cb42fe Mon Sep 17 00:00:00 2001 From: drammopo Date: Tue, 4 Feb 2014 18:34:37 +0200 Subject: [PATCH 04/19] Panda and Tiger assignment corrections. --- models/panda.rb | 4 +++- models/tiger.rb | 10 +++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/models/panda.rb b/models/panda.rb index a22bc5a..bcc9a8a 100644 --- a/models/panda.rb +++ b/models/panda.rb @@ -1,6 +1,6 @@ class Automobile attr_accessor :color, :make, :model, :year - def self.wheels? + def self.wheels 4 end @@ -11,3 +11,5 @@ def initialize(args) @year = args[:year] end end + +puts Automobile.wheels diff --git a/models/tiger.rb b/models/tiger.rb index 2d6e90a..2634d4f 100644 --- a/models/tiger.rb +++ b/models/tiger.rb @@ -1,12 +1,13 @@ class Vehicle - def tires + def self.wheels + 2 end end class Automobile < Vehicle attr_accessor :color, :make, :model, :year - def self.wheels? + def self.wheels 4 end @@ -19,7 +20,6 @@ def initialize(args) end class Motorcycle < Vehicle - def tires - return 2 - end end + +puts Motorcycle.wheels From 93e0222c8ed76c0367833db2f4099375557794f7 Mon Sep 17 00:00:00 2001 From: drammopo Date: Tue, 4 Feb 2014 19:25:44 +0200 Subject: [PATCH 05/19] Added update feature to Automobile class. --- models/tiger.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/models/tiger.rb b/models/tiger.rb index 2634d4f..d28f6d8 100644 --- a/models/tiger.rb +++ b/models/tiger.rb @@ -17,9 +17,23 @@ def initialize(args) @model = args[:model] @year = args[:year] end + + def update(new_args) + @color = new_args[:color] + @make = new_args[:make] + @model = new_args[:model] + @year = new_args[:year] + end end class Motorcycle < Vehicle end puts Motorcycle.wheels + +auto = Automobile.new(color: 'Red', make: 'Ford', model: 'Mustang', year: 2007) +p auto +auto.update(color: 'Blue', year: 2012) +puts auto.color +puts auto.year + From 4fa755a19fa9b0dece442b0a12995164ceecc1c8 Mon Sep 17 00:00:00 2001 From: drammopo Date: Tue, 4 Feb 2014 19:54:39 +0200 Subject: [PATCH 06/19] Stopped update method from resetting instance variables if set. --- models/tiger.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/models/tiger.rb b/models/tiger.rb index d28f6d8..753d3c6 100644 --- a/models/tiger.rb +++ b/models/tiger.rb @@ -19,10 +19,10 @@ def initialize(args) end def update(new_args) - @color = new_args[:color] - @make = new_args[:make] - @model = new_args[:model] - @year = new_args[:year] + @color ||= new_args[:color] + @make ||= new_args[:make] + @model ||= new_args[:model] + @year ||= new_args[:year] end end @@ -36,4 +36,5 @@ class Motorcycle < Vehicle auto.update(color: 'Blue', year: 2012) puts auto.color puts auto.year +puts auto.model From 31b8b38b3b2f88fd0681c9b6ebfbd11604276320 Mon Sep 17 00:00:00 2001 From: drammopo Date: Tue, 4 Feb 2014 20:13:12 +0200 Subject: [PATCH 07/19] Feature: Allow overwrite on update. --- models/tiger.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/models/tiger.rb b/models/tiger.rb index 753d3c6..ebf0dc7 100644 --- a/models/tiger.rb +++ b/models/tiger.rb @@ -19,10 +19,10 @@ def initialize(args) end def update(new_args) - @color ||= new_args[:color] - @make ||= new_args[:make] - @model ||= new_args[:model] - @year ||= new_args[:year] + @color = new_args[:color] if new_args[:color] + @make ||= new_args[:make] if new_args[:make] + @model ||= new_args[:model] if new_args[:model] + @year ||= new_args[:year] if new_args[:year] end end From c5bf5c3f74abf759a935a0a4f1f7fa311b64dbaf Mon Sep 17 00:00:00 2001 From: drammopo Date: Tue, 4 Feb 2014 20:27:27 +0200 Subject: [PATCH 08/19] Feature: Fixed typos. --- models/tiger.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/models/tiger.rb b/models/tiger.rb index ebf0dc7..6f63eb9 100644 --- a/models/tiger.rb +++ b/models/tiger.rb @@ -20,9 +20,9 @@ def initialize(args) def update(new_args) @color = new_args[:color] if new_args[:color] - @make ||= new_args[:make] if new_args[:make] - @model ||= new_args[:model] if new_args[:model] - @year ||= new_args[:year] if new_args[:year] + @make = new_args[:make] if new_args[:make] + @model = new_args[:model] if new_args[:model] + @year = new_args[:year] if new_args[:year] end end From 3a2f8004d02797df891304980899b537dfd72773 Mon Sep 17 00:00:00 2001 From: drammopo Date: Thu, 6 Feb 2014 19:36:41 +0200 Subject: [PATCH 09/19] Eagle level: Create a class variable (@@) in the Vehicle that tracks all vehicles made. --- models/eagle.rb | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 models/eagle.rb diff --git a/models/eagle.rb b/models/eagle.rb new file mode 100644 index 0000000..daf6c6b --- /dev/null +++ b/models/eagle.rb @@ -0,0 +1,53 @@ +class Vehicle + @@count = 0 + def initialize + @@count += 1 + end + + def self.count + @@count + end + + def self.count=(value) + @@count = value + end + + def self.wheels + 2 + end +end + +class Automobile < Vehicle + + attr_accessor :color, :make, :model, :year + def self.wheels + 4 + end + + def initialize(args) + @color = args[:color] + @make = args[:make] + @model = args[:model] + @year = args[:year] + end + + def update(new_args) + @color = new_args[:color] if new_args[:color] + @make = new_args[:make] if new_args[:make] + @model = new_args[:model] if new_args[:model] + @year = new_args[:year] if new_args[:year] + end +end + +class Motorcycle < Vehicle +end + +#v = Vehicle.new + +8.times { Vehicle.new } +auto = Automobile.new(color: 'Red', make: 'Ford', model: 'Mustang', year: 2007) +4.times { Motorcycle.new } +puts Vehicle.count + + + From 73bb8b6608335dea2a58f016674637c6a687c0f0 Mon Sep 17 00:00:00 2001 From: drammopo Date: Thu, 6 Feb 2014 20:06:27 +0200 Subject: [PATCH 10/19] Eagle level: Corrected Automobile super method call to take zero arguments. --- models/eagle.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/eagle.rb b/models/eagle.rb index daf6c6b..df77014 100644 --- a/models/eagle.rb +++ b/models/eagle.rb @@ -29,6 +29,7 @@ def initialize(args) @make = args[:make] @model = args[:model] @year = args[:year] + super() end def update(new_args) @@ -42,7 +43,6 @@ def update(new_args) class Motorcycle < Vehicle end -#v = Vehicle.new 8.times { Vehicle.new } auto = Automobile.new(color: 'Red', make: 'Ford', model: 'Mustang', year: 2007) From cb981b992d56e512461cb9c6a2c1807bbddafcf2 Mon Sep 17 00:00:00 2001 From: drammopo Date: Thu, 6 Feb 2014 22:02:11 +0200 Subject: [PATCH 11/19] Eagle level: First attempt at Vehicle search method based on https://gist.github.com/jwo/8526339 --- models/eagle.rb | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/models/eagle.rb b/models/eagle.rb index df77014..4f3a13b 100644 --- a/models/eagle.rb +++ b/models/eagle.rb @@ -1,7 +1,11 @@ class Vehicle + @@count = 0 + @@vehicles = [] + #attr_reader :vehicles def initialize @@count += 1 + @vehicles = [] end def self.count @@ -12,11 +16,26 @@ def self.count=(value) @@count = value end + def self.search(args) + + results = vehicles + args.each do |key, value| + results.select!{|vehicle| vehicle.send(key) == value.to_sym} + end + results + + end + + def self.register(automobile) + vehicles << automobile + end + def self.wheels 2 end end + class Automobile < Vehicle attr_accessor :color, :make, :model, :year @@ -29,6 +48,7 @@ def initialize(args) @make = args[:make] @model = args[:model] @year = args[:year] + Vehicle.register(self) super() end @@ -45,9 +65,12 @@ class Motorcycle < Vehicle 8.times { Vehicle.new } -auto = Automobile.new(color: 'Red', make: 'Ford', model: 'Mustang', year: 2007) 4.times { Motorcycle.new } -puts Vehicle.count - - +auto = Automobile.new(model: 'Mustang', color: 'Red', make: 'Ford', year: 2007) +Automobile.new model: :mazda, color: :red, make: 'Civic', year: 2012 +Automobile.new model: :toyota, color: :red, make: 'Corolla', year: 2010 +Automobile.new model: :honda, color: :blue, make: 'Civic', year: 2013 +Automobile.new model: :mazda, color: :blue, make: 'Accord', year: 2003 +puts Vehicle.count +puts Vehicle.search(colour: "blue", model: "honda").inspect From 4fba59b26bbab3fdb5df70de4a1b8be40dc4b850 Mon Sep 17 00:00:00 2001 From: drammopo Date: Thu, 6 Feb 2014 22:17:25 +0200 Subject: [PATCH 12/19] Eagle level: Implemented Vehicle search method. --- models/eagle.rb | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/models/eagle.rb b/models/eagle.rb index 4f3a13b..89e51bf 100644 --- a/models/eagle.rb +++ b/models/eagle.rb @@ -1,24 +1,17 @@ class Vehicle - @@count = 0 @@vehicles = [] - #attr_reader :vehicles def initialize - @@count += 1 - @vehicles = [] - end - def self.count - @@count end - def self.count=(value) - @@count = value + def self.count + @@vehicles.count end def self.search(args) - results = vehicles + results = @@vehicles args.each do |key, value| results.select!{|vehicle| vehicle.send(key) == value.to_sym} end @@ -27,7 +20,7 @@ def self.search(args) end def self.register(automobile) - vehicles << automobile + @@vehicles << automobile end def self.wheels @@ -63,9 +56,6 @@ def update(new_args) class Motorcycle < Vehicle end - -8.times { Vehicle.new } -4.times { Motorcycle.new } auto = Automobile.new(model: 'Mustang', color: 'Red', make: 'Ford', year: 2007) Automobile.new model: :mazda, color: :red, make: 'Civic', year: 2012 Automobile.new model: :toyota, color: :red, make: 'Corolla', year: 2010 @@ -73,4 +63,4 @@ class Motorcycle < Vehicle Automobile.new model: :mazda, color: :blue, make: 'Accord', year: 2003 puts Vehicle.count -puts Vehicle.search(colour: "blue", model: "honda").inspect +puts Vehicle.search(color: "blue", model: "honda").inspect From d7b7038e354ccf71e80d112f7f6484d14e6c8579 Mon Sep 17 00:00:00 2001 From: drammopo Date: Thu, 6 Feb 2014 22:52:52 +0200 Subject: [PATCH 13/19] Eagle level: Added a more dynamic 'register' method. --- models/eagle.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/models/eagle.rb b/models/eagle.rb index 89e51bf..16b0b5a 100644 --- a/models/eagle.rb +++ b/models/eagle.rb @@ -30,7 +30,7 @@ def self.wheels class Automobile < Vehicle - + @@automobiles = [] attr_accessor :color, :make, :model, :year def self.wheels 4 @@ -51,6 +51,13 @@ def update(new_args) @model = new_args[:model] if new_args[:model] @year = new_args[:year] if new_args[:year] end + + def self.build_car(args) + auto = Automobile.new(args) + @@vehicles << auto + auto + end + end class Motorcycle < Vehicle From 8af26ed5f22ef442042e9cb206512bfac586b4e3 Mon Sep 17 00:00:00 2001 From: drammopo Date: Thu, 6 Feb 2014 23:03:25 +0200 Subject: [PATCH 14/19] Eagle level: Added first RSpec test for Motorcycle class. --- spec/automobile_spec.rb | 0 spec/motorcycle_spec.rb | 11 +++++++++++ spec/vehicle_spec.rb | 0 3 files changed, 11 insertions(+) create mode 100644 spec/automobile_spec.rb create mode 100644 spec/motorcycle_spec.rb create mode 100644 spec/vehicle_spec.rb diff --git a/spec/automobile_spec.rb b/spec/automobile_spec.rb new file mode 100644 index 0000000..e69de29 diff --git a/spec/motorcycle_spec.rb b/spec/motorcycle_spec.rb new file mode 100644 index 0000000..f4adbc7 --- /dev/null +++ b/spec/motorcycle_spec.rb @@ -0,0 +1,11 @@ +require 'rspec' +require './models/eagle' + +describe Motorcycle do + + let(:cycle) { Motorcycle.new } + + it "increments the vehicle count when a new Motorcycle is created " do + Vehicle.count.should eq(1) + end +end diff --git a/spec/vehicle_spec.rb b/spec/vehicle_spec.rb new file mode 100644 index 0000000..e69de29 From 4b8b0bb2d256a025e7fc968460d26d341ff51c4e Mon Sep 17 00:00:00 2001 From: drammopo Date: Fri, 7 Feb 2014 22:43:56 +0200 Subject: [PATCH 15/19] Cleaned up registration process and add test case ideas. --- models/eagle.rb | 31 ++++++++++++------------------- spec/automobile_spec.rb | 9 +++++++++ spec/motorcycle_spec.rb | 18 ++++++++++++++---- spec/scenarios.txt | 30 ++++++++++++++++++++++++++++++ spec/vehicle_spec.rb | 6 ++++++ 5 files changed, 71 insertions(+), 23 deletions(-) create mode 100644 spec/scenarios.txt diff --git a/models/eagle.rb b/models/eagle.rb index 16b0b5a..9bb5337 100644 --- a/models/eagle.rb +++ b/models/eagle.rb @@ -19,10 +19,6 @@ def self.search(args) end - def self.register(automobile) - @@vehicles << automobile - end - def self.wheels 2 end @@ -30,7 +26,7 @@ def self.wheels class Automobile < Vehicle - @@automobiles = [] + @@auto = [] attr_accessor :color, :make, :model, :year def self.wheels 4 @@ -41,10 +37,15 @@ def initialize(args) @make = args[:make] @model = args[:model] @year = args[:year] - Vehicle.register(self) super() end + def self.build(args) + auto = Automobile.build(args) + @@vehicles << auto + auto + end + def update(new_args) @color = new_args[:color] if new_args[:color] @make = new_args[:make] if new_args[:make] @@ -52,22 +53,14 @@ def update(new_args) @year = new_args[:year] if new_args[:year] end - def self.build_car(args) - auto = Automobile.new(args) - @@vehicles << auto - auto + def all_autos + @@auto end + def self.count + @@auto.count + end end class Motorcycle < Vehicle end - -auto = Automobile.new(model: 'Mustang', color: 'Red', make: 'Ford', year: 2007) -Automobile.new model: :mazda, color: :red, make: 'Civic', year: 2012 -Automobile.new model: :toyota, color: :red, make: 'Corolla', year: 2010 -Automobile.new model: :honda, color: :blue, make: 'Civic', year: 2013 -Automobile.new model: :mazda, color: :blue, make: 'Accord', year: 2003 - -puts Vehicle.count -puts Vehicle.search(color: "blue", model: "honda").inspect diff --git a/spec/automobile_spec.rb b/spec/automobile_spec.rb index e69de29..cc88340 100644 --- a/spec/automobile_spec.rb +++ b/spec/automobile_spec.rb @@ -0,0 +1,9 @@ +require 'rspec' +require '../models/eagle' + +describe Automobile do + it "records the auto when I build it" do + auto = Automobile.build + expect(Automobile.all_autos).to include?(auto) + end +end diff --git a/spec/motorcycle_spec.rb b/spec/motorcycle_spec.rb index f4adbc7..4174b8d 100644 --- a/spec/motorcycle_spec.rb +++ b/spec/motorcycle_spec.rb @@ -1,11 +1,21 @@ require 'rspec' -require './models/eagle' +require '../models/eagle' describe Motorcycle do + before :each do + @bike = Motorcycle.new + end - let(:cycle) { Motorcycle.new } + describe '.wheels' do + it 'should return two' do + wheels = Motorcycle.wheels + wheels.should == 2 + end + end - it "increments the vehicle count when a new Motorcycle is created " do - Vehicle.count.should eq(1) + describe "#new" do + it "takes no parameters and returns a Motorcycle object" do + @bike.should be_an_instance_of Motorcycle + end end end diff --git a/spec/scenarios.txt b/spec/scenarios.txt new file mode 100644 index 0000000..1b0b178 --- /dev/null +++ b/spec/scenarios.txt @@ -0,0 +1,30 @@ +#Vehicle scenarios +Vehicle.wheels +8.times { Vehicle.new } and Vehicle.count == 8 +Vehicle.search(color: "blue", model: "honda").inspect + + +#Automobilce scenarios +Automobile.is_a?(Automobile) +Automobile.wheels == 4 +auto = Automobile.new(model: 'Mustang', color: 'Red', make: 'Ford', year: 2007) +auto.color == 'Red' +auto.update(color: 'Blue', year: 2012) +auto.color == 'Blue' +auto.make = 'Ford' +auto.year == 2012 + +Automobile.new model: :mazda, color: :red, make: 'Civic', year: 2012 +Automobile.new model: :toyota, color: :red, make: 'Corolla', year: 2010 +Automobile.new model: :honda, color: :blue, make: 'Civic', year: 2013 +Automobile.new model: :mazda, color: :blue, make: 'Accord', year: 2003 +Vehicle.count == 5 #=> after all the above tests have run + +#Motorcycle +Motorcycle.wheels +4.times { Motorcycle.new } and Vehicle.count == 4 +Motorcycle.is_a?(Motorcycle) + + + + diff --git a/spec/vehicle_spec.rb b/spec/vehicle_spec.rb index e69de29..bcefade 100644 --- a/spec/vehicle_spec.rb +++ b/spec/vehicle_spec.rb @@ -0,0 +1,6 @@ +require 'rspec' +require '../models/eagle' + +describe Vehicle do + let(:vehicle) { Vehicle.new } +end From e47650943c659a8a71c14030eb60630529abbbc1 Mon Sep 17 00:00:00 2001 From: drammopo Date: Sat, 8 Feb 2014 00:14:31 +0200 Subject: [PATCH 16/19] Feature: Added .build . First Automobile test case now passes. --- models/eagle.rb | 5 +++-- spec/automobile_spec.rb | 4 ++-- spec/scenarios.txt | 4 ++++ 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/models/eagle.rb b/models/eagle.rb index 9bb5337..d599b23 100644 --- a/models/eagle.rb +++ b/models/eagle.rb @@ -41,8 +41,9 @@ def initialize(args) end def self.build(args) - auto = Automobile.build(args) + auto = Automobile.new(args) @@vehicles << auto + @@auto << auto auto end @@ -53,7 +54,7 @@ def update(new_args) @year = new_args[:year] if new_args[:year] end - def all_autos + def self.all_autos @@auto end diff --git a/spec/automobile_spec.rb b/spec/automobile_spec.rb index cc88340..ebc41f5 100644 --- a/spec/automobile_spec.rb +++ b/spec/automobile_spec.rb @@ -3,7 +3,7 @@ describe Automobile do it "records the auto when I build it" do - auto = Automobile.build - expect(Automobile.all_autos).to include?(auto) + auto = Automobile.build(model: 'Mustang', color: 'Red', make: 'Ford', year: 2007) + expect(Automobile.all_autos).to include (auto) end end diff --git a/spec/scenarios.txt b/spec/scenarios.txt index 1b0b178..aabe25b 100644 --- a/spec/scenarios.txt +++ b/spec/scenarios.txt @@ -20,6 +20,10 @@ Automobile.new model: :honda, color: :blue, make: 'Civic', year: 2013 Automobile.new model: :mazda, color: :blue, make: 'Accord', year: 2003 Vehicle.count == 5 #=> after all the above tests have run +Automobile.count #=> 0 +Automobile.build(model: 'Mustang', color: 'Red', make: 'Ford', year: 2007) +Automobile.count #=> 1 + #Motorcycle Motorcycle.wheels 4.times { Motorcycle.new } and Vehicle.count == 4 From 4e247f27b34d9bac2c5ad61d4b2ad676ccd7baa4 Mon Sep 17 00:00:00 2001 From: drammopo Date: Sat, 8 Feb 2014 20:09:02 +0200 Subject: [PATCH 17/19] Feature: Added Automobile and Motorcycle test specs. --- spec/automobile_spec.rb | 106 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 103 insertions(+), 3 deletions(-) diff --git a/spec/automobile_spec.rb b/spec/automobile_spec.rb index ebc41f5..dc25b6c 100644 --- a/spec/automobile_spec.rb +++ b/spec/automobile_spec.rb @@ -2,8 +2,108 @@ require '../models/eagle' describe Automobile do - it "records the auto when I build it" do - auto = Automobile.build(model: 'Mustang', color: 'Red', make: 'Ford', year: 2007) - expect(Automobile.all_autos).to include (auto) + describe '.wheels' do + it 'should return four' do + wheels = Automobile.wheels + wheels.should == 4 end + end + + + before :each do + @auto = Automobile.new(model: 'Mustang', color: 'Red', make: 'Ford', year: 2007) + end + + describe "#new" do + it "takes four parameters and returns a Automobile object" do + @auto.should be_an_instance_of Automobile + end + end + + describe "#model" do + it 'should have model attribute' do + @auto.should respond_to(:model) + end + + it "returns the correct model" do + @auto.model.should eql 'Mustang' + end + end + + describe "#color" do + it 'should have color attribute' do + @auto.should respond_to(:color) + end + + it "returns the correct color" do + @auto.color.should eql 'Red' + end + end + + describe "#make" do + it 'should have make attribute' do + @auto.should respond_to(:make) + end + + it "returns the correct make" do + @auto.make.should eql 'Ford' + end + end + + describe "#year" do + it 'should have year attribute' do + @auto.should respond_to(:year) + end + + it "returns the correct year" do + @auto.year.should eql 2007 + end + end + + describe "#update" do + context "with no parameters" do + it "should keep the Automobile unchanged if no parameters are given " do + @auto.update({}) + @auto.model.should eql 'Mustang' + @auto.color.should eql 'Red' + @auto.make.should eql 'Ford' + @auto.year.should eql 2007 + end + end + + it 'should update the model attribute' do + @auto.update(model: 'Calibre') + @auto.model.should eql 'Calibre' + end + it 'should update the color attribute' do + @auto.update(color: 'Blue') + @auto.color.should eql 'Blue' + end + it 'should update the make attribute' do + @auto.update(make: 'Dodge') + @auto.make.should eql 'Dodge' + end + it 'should update the year attribute' do + @auto.update(year: 2009) + @auto.year.should eql 2009 + end + + context "with all parameters" do + it 'should takes four parameters and change the entire Automobile object' do + @auto.update(model: 'Explorer', color: 'Silver', make: 'Ford', year: 1998) + @auto.model.should eql 'Explorer' + @auto.color.should eql 'Silver' + @auto.make.should eql 'Ford' + @auto.year.should eql 1998 + end + end + end + + it "records the auto when I build it" do + auto = Automobile.build(model: 'Mustang', color: 'Red', make: 'Ford', year: 2007) + expect(Automobile.all_autos).to include (auto) + end + end + +# command rspec automobile_spec.rb --format nested From c7e1d6d9ea5f436d8b533f9372c777c24d7e212d Mon Sep 17 00:00:00 2001 From: drammopo Date: Tue, 11 Feb 2014 20:54:30 +0200 Subject: [PATCH 18/19] Switched to RSpec's let(:sym) {} syntax and added more tests. --- spec/automobile_spec.rb | 82 ++++++++++++++++++++++------------------- spec/motorcycle_spec.rb | 13 ++++--- spec/vehicle_spec.rb | 28 ++++++++++++++ 3 files changed, 79 insertions(+), 44 deletions(-) diff --git a/spec/automobile_spec.rb b/spec/automobile_spec.rb index dc25b6c..8b745e2 100644 --- a/spec/automobile_spec.rb +++ b/spec/automobile_spec.rb @@ -4,106 +4,112 @@ describe Automobile do describe '.wheels' do it 'should return four' do - wheels = Automobile.wheels - wheels.should == 4 + Automobile.wheels.should eq(4) end end - - before :each do - @auto = Automobile.new(model: 'Mustang', color: 'Red', make: 'Ford', year: 2007) - end + let (:auto) { Automobile.new(model: 'Mustang', color: 'Red', make: 'Ford', year: 2007)} describe "#new" do it "takes four parameters and returns a Automobile object" do - @auto.should be_an_instance_of Automobile + auto.should be_an_instance_of Automobile + end + + it 'should inherit from vehicle' do + auto.is_a?(Vehicle).should be_true end end describe "#model" do it 'should have model attribute' do - @auto.should respond_to(:model) + auto.should respond_to(:model) end it "returns the correct model" do - @auto.model.should eql 'Mustang' + auto.model.should eql 'Mustang' end end describe "#color" do it 'should have color attribute' do - @auto.should respond_to(:color) + auto.should respond_to(:color) end it "returns the correct color" do - @auto.color.should eql 'Red' + auto.color.should eql 'Red' end end describe "#make" do it 'should have make attribute' do - @auto.should respond_to(:make) + auto.should respond_to(:make) end it "returns the correct make" do - @auto.make.should eql 'Ford' + auto.make.should eql 'Ford' end end describe "#year" do it 'should have year attribute' do - @auto.should respond_to(:year) + auto.should respond_to(:year) end it "returns the correct year" do - @auto.year.should eql 2007 + auto.year.should eql 2007 end end describe "#update" do context "with no parameters" do it "should keep the Automobile unchanged if no parameters are given " do - @auto.update({}) - @auto.model.should eql 'Mustang' - @auto.color.should eql 'Red' - @auto.make.should eql 'Ford' - @auto.year.should eql 2007 + auto.update({}) + auto.model.should eql 'Mustang' + auto.color.should eql 'Red' + auto.make.should eql 'Ford' + auto.year.should eql 2007 end end it 'should update the model attribute' do - @auto.update(model: 'Calibre') - @auto.model.should eql 'Calibre' + auto.update(model: 'Calibre') + auto.model.should eql 'Calibre' end it 'should update the color attribute' do - @auto.update(color: 'Blue') - @auto.color.should eql 'Blue' + auto.update(color: 'Blue') + auto.color.should eql 'Blue' end it 'should update the make attribute' do - @auto.update(make: 'Dodge') - @auto.make.should eql 'Dodge' + auto.update(make: 'Dodge') + auto.make.should eql 'Dodge' end it 'should update the year attribute' do - @auto.update(year: 2009) - @auto.year.should eql 2009 + auto.update(year: 2009) + auto.year.should eql 2009 end context "with all parameters" do - it 'should takes four parameters and change the entire Automobile object' do - @auto.update(model: 'Explorer', color: 'Silver', make: 'Ford', year: 1998) - @auto.model.should eql 'Explorer' - @auto.color.should eql 'Silver' - @auto.make.should eql 'Ford' - @auto.year.should eql 1998 + it 'should takes four parameters and changes the entire Automobile object' do + auto.update(model: 'Explorer', color: 'Silver', make: 'Ford', year: 1998) + auto.model.should eql 'Explorer' + auto.color.should eql 'Silver' + auto.make.should eql 'Ford' + auto.year.should eql 1998 end end end - it "records the auto when I build it" do - auto = Automobile.build(model: 'Mustang', color: 'Red', make: 'Ford', year: 2007) - expect(Automobile.all_autos).to include (auto) - end + describe "#build" do + it "records the auto when I build it" do + auto = Automobile.build(model: 'Mustang', color: 'Red', make: 'Ford', year: 2007) + expect(Automobile.all_autos).to include (auto) + expect(Vehicle.all_vehicles).to include (auto) + end + it "should increment the Vehicle count" do + expect{Automobile.build model: :honda, color: :blue, make: 'Civic', year: 2013}.to change{Vehicle.count}.from(1).to(2) + end + end end # command rspec automobile_spec.rb --format nested diff --git a/spec/motorcycle_spec.rb b/spec/motorcycle_spec.rb index 4174b8d..c12aec5 100644 --- a/spec/motorcycle_spec.rb +++ b/spec/motorcycle_spec.rb @@ -2,20 +2,21 @@ require '../models/eagle' describe Motorcycle do - before :each do - @bike = Motorcycle.new - end + let (:bike) {Motorcycle.new} describe '.wheels' do it 'should return two' do - wheels = Motorcycle.wheels - wheels.should == 2 + Motorcycle.wheels.should eq(2) end end describe "#new" do it "takes no parameters and returns a Motorcycle object" do - @bike.should be_an_instance_of Motorcycle + bike.should be_an_instance_of Motorcycle + end + + it 'should inherit from vehicle' do + bike.is_a?(Vehicle).should be_true end end end diff --git a/spec/vehicle_spec.rb b/spec/vehicle_spec.rb index bcefade..d8955a1 100644 --- a/spec/vehicle_spec.rb +++ b/spec/vehicle_spec.rb @@ -3,4 +3,32 @@ describe Vehicle do let(:vehicle) { Vehicle.new } + + describe '.wheels' do + it 'should return two' do + Vehicle.wheels.should eq(2) + end + end + + describe "#new" do + it "takes no parameters and returns a Vehicle object" do + vehicle.should be_an_instance_of Vehicle + end + end + + describe "#search" do + it "should find an Automobile when it is searched for" do + Automobile.build model: :mazda, color: :red, make: 'Civic', year: 2012 + Automobile.build model: :toyota, color: :red, make: 'Corolla', year: 2010 + auto = Automobile.build model: :honda, color: :blue, make: 'Civic', year: 2013 + Automobile.build model: :mazda, color: :blue, make: 'Accord', year: 2003 + Vehicle.search(color: "blue", model: "honda").first.should eq(auto) + end + end + + describe "#count" do + it "should not increment the Vehicle count no matter how many vehicles are created but not built" do + expect{8.times { Vehicle.new }}.to_not change{Vehicle.count}.from(0).to(0) + end + end end From 0ef2e55341e75b64c61913f771e6255397605782 Mon Sep 17 00:00:00 2001 From: drammopo Date: Tue, 11 Feb 2014 21:26:27 +0200 Subject: [PATCH 19/19] Latest Vehicle test specfications including #search test --- models/eagle.rb | 3 +++ spec/automobile_spec.rb | 2 +- spec/vehicle_spec.rb | 1 + 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/models/eagle.rb b/models/eagle.rb index d599b23..cc74b06 100644 --- a/models/eagle.rb +++ b/models/eagle.rb @@ -16,7 +16,10 @@ def self.search(args) results.select!{|vehicle| vehicle.send(key) == value.to_sym} end results + end + def self.all_vehicles + @@vehicles end def self.wheels diff --git a/spec/automobile_spec.rb b/spec/automobile_spec.rb index 8b745e2..9b286b5 100644 --- a/spec/automobile_spec.rb +++ b/spec/automobile_spec.rb @@ -112,4 +112,4 @@ end end -# command rspec automobile_spec.rb --format nested + diff --git a/spec/vehicle_spec.rb b/spec/vehicle_spec.rb index d8955a1..e8e140d 100644 --- a/spec/vehicle_spec.rb +++ b/spec/vehicle_spec.rb @@ -2,6 +2,7 @@ require '../models/eagle' describe Vehicle do + let(:vehicle) { Vehicle.new } describe '.wheels' do