Assume, for the moment, that ActiveResource isn’t overrated, and that we have an entire app based around it, that consumes JSON from another Rails app. Is there a nice FactoryGirl-like way to mock our models?
Unable to sit idly, I did some further research and found a nice way to combine Webmock with FactoryGirl.
def webmock_stub model
stub_request(:get, host + model.class.element_path(model.id))
.with(query: hash_including({}))
.to_return( body: model.to_json)
end
before do
user = FactoryGirl.build_stubbed(:user)
webmock_stub(user)
end
Since User.element_path(1) returns a resource URL like "/users/1.json", this was easy to wrap into a generalized helper method.