Rocking the Mocking
Posted by Adam Wiggins on May 02, 2008 at 02:12 PM
How do you write a spec for this method without touching the filesystem or the user's environment?
def authkey File.read("#{ENV['HOME']}/.ssh/id_rsa.pub") end
Just repeat this mantra to yourself: It's Ruby. Everything Is An Object Or A Method. Objects And Methods Are Always Mutable.
Got your answer yet? Here's mine:
it "reads the ssh rsa key from the user's home directory" do ENV.should_receive(:[]).with('HOME').and_return('/home/joe') File.should_receive(:read).with('/home/joe/.ssh/id_rsa.pub').and_return('the key') @client.authkey.should == 'the key' end
Comments
There is 1 comment on this post. Post yours →
I love Mocha... and lately I've been trying to follow a one expect/one test pattern. Just to see if my specs are more thorough or not.
require 'mocha'
describe User do describe 'ssh key' do it 'is found in the users home directory' do File.stubs(:read).returns('the key') ENV.expects(:[]).with('HOME').returns('/home/joe') @client.authkey.should == 'the key' end
end end
I have no idea if that will run or not... but IT SHOULDA hehe!
Post a comment
Required fields in bold.