Skip to content

Instantly share code, notes, and snippets.

@practicingruby
Last active December 30, 2015 01:08
Show Gist options
  • Save practicingruby/7753863 to your computer and use it in GitHub Desktop.
Save practicingruby/7753863 to your computer and use it in GitHub Desktop.
Tests for a game object in "Hunt The Wumpus". Please help improve its clarity.
require_relative "../helper"
describe "A room" do
let(:room) { Wumpus::Room.new(12) }
it "has a number" do
room.number.must_equal(12)
end
it "may contain hazards" do
# rooms start out safe
assert room.safe?
# hazards can be added
room.add(:wumpus)
room.add(:bats)
# a room with hazards isn't safe
refute room.safe?
# hazards can be detected by name
assert room.has?(:wumpus)
assert room.has?(:bats)
refute room.has?(:alf)
# hazards can be removed
room.remove(:bats)
refute room.has?(:bats)
end
describe "with neighbors" do
let(:exit_numbers) { [11, 3, 7] }
before do
exit_numbers.each { |i| room.connect(Wumpus::Room.new(i)) }
end
it "has two-way connections to neighbors" do
exit_numbers.each do |i|
# a neighbor can be looked up by room number
room.neighbor(i).number.must_equal(i)
# Room connections are bidirectional
room.neighbor(i).neighbor(room.number).must_equal(room)
end
end
it "knows the numbers of all neighboring rooms" do
room.exits.must_equal(exit_numbers)
end
it "can choose a neighbor randomly" do
exit_numbers.must_include(room.random_neighbor.number)
end
it "is not safe if its neighbors have hazards" do
room.random_neighbor.add(:wumpus)
refute room.safe?
end
it "is safe when it and its neighbors have no hazards" do
assert room.safe?
end
end
end
@PragTob
Copy link

PragTob commented Dec 2, 2013

Hi Greg,

okay short version now since my last one got lost:

Overall they look good I got a few things though:

  • The second and the third test seem too long. They also sport multiple assertions (I like one assertion/spec). To refactor this imo they should be split up into multiple test cases maybe with a common describe block. That would also let you get rid of the comments
  • Speaking of comments, I don't like the ### SOMETHING ### comments - imo in most cases they can be omitted or substituted with a describe block
  • Also the Array [2,4,8] is repeated, I think it's just some room numbers but maybe a constant could make that clearer to show that there is no special meaning to those numbers
  • the line [2,4,8].each { |i| room.connect(Wumpus::Room.new(i)) } is repeated 3 times in total. A describe with a before block would make that better, if not then a method.

Cheers,
Tobi

@locks
Copy link

locks commented Dec 3, 2013

Mixing #assert with #must_equal tests seems a bit confusing to me.
What's the reasoning for this?

P.S. I agree With @PragTob about replacing comments with describe blocks.

@practicingruby
Copy link
Author

@locks: Simply because assert thing.has?(x) is more expressive and at a higher level than thing.has?(x).must_equal(true).

@practicingruby
Copy link
Author

Thanks guys, I revised based on the feedback. The only test I didn't modify much is the hazards test... yes, it's long and has many assertions... but it's testing a bunch of basic data manipulations and queries that go hand-in-hand with each other, so I can't see how to separate them without it seeming a bit too contrived.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment