Skip to content

Instantly share code, notes, and snippets.

@bgswan
Created December 31, 2010 15:52
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save bgswan/761102 to your computer and use it in GitHub Desktop.
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
a_book = Book.create(:author => 'an author')
put :update, :id => a_book.to_param, :book => {:author => 'JR Hartley'}
assert_equal 'JR Hartley', a_book.author
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment