Seeking guidance: writing effective tests for externally sourced data

So first, thanks in no small part to Marnen’s rants, I’ve become a
complete RSpec / FactoryGirl / TDD convert. Hey, autotest coupled with
Growl is the bee knees! And I’ve even started pushing builds out to
Heroku to keep me honest.

Now I feel uneasy when I’ve written a piece of code that isn’t covered
by an effective test. That’s how it should be.

But I’m stumped on how to test the following, so I turn to the
collective wisdom of the RoR forum.

My app periodically needs to fetch XML data from external sites. To
avoid inadvertently pummeling the external servers, I’ve written some
extensions to Mechanize that observe the following rules:

  • any XML data I fetch from the external site is cached
  • I will NOT fetch if the most recent attempt was less than K hours ago
  • I will fetch the data if it’s absent or stale (i.e. older then J days)

Testing it manually, it works like a champ. But how the heck do I
automate the testing? It seems I need to fake up the Mechanize GET
method so I can simulate various failure modes, etc. Do I create a
FactoryGirl stub (or mock???). So I’m looking for suggestions and
(ideally) examples of how experienced testers would test this.

TIA.

  • ff

Fearless F. wrote in post #978383:

So first, thanks in no small part to Marnen’s rants, I’ve become a
complete RSpec / FactoryGirl / TDD convert. Hey, autotest coupled with
Growl is the bee knees! And I’ve even started pushing builds out to
Heroku to keep me honest.

Now I feel uneasy when I’ve written a piece of code that isn’t covered
by an effective test. That’s how it should be.

Excellent! Glad to have shown someone else the light.

But I’m stumped on how to test the following, so I turn to the
collective wisdom of the RoR forum.

My app periodically needs to fetch XML data from external sites. To
avoid inadvertently pummeling the external servers, I’ve written some
extensions to Mechanize that observe the following rules:

  • any XML data I fetch from the external site is cached

The external site isn’t already setting its HTTP caching headers
properly for this?

  • I will NOT fetch if the most recent attempt was less than K hours ago
  • I will fetch the data if it’s absent or stale (i.e. older then J days)

Testing it manually, it works like a champ. But how the heck do I
automate the testing? It seems I need to fake up the Mechanize GET
method so I can simulate various failure modes, etc. Do I create a
FactoryGirl stub (or mock???). So I’m looking for suggestions and
(ideally) examples of how experienced testers would test this.

Use Webmock, and perhaps VCR. There is an introductory Webmock
presentation on my GitHub account.

TIA.

  • ff

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Sent from my iPhone

Marnen Laibow-Koser wrote in post #978394:

  • any XML data I fetch from the external site is cached
    The external site isn’t already setting its HTTP caching headers
    properly for this?

That’s a good suggestion, and I’ll certainly look into heeding the HTTP
expiration time stamps. In my case, data isn’t updated on a regular
schedule, so the header may be of limited utility. But as I say, I’ll
look into it.

Use Webmock, and perhaps VCR. There is an introductory Webmock
presentation on my GitHub account.

I looked first at your “Testing Remote Services with WebMock” preso (a
tasty appetizer), and then at https://github.com/bblimke/webmock (the
main course) – that’s exactly what I need. I’ll be up late tonight
wiring it in. Thanks for the pointer!

  • ff

A minor correction:

Fearless F. wrote in post #978457:

Marnen Laibow-Koser wrote in post #978394:

The external site isn’t already setting its HTTP caching headers
properly for this?

That’s a good suggestion, and I’ll certainly look into heeding the HTTP
expiration time stamps…

I realize I should have said “Cache-Control HTTP headers” rather than
“Expires HTTP header”.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs