rspec test result from csv.read mocking file

前端 未结 1 850
野的像风
野的像风 2021-02-05 08:20

I\'m using ruby 1.9 and I\'m trying to do BDD. My first test \'should read in the csv\' works, but the second where I require a file object to be mocked doesn\'t.

Here i

相关标签:
1条回答
  • 2021-02-05 08:53

    You can stub File.open:

    let(:data) { "title\tsurname\tfirstname\rtitle2\tsurname2\tfirstname2\r" }
    let(:result) {[["title","surname","firstname"],["title2","surname2","firstname2"]] }
    
    it "should parse file contents and return a result" do
      expect(File).to receive(:open).with("filename","rb") { StringIO.new(data) }
      person.import("filename").should eq(result)
    end
    

    StringIO essentially wraps a string and makes it behave like an IO object (a File in this case)

    0 讨论(0)
提交回复
热议问题