开发者

Mock external API object with rails3

开发者 https://www.devze.com 2023-03-12 10:40 出处:网络
I want to mock/stub: @the_bill = GovKit::OpenCongress::Bill.find_by_idents(\"112-s368\").first for use in my tests.

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 purpos开发者_高级运维e 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消