RSpec控制器测试不生成正确的URL

时间:2013-06-04 00:30:09

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

我正在尝试为命名空间控制器创建RSpec控制器测试,但rspec似乎无法检测到嵌套并为post :create操作生成正确的路径。

这是我的规范代码:

# for: /app/controllers/admin/crm/report_adjustments_controller.rb
require 'spec_helper'
describe Admin::Crm::ReportAdjustmentsController do
  render_views

  before(:each) do
    signin
  end

  describe "GET 'index'" do
    it "returns http success" do
      get :index
      response.should be_success
    end
  end

  describe "POST 'create'" do
    it "creates with right parameters" do
      expect {
        post :create, report_adjustment: {distributor_id: @ole_distributor.id, amount: "30.0", date: Date.today }
      }.to change(Crm::ReportAdjustment, :count).by(1)
      response.should be_success
    end
  end
end

# routes.rb
namespace :admin do
  namespace :crm do
    resources :report_adjustments
  end
end

对于此代码,get :index工作正常,但在调用post :create时,会生成以下错误:undefined method 'crm_report_adjustment_url'

为什么RSpec能够聪明地用get :index来解决问题,而不是post :create?如何让RSpec正确加载正确的路线,即admin_crm_report_adjustments_url

提前致谢。

1 个答案:

答案 0 :(得分:1)

尝试发布到网址:

post admin_crm_report_adjustments_url

# or 

post "/admin/crm/report_adjustments"