为破坏行动编写测试但是没有路由匹配{:action =>" destroy",:controller =>"地址"}

时间:2014-08-11 18:30:08

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

我正在尝试为我的addresses_controller的销毁操作编写一个rspec测试。即使我在rake routes中清楚地看到它,我也遇到了无法找到路线的问题。

我收到以下错误:

Failure/Error: delete action, params
ActionController::UrlGenerationError:
No route matches {:action=>"destroy", :controller=>"addresses"}

以下是我的测试:

describe '#destroy' do
  let (:old_address) { FactoryGirl.create(:address, addressable: user) }

  let(:action) { :destroy }
  let(:params) {   }
  let(:referer) { '/' }


  before do
    request.env['HTTP_REFERER'] = referer
    login_user user, request
  end

  it 'redirects back' do
    delete action, params
    expect(response).to redirect_to(referer)
  end

end

佣金路线:

DELETE /addresses/:id(.:format)                   addresses#destroy

addresses_controller.rb:

def destroy
  address = Address.find_by_id(params[:id])
  if address.present? && address.destroy
    flash[:notice] = "Address deleted."
  end
  redirect_to :back
end

为什么我可以手动触发它并且它​​出现在我的佣金路线中时没有检测到我的路线?

1 个答案:

答案 0 :(得分:1)

你错过了参数。 (路线需要让它来弄清楚网址)

尝试let(:params) { id: old_address.id}