0

在我的控制器中测试方法时,我没有得到与 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...

这里可能有什么问题导致此错误?

4

2 回答 2

0

params[:files][:headers]你经过的地方:header => 1。键不一样。这可能不会导致找不到路线,而只是为了更正。根据 rails 约定动作没有参数

如果您要以任何方法传递可选参数:请查看:http ://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_methods.html

以下是方法定义的示例:

def foo(arg1="Miles", arg2="Coltrane", arg3="Roach")
  "#{arg1}, #{arg2}, #{arg3}."
end
于 2013-10-02T13:05:50.917 回答
0

尝试这个:

post :process_csv, :files => {:csv => csv_file, :header => 1}
于 2013-10-02T12:31:09.483 回答