Skip to content

Instantly share code, notes, and snippets.

@totallymike
Created October 31, 2012 02:00
Show Gist options
  • Save totallymike/3984369 to your computer and use it in GitHub Desktop.
Save totallymike/3984369 to your computer and use it in GitHub Desktop.
How I want these doubles to work
class Foo
attr_reader :stuff
def initialize(stuff)
@stuff = stuff
end
def bar
sock = TCPSocket.new('127.0.0.1', 1337)
stuff.each do |thing|
sock.puts thing
sock.close
end
end
end
describe Foo do
it "will send out a line for each thing" do
sock = double()
sock.should_receive(:puts).at_least(:once) do |msg|
msg.should eq "woo"
end
TCPSocket.stub(:new).and_return sock
foo = Foo.new(['woo'])
foo.bar
end
it "doesn't open and close sockets all willy-nilly" do
sock = double()
sock.should_receive(:close).once
foo = Foo.new(['woo', 'yeah', 'party'])
foo.bar
end
end
=> Oh no failzor!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment