Skip to content

Instantly share code, notes, and snippets.

@mkocher
Created September 5, 2012 04:30
Show Gist options
  • Save mkocher/3630468 to your computer and use it in GitHub Desktop.
Save mkocher/3630468 to your computer and use it in GitHub Desktop.
Fakes in Ruby
class FileTransferJob
def initialize(ftp_service = FtpService.new)
@ftp_service = ftp_service
end
def do_the_needful
@ftp_service.put("/tmp/the_file_to_send", "/tmp/the_destination")
end
end
describe FileTransferJob do
class FakeFtpService
attr_accessor :transfers
def put(file, destination)
self.transfers ||= []
transfers << {:file => file, :destination => destination}
end
end
let(:ftp_service) { FakeFtpService.new }
let(:job) { FileTransferJob.new(ftp_service) }
it "sends the file" do
job.do_the_needful
ftp_service.transfers.first[:file].should == "/tmp/the_file_to_send"
ftp_service.transfers.first[:destination].should == "/tmp/the_destination"
end
end
Matthews-MacBook-Air:lobot (ubuntu) $ rspec ~/Desktop/fake.rb
FileTransferJob
sends the file
Finished in 0.00058 seconds
1 example, 0 failures
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment