diff --git a/minitest/navy.rb b/minitest/navy.rb index c672e0a..bedc61e 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,10 +14,14 @@ def fire_upon_target class Battleship attr_reader :ammunition def initialize - @ammunition = 100 + @ammunition = 10 end 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 de35ec0..c962b07 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 @@ -32,4 +47,12 @@ def test_will_decrease_ammunition_when_firing 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..11b303d 100644 --- a/rspec/navy.rb +++ b/rspec/navy.rb @@ -17,5 +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 da8c3ee..75f2916 100644 --- a/rspec/navy_spec.rb +++ b/rspec/navy_spec.rb @@ -17,4 +17,16 @@ 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 + + it "receives a 'hit' or 'miss' after calling fire!" do + 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 end