Skip to content

Instantly share code, notes, and snippets.

@jbrains
Forked from bgswan/mock_problems.rb
Created January 3, 2011 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbrains/763792 to your computer and use it in GitHub Desktop.
Save jbrains/763792 to your computer and use it in GitHub Desktop.
# 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