Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions minitest/fate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Fate
end
29 changes: 27 additions & 2 deletions minitest/navy.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Admiral
attr_reader :battleship
def initialize(battleship)
@battleship = battleship
end
Expand All @@ -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
38 changes: 34 additions & 4 deletions minitest/navy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,52 @@ 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
@battleship.expect :fire!, nil
@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
Expand Down