Stubs and spies help you maintain the order of the Four Phase Test.

August 6, 2009 Comments (View) Spy vs spy Update: this functionality has been extracted to the bourne gem . We’ve recently been making use of an alternative to the traditional mock-and-stub pattern common in Ruby: the Test Spy . What do you mean, spy? Test spies allow you to record method invocations for later verification. Basic usage goes something like this: describe PostsController do it "should show the given post on GET show" do post = stub('a post', :to_param => '1') Post.stubs(:find => post) get :show, :id => post.to_param Post.should have_received(:find).with(post.to_param) should render_template(:show) should assign_to(:post).with(post) end end Compare that with the traditional expectation-based example: describe PostsController do it "should show the given post on GET show" do post = stub('a post', :to_param => '1') Post.expects(:find).with(post.to_param).return...