[rspec-users] Stubbing through multiple layers

DeNigris Sean sean at clipperadams.com
Fri Jan 8 22:37:13 EST 2010

Hi list,

I'm writing an example for a class that represents the "pickaxe" e-book, which I view in Preview.app:

describe PickaxeBook do
	it "should tell whether it's ready to read" do
		preview = stub('preview')
		preview.stub!(:******this is what I'm not sure about - see below*******).returns("programming_ruby_1.9.pdf")

		my_pickaxe_book = PickaxeBook.new(preview)

		my_pickaxe_book.should be_ready_to_read

class PickaxeBook
	def ready_to_read?
		#****** this is the dependency I want to stub: preview.front_window.title ********
		preview.front_window.title =~ /programming_ruby_1.9\.pdf.*/

Can you see the problem?  ready_to_read wants to query the object returned by front_window, but how do I stub that?  I should note that the Preview class is in a different library, which is why I don't just change front_window.title

I thought of a few things, like introducing a role (something like "PreviewInfoRetriever") with a method "front_window_title" that forwards to preview.front_window.title, but that seems like a lot of complexity.

To summarize: what's the best way to handle a dependency like object.method.method?


Sean DeNigris
sean at clipperadams.com

More information about the rspec-users mailing list