Created
March 10, 2009 02:10
-
-
Save nalanj/76673 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Reusable mocking of ActiveResource | |
| # | |
| # I use this technique when I need to mock ActiveResource in several tests and it gets a bit too keyboard | |
| # heavy to use the default mechanism. | |
| # Shortcut for typing ActiveResource::HttpMock.respond_to | |
| def ar_mock(&block) | |
| ActiveResource::HttpMock.respond_to(&block) | |
| end | |
| module ActiveResource | |
| class HttpMock | |
| class Responder | |
| # Mocks create and get for a remote user object. | |
| def user(user_hash) | |
| post "/users.xml", request_headers, | |
| user_hash.to_xml(:root => "user"), 200, { | |
| 'Location' => "http://0.0.0.0:3000/users/#{user_hash[:id]}" | |
| } | |
| get "/users/#{id}.xml", request_headers, | |
| user_hash[:id] | |
| end | |
| end | |
| end | |
| end | |
| # Actually using it: | |
| ar_mock do |mock| | |
| mock.user({ :username => "alan", :password => "supersecret", :id => 1 }) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment