Skip to content

Instantly share code, notes, and snippets.

@bgswan
bgswan / mock_versus_spy.rb
Created December 31, 2010 16:06
Example of a mock test and the same test with a spy
# Checking an interaction with a mock
it "should notify bidders in a mocky way" do
bidder = mock( :bidder )
bidder.should_receive( :bid_changed ).with( '10.00' )
auctioneer = Auctioneer.new
auctioneer.add_bidder( bidder )
auctioneer.accept_bid( '10.00' )
end
@bgswan
bgswan / mock_problems.rb
Created December 31, 2010 15:52
Mock style test versus non mock test
# common use of mocks in a Rails app
it "updates the requested book" do
mock_book = mock(:book)
Book.should_receive(:find).with("37").and_return(mock_book)
mock_book.should_receive(:update_attributes).with({:these => 'params'})
put :update, :id => "37", :book => {:these => 'params'}
end
# without mocks a more readable, less brittle test
it "updates the requested book" do