我没有与rspec匹配路由来测试我的控制器中的方法。
以下是测试代码:
let(:csv_file){ fixture_file_upload('files/sample_employee_data.csv', 'text/csv') }
describe "#process_csv" do
it "should output a valid csv file" do
post '/payslips/process_csv', :csv => csv_file, :header => 1
puts response
end
end
以下是我的routes.rb文件代码:
PayCalculator::Application.routes.draw do
resources :payslips do
collection { post :process_csv }
end
root 'payslips#index'
end
以下是方法
def process_csv(uploaded_file = params[:files][:csv], headers = params[:files][:headers])
begin
rows = CSV_Manager.extract_csv(uploaded_file, headers)
rows.each do |row|
payslip = Payslip.create(
:first_name => row[0],
:last_name => row[1],
:annual_salary => row[2],
:superannuation => row[3].to_i,
:payment_start_date => row[4]
)
redirect_to root_url, notice: payslip.errors.full_messages and return unless payslip.valid?
prepare_output(row)
end
@rows = self.pay_data
csv_file = CSV_Manager.prepare_csv(@rows, ["Name", "Pay Period", "Gross Income", "Income Tax", "Net Income", "Superannuation"])
send_data csv_file, :type => 'text/csv; charset=iso-8859-1; header=present',
:disposition => "attachment;filename=Payslip #{Date.today.to_s}.csv"
rescue
redirect_to root_url, notice: "CSV not supplied or invalid format"
end
end
当我运行rspec spec/
时,我得到以下错误:
Failure/Error: post '/payslips/process_csv', :csv => csv_file, :header => 1
ActionController::UrlGeneratorError:
No route matches...
导致此错误的可能是什么错误?
答案 0 :(得分:0)
试试这个:
post :process_csv, :files => {:csv => csv_file, :header => 1}
答案 1 :(得分:0)
params[:files][:headers]
您传递:header => 1
的地方。关键是不同的。这不会导致找不到路线,只是为了纠正。根据rails约定,action没有参数
如果您打算以任何方式传递可选参数:请查看:http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_methods.html
以下是方法定义的例子:
def foo(arg1="Miles", arg2="Coltrane", arg3="Roach")
"#{arg1}, #{arg2}, #{arg3}."
end