I am trying to get a Bill object to perform tests on. This is a US congress bill, which I have in xml via rsync on a data directory. My code takes in the name of the bill, say “h1.xml”, parses the xml and then gets the full text of the bill from http://www.govtrack.us. So, in my main app, to create a bill
- Get a bill name (e.g. h1) (via globbing in the method def self.update_from_directory)
def self.update_from_directory
Dir.glob("#{Rails.root}/data/bills/small_set/*.xml").each do |bill_path|
bill_name = bill_path.match(/.*\/(.*).xml$/)[1]
b = Bill.find_or_create_by(:govtrack_name => bill_name)
b.update_bill
b.save!
end
end
- Update the bill
def update_bill
file_data = File.new("#{Rails.root}/data/bills/#{self.govtrack_name}.xml", 'r')
bill = Feedzirra::Parser::GovTrackBill.parse(file_data)
if bill && (self.introduced_date.nil? || (bill.introduced_date.to_date > self.introduced_date))
self.congress = bill.congress
self.bill_type = bill.bill_type
self.bill_number = bill.bill_number
... and so on . . . until:
get_bill_text
- Update the bill record with the bill text
def get_bill_text
bill_object = HTTParty.get("#{GOVTRACK_URL}data/us/bills.text/#{self.congress.to_s}/#{self.bill_type}/#{self.bill_type + self.bill_number.to_s}.html")
self.bill_html = bill_object.response.body
self.text_updated_on = Date.today
Rails.logger.info "Updated Bill Text for #{self.ident}"
end
My goal is very simple, I want to mock a whole bill for a test:
def setup
@my_test_bill = Bill.new(:govtrack_id => "h1")
@my_test_bill.update_bill
end
I am trying to get webmock and vcr working, but all the examples I can find, provide a way to mock a specific call and I don’t want to have to retype a whole new update_bill method.
Any thoughts greatly appreciated.
Tim
Consider changing your update_bill method to:
Then change your setup method to:
That may not be exactly the solution, but this kind of approach–passing the receiver back to a block given to the method, allows you to modify the exact behavior of a given method at runtime.