5

我正在尝试为命名空间控制器创建 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

提前致谢。

4

1 回答 1

1

尝试发布到网址:

post admin_crm_report_adjustments_url

# or 

post "/admin/crm/report_adjustments"
于 2014-12-21T16:30:07.100 回答