在我的控制器中测试方法时,我没有得到与 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...
这里可能有什么问题导致此错误?