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
8 changes: 7 additions & 1 deletion minitest/navy.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Admiral
attr_reader :battleship

def initialize(battleship)
@battleship = battleship
end
Expand All @@ -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
25 changes: 24 additions & 1 deletion minitest/navy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is obviously just style, but I would ditch the parens.. and if you're testing a brand new object, you can probably do:

def test_starting_ammo_at_10
  assert_equal 10, Battleship.new.ammunition
end

end
end

describe Battleship do
Expand All @@ -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
10 changes: 10 additions & 0 deletions rspec/navy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 12 additions & 0 deletions rspec/navy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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