Test Driving Module Methods—Part Two

Test Driving Module Methods—Part Two

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.

Eric Smith

Principal Crafter

Eric Smith is a fan of the Chicago Bears, Chicago Cubs, and Bruce Springsteen; and he’s the recent author of Game Development with Rust and WebAssembly, published by Packt. Eric is a consummate polyglot, with more than a decade of experience leading development teams and delivering software for global enterprise systems. He has also delivered native Android and iOS apps at every stage of their lifecycle.