Skip to content

Instantly share code, notes, and snippets.

@bvandgrift
Created September 2, 2011 01:02
Show Gist options
  • Save bvandgrift/1187706 to your computer and use it in GitHub Desktop.
Save bvandgrift/1187706 to your computer and use it in GitHub Desktop.
looking for a better test pattern for code passed in as blocks.
# any help with the following would be awesome (@bvandgrift)
# here's the problem code (ish)
# testing the stuff in the 'chunkify' block sucks
def make_breakfast
# ...
dough_for_biscuits = DoughBro.new
dough_for_biscuits.chunkify do |chunk|
if chunk.any?
the_good_stuff = roll_dem_biscuits(chunky)
# ...
end
end
# ...
end
# the test, which seems clunky and inelegant
# using mocha
let(:reader) { TheProblemClass }
it "sends the good stuff down the road" do
chunks = mock do
stubs(:any?).returns(true, false)
# expects biscuit stuff
end
class BigFaker
def chunkify
proto_biscuits = grab_some_dough
yield proto_biscuits
end
def grab_some_dough; []; end
end
BigFaker.any_instance.stubs(:make_biscuits).returns(chunks)
DoughBro.expects(:new).returns(FakeReader.new)
reader.make_breakfast
end
# so this all works, but seems really clumsy. is there a right way to
# do this?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment