Skip to content

Instantly share code, notes, and snippets.

@cheeyeo
Forked from yellow5/importer.rb
Last active August 29, 2015 14:27
Show Gist options
  • Save cheeyeo/8c63bf1d4339b8829e40 to your computer and use it in GitHub Desktop.
Save cheeyeo/8c63bf1d4339b8829e40 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