bonhoffer
bonhoffer

Reputation: 1473

Mock external API object with rails3

I want to mock/stub:

@the_bill = GovKit::OpenCongress::Bill.find_by_idents("112-s368").first

for use in my tests.

which returns the following object that i would like to fix for the purpose of my tests:

--- !ruby/object:GovKit::OpenCongress::Bill 
bill_type: s
co_sponsors: 
- !ruby/object:GovKit::OpenCongress::Person {}

id: 68340
introduced: 1297836000
most_recent_actions: 
- result: 
  created_at: "2011-02-17T07:45:50Z"
  govtrack_order: 
  amendment_id: 
  text: Read twice and referred to the Committee on Agriculture, Nutrition, and Forestry.
  date: 1297836000
  how: 
  id: 287979
  vote_type: 
  type: BillAction
  roll_call_id: 
  action_type: action
  datetime: "2011-02-16T00:00:00Z"
  where: 
  bill_id: 68340
  roll_call_number: 
- result: 
  created_at: "2011-02-17T07:45:49Z"
  govtrack_order: 
  amendment_id: 
  text: 
  date: 1297836000
  how: 
  id: 287978
  vote_type: 
  type: BillAction
  roll_call_id: 
  action_type: introduced
  datetime: "2011-02-16T00:00:00Z"
  where: 
  bill_id: 68340
  roll_call_number: 
number: 368
plain_language_summary: 
recent_blogs: []

I've tried Factory_girl (can't do it, not model based object), Fabrication (still same issues) and OpenStruct, probably possible, but had trouble converting yaml to OpenStruct and getting the mock in the right place.

Right now, i'm doing the api call in my tests, not what i want. I am thinking webmock is my solution, but I couldn't find out in the docs how to just load a simple object.

Upvotes: 2

Views: 771

Answers (1)

joeellis
joeellis

Reputation: 2785

Try VCR for mocking out an API. I had the exact same question about 6 months ago and only recently discovered this library. It does exactly what you need, will cache the objects for testing later, but can also automatically refresh them on regular intervals. So far it's hands down the best solution I've found for this.

Upvotes: 5

Related Questions