Created
March 4, 2012 04:25
-
-
Save yellow5/1970655 to your computer and use it in GitHub Desktop.
Ruby spec stubbing a block using mocha
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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