Ruby on rails 如何在rails中测试从远程源读取CSV的控制器操作?
我想测试一个像这样的函数Ruby on rails 如何在rails中测试从远程源读取CSV的控制器操作?,ruby-on-rails,csv,rspec-rails,Ruby On Rails,Csv,Rspec Rails,我想测试一个像这样的函数 def load_template_from_csv arr = [] images = [] CSV.new(open(params[:a_csv]), headers: true).each_with_index do |row, index| # some formating res << {row} end render json: { data: res } end 但这不起
def load_template_from_csv
arr = []
images = []
CSV.new(open(params[:a_csv]), headers: true).each_with_index do |row, index|
# some formating
res << {row}
end
render json: { data: res }
end
但这不起作用,我是否缺少一些基础知识,或者这是一个很难测试的函数 定义“不工作”。您是否获得预期的200状态,但希望注释中的项目?不,我没有获得200状态。我想了解的是我应该嘲笑什么部分,不应该嘲笑什么部分。
it 'tests load_ad_template_from_csv action' do
account_id = Account.last.id
get :load_template_from_csv, internal_ad_account_id: ad_account_id, ad_csv: 'test'
# expect(File).to receive(:open).with('test', headers: true)
# expect(CSV.stub(:new)).to receive(:new).with('test', headers: true)
# expect(CSV.stub).to receive(:read).with('test', headers: true)
# CSV.should_receive(:new).with("file", headers: true)
expect(response.status).to eq(200)
end