rspec存根CSV带有标头的Foreach:true

时间:2014-03-26 07:11:55

标签: ruby-on-rails rspec rspec-rails

我花了一整天的时间来了解如何使用标题来存储此CSV Foreach:在RSPEC中为true,然后在ROW和INDEX中使用do块。我尝试过几件事情,似乎不行。

这是我的模特

class Test

  def self.import(file_path)
    CSV.foreach(file_path, headers: true) do |row, index|
     Here the rest of the code to create/insert record
    end
  end

end

然后在rspec上,我尝试了以下几种方法:

describe "Class" do
  context "successfully insert" do
    let(:data) { "name,lastname\nHero,SuperHero}" }

    before do
      # CSV foreach Stub here
      CSV.stub(:foreach).with("file_path", headers: true).and_return(data)
    end

    it "inserts new record" do
      expect do
       Test.import("file_path")
      end.to change(Test, :count).by(1)
    end
  end
end

它不起作用,它只会返回CSV.foreach(file_path,headers:true)的数据,但它不会进入' do block'。

任何有关此事的帮助,

非常感谢

1 个答案:

答案 0 :(得分:0)

我认为您需要在这里使用 <h1>Position Trigger Messages</h1> <div class="container"> <div class="sheet_column"> <form name="flow_form"> <h2>Flow</h2> <label class="flow_row"> <input type="radio" name="flow" value="anvil" onclick="processFlow()" checked>ANVIL</label> <br> <label class="flow_row"> <input type="radio" name="flow" value="fx" onclick="processFlow()">FX</label> <br> <label class="flow_row"> <input type="radio" name="flow" value="debt" onclick="processFlow()">Debt</label> <br> <label class="flow_row"> <input type="radio" name="flow" value="prms_repos" onclick="processFlow()">PRMS Repos</label> <br> <label class="flow_row"> <input type="radio" name="flow" value="prms_fx" onclick="processFlow()">PRMS FX</label> <br> <label class="flow_row"> <input type="radio" name="flow" value="equity_racs" onclick="processFlow()">Equity options from RACS</label> <br> <label class="flow_row"> <input type="radio" name="flow" value="convertible_bonds">Convertible bonds</label> <br> <label class="flow_row"> <input type="radio" name="flow" value="firm_derivatives" onclick="processFlow()">Firm derivatives</label> <br> <label class="flow_row"> <input type="radio" name="flow" value="stocks" onclick="processFlow()">Stocks</label> </form> </div>代替and_yield

检查rspec-mocks的自述文件:https://github.com/rspec/rspec-mocks