From a93654530b62907a20d21a022139d3c82af012a6 Mon Sep 17 00:00:00 2001 From: Michael Vezzani Date: Tue, 4 Mar 2014 14:23:08 -0500 Subject: [PATCH 1/4] Check that an admiral has a battleship and confirm that starting ammunition is at 10. --- minitest/navy.rb | 4 +++- minitest/navy_test.rb | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/minitest/navy.rb b/minitest/navy.rb index c672e0a..0ef0115 100644 --- a/minitest/navy.rb +++ b/minitest/navy.rb @@ -1,4 +1,6 @@ class Admiral + attr_reader :battleship + def initialize(battleship) @battleship = battleship end @@ -12,7 +14,7 @@ def fire_upon_target class Battleship attr_reader :ammunition def initialize - @ammunition = 100 + @ammunition = 10 end def fire! diff --git a/minitest/navy_test.rb b/minitest/navy_test.rb index de35ec0..c52464f 100644 --- a/minitest/navy_test.rb +++ b/minitest/navy_test.rb @@ -16,13 +16,28 @@ def test_can_tell_the_battleship_to_fire end end -class TestBattleship< MiniTest::Unit::TestCase +class TestAdmiral < MiniTest::Unit::TestCase + + def test_has_a_battleship + @battleship = Battleship.new + @admiral = Admiral.new(@battleship) + assert_equal (@admiral.battleship), @battleship + end + +end + +class TestBattleship < MiniTest::Unit::TestCase def test_will_decrease_ammunition_when_firing battleship = Battleship.new starting_ammunition = battleship.ammunition battleship.fire! assert_equal (starting_ammunition - 1), battleship.ammunition end + + def test_starting_ammo_at_10 + battleship = Battleship.new + assert_equal (10), battleship.ammunition + end end describe Battleship do From bdd713bc44574063ddab251febfeb6738393a44c Mon Sep 17 00:00:00 2001 From: Michael Vezzani Date: Tue, 4 Mar 2014 14:30:51 -0500 Subject: [PATCH 2/4] MiniTest for requesting more ammunition. RSpec for requesting more ammunition. --- minitest/navy.rb | 4 ++++ minitest/navy_test.rb | 8 ++++++++ rspec/navy.rb | 4 ++++ rspec/navy_spec.rb | 6 ++++++ 4 files changed, 22 insertions(+) diff --git a/minitest/navy.rb b/minitest/navy.rb index 0ef0115..bedc61e 100644 --- a/minitest/navy.rb +++ b/minitest/navy.rb @@ -20,4 +20,8 @@ def initialize def fire! @ammunition = @ammunition - 1 end + + def get_ammo! + @ammunition = @ammunition + 10 + end end diff --git a/minitest/navy_test.rb b/minitest/navy_test.rb index c52464f..c962b07 100644 --- a/minitest/navy_test.rb +++ b/minitest/navy_test.rb @@ -47,4 +47,12 @@ def test_starting_ammo_at_10 battleship.fire! battleship.ammunition.must_equal (starting_ammunition -1) end + + it "should be able to request ammo" do + battleship = Battleship.new + starting_ammunition = battleship.ammunition + battleship.get_ammo! + battleship.ammunition.must_equal (starting_ammunition + 10) + end + end diff --git a/rspec/navy.rb b/rspec/navy.rb index 09c11cc..4bc9e88 100644 --- a/rspec/navy.rb +++ b/rspec/navy.rb @@ -18,4 +18,8 @@ def initialize def fire! @ammunition = @ammunition - 1 end + + def get_ammo! + @ammunition = @ammunition + 10 + end end diff --git a/rspec/navy_spec.rb b/rspec/navy_spec.rb index da8c3ee..cb0fa1f 100644 --- a/rspec/navy_spec.rb +++ b/rspec/navy_spec.rb @@ -17,4 +17,10 @@ subject.fire! }.to change(subject, :ammunition).by(-1) end + + it "receives ammunition upon request" do + expect { + subject.get_ammo! + }.to change(subject, :ammunition).by(10) + end end From d9f6cd02980591e334e33013dfcad4a3d6646284 Mon Sep 17 00:00:00 2001 From: Michael Vezzani Date: Thu, 13 Mar 2014 16:31:46 -0400 Subject: [PATCH 3/4] Need to figure out how to test for a response of 'hit' or 'miss' --- rspec/navy.rb | 6 ++++++ rspec/navy_spec.rb | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/rspec/navy.rb b/rspec/navy.rb index 4bc9e88..11b303d 100644 --- a/rspec/navy.rb +++ b/rspec/navy.rb @@ -17,9 +17,15 @@ def initialize def fire! @ammunition = @ammunition - 1 + hit_or_miss end def get_ammo! @ammunition = @ammunition + 10 end + + def hit_or_miss + ["hit", "miss"].sample + end + end diff --git a/rspec/navy_spec.rb b/rspec/navy_spec.rb index cb0fa1f..a6e961c 100644 --- a/rspec/navy_spec.rb +++ b/rspec/navy_spec.rb @@ -23,4 +23,10 @@ subject.get_ammo! }.to change(subject, :ammunition).by(10) end + + it "receives a 'hit' or 'miss' after calling fire!" do + subject.fire!.should eq("hit") || subject.fire!.should eq("miss") + # The following syntax does not work, and I'm not sure why. + # expect { subject.fire! }.to eq("hit") + end end From 9fabfbf961ffee8c4528a889e1d43e828a3a6fc7 Mon Sep 17 00:00:00 2001 From: Michael Vezzani Date: Thu, 13 Mar 2014 16:34:48 -0400 Subject: [PATCH 4/4] What is wrong with my || syntax? --- rspec/navy_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rspec/navy_spec.rb b/rspec/navy_spec.rb index a6e961c..75f2916 100644 --- a/rspec/navy_spec.rb +++ b/rspec/navy_spec.rb @@ -25,7 +25,7 @@ end it "receives a 'hit' or 'miss' after calling fire!" do - subject.fire!.should eq("hit") || subject.fire!.should eq("miss") + subject.fire!.should eq("hit" || "miss") # The following syntax does not work, and I'm not sure why. # expect { subject.fire! }.to eq("hit") end