Skip to content

Instantly share code, notes, and snippets.

@yellow5
Created March 4, 2012 04:25
Show Gist options
  • Save yellow5/1970655 to your computer and use it in GitHub Desktop.
Save yellow5/1970655 to your computer and use it in GitHub Desktop.
Ruby spec stubbing a block using mocha
class Importer
def parse_csv_file(csv_file)
File.open(csv_file) do |file|
ActiveRecordModel.transaction do
import_csv_stream(file)
end
end
end
end
describe Importer do
describe '#parse_csv_file' do
let(:csv_file) { "#{Rails.root}/tmp/expected_import.csv" }
let(:importer) { Import.new }
let(:opened_file) { mock('file') }
let(:data) { "line|value\n2|12345\n" }
let(:data_stream) { StringIO.new(data) }
def do_invoke
importer.parse_csv_file(csv_file)
end
it 'opens the received file' do
File.expects(:open).with(csv_file)
do_invoke
end
it 'performs an ActiveRecordModel.transaction' do
File.stubs(:open).yields(opened_file).returns(data_stream)
ActiveRecordModel.expects(:transaction)
do_invoke
end
it 'passes the open file to import_csv_stream' do
File.stubs(:open).yields(opened_file).returns(data_stream)
importer.expects(:import_csv_stream).with(opened_file)
do_invoke
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment