teh hayley

[Originally written on tumblr]

Fun error this morning:

undefined method `links_with' for #<Mechanize::File:0x007fe40c9ff158> (NoMethodError)
      

So for some background, I haven't ever looked into a proper way to fake web requests in testing, so instead I'll do something like this in my spec files:

agent = Mechanize.new
      page = agent.get "file:///#{Dir.pwd}/spec/fixtures/nws/20121024/index_hls3.shtml"
      statements = find_offices_with_local_statements(page)
      

Note: If you're curious as to what I'm testing, it's a National Hurricane Center page and this is for Cyclocane.

The thing that got me was I did a copy/paste from some code that was also calling links_with on a local file and that code had absolutely no problems.

Through testing, I eventually discovered that apparently mechanize does not like .shtml extensions on local files. Grabbing the file directly from the NHC website worked fine, but if I loaded it off of the filesystem, for some reason, it wouldn't parse it properly.

the fix

I'm not sure if there's a better way or if this is actually something that should be a bug report, but the fix is pretty simple.

rename the .shtml file to .html

I've tried this with a couple already and it seems to take care of the parsing weirdness.


teh hayley