diff --git a/minitest/fate.rb b/minitest/fate.rb new file mode 100644 index 0000000..93ac265 --- /dev/null +++ b/minitest/fate.rb @@ -0,0 +1,2 @@ +class Fate +end \ No newline at end of file diff --git a/minitest/navy.rb b/minitest/navy.rb index c672e0a..fb0e7cb 100644 --- a/minitest/navy.rb +++ b/minitest/navy.rb @@ -1,4 +1,5 @@ class Admiral + attr_reader :battleship def initialize(battleship) @battleship = battleship end @@ -7,15 +8,39 @@ def fire_upon_target @battleship.fire! end + #def has_a_ship? + #@battleship.max_hits > 0 + #end +end + +module Fate + class Fate + def hit_or_miss + result = rand(5)+1 + result.even? + end + end end class Battleship - attr_reader :ammunition + include Fate + + INITIAL_AMMUNITION = 10 + MAX_HITS = 10 + + attr_accessor :ammunition, :max_hits + def initialize - @ammunition = 100 + @ammunition = INITIAL_AMMUNITION + @max_hits = MAX_HITS end def fire! + @max_hits = @max_hits - 1 if Fate.new.hit_or_miss @ammunition = @ammunition - 1 end + + def reload!(extra_ammunition) + @ammunition += extra_ammunition + end end diff --git a/minitest/navy_test.rb b/minitest/navy_test.rb index de35ec0..06fb229 100644 --- a/minitest/navy_test.rb +++ b/minitest/navy_test.rb @@ -7,6 +7,8 @@ class TestAdmiral < MiniTest::Unit::TestCase def setup @battleship = MiniTest::Mock.new @admiral = Admiral.new(@battleship) + @another_battleship = Battleship.new + @another_admiral = Admiral.new(@another_battleship) end def test_can_tell_the_battleship_to_fire @@ -14,15 +16,43 @@ def test_can_tell_the_battleship_to_fire @admiral.fire_upon_target @battleship.verify end + + def test_have_a_battleship + assert_equal true, @another_admiral.battleship.max_hits > 0 + end end class TestBattleship< MiniTest::Unit::TestCase + + def setup + @mock_battleship = MiniTest::Mock.new + @battleship = Battleship.new + @starting_ammunition = @battleship.ammunition + end + def test_will_decrease_ammunition_when_firing - battleship = Battleship.new - starting_ammunition = battleship.ammunition - battleship.fire! - assert_equal (starting_ammunition - 1), battleship.ammunition + @battleship.fire! + assert_equal (@starting_ammunition - 1), @battleship.ammunition + end + + def test_can_request_more_ammunition + @mock_battleship.expect :reload!, nil + @mock_battleship.reload! + @mock_battleship.verify + end + + def test_can_receive_more_ammunition + extra_ammunition = 10 + @battleship.reload!(extra_ammunition) + assert_equal (@starting_ammunition + extra_ammunition), @battleship.ammunition + end + + def test_hit_or_miss + @mock_battleship.expect :fire!, [true, false] + @mock_battleship.fire! + @mock_battleship.verify end + end describe Battleship do