Test Driving Module Methods—Part Two

Test Driving Module Methods—Part Two

Eric Smith
Eric Smith

June 15, 2007

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:

@mock_session.should_receive(:close_handle)
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.