Test Driving Module Methods—Part Two

Ever write an entire blog post, spend significant time on it, correct it, and eventually grow to love it, only to discover two months later you did things the hard way?

That’s what I’ve just discovered about my post Test Driving Module Methods. Turns out I’d been missing an important feature of RSpec, the and_yield command. Whereas previously I had written static methods and monkey patched the SFTP, I simply could have written:

1 @mock_session.should_receive(:close_handle)
2    Net::SFTP.start.should_receive(:start).with("test_server", "user", "password").and_yield(@mock_session)

That lets me test that the session I’m passing in is getting the right parameters like any normal code, and is far cleaner. Frankly I should have added the feature to RSpec for my original solution—if it didn’t already exist. Too bad about my lovely blog post—I guess I’ll stick to push-ups.

Eric Smith, Software Crafter

Eric Smith is a Principal Crafter at 8th Light Chicago.

Interested in 8th Light's services? Let's talk.

Contact Us