【发布时间】:2011-03-07 11:02:19
【问题描述】:
我正在使用 ruby 1.9,我正在尝试做 BDD。我的第一个测试“应该在 csv 中读取”有效,但我需要模拟文件对象的第二个测试无效。
这是我的模型规格:
require 'spec_helper'
describe Person do
describe "Importing data" do
let(:person) { Person.new }
let(:data) { "title\tsurname\tfirstname\t\rtitle2\tsurname2\tfirstname2\t\r"}
let(:result) {[["title","surname","firstname"],["title2","surname2","firstname2"]] }
it "should read in the csv" do
CSV.should_receive(:read).
with("filename", :row_sep => "\r", :col_sep => "\t")
person.import("filename")
end
it "should have result" do
filename = mock(File, :exists? => true, :read => data)
person.import(filename).should eq(result)
end
end
end
这是目前为止的代码:
class Person < ActiveRecord::Base
attr_accessor :import_file
def import(filename)
CSV.read(filename, :row_sep => "\r", :col_sep => "\t")
end
end
我基本上想模拟一个文件,以便当 CSV 方法尝试从文件中读取时,它会返回我的数据变量。然后我可以测试它是否等于我的结果变量。
【问题讨论】:
-
你能发布失败规范的堆栈跟踪吗?你有没有
inspect模拟看看它是不是你认为的那样?
标签: ruby-on-rails file mocking rspec rspec2