Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec Controller Test not generating right url

I am attempting to create a RSpec controller test for a namespaced controller, but rspec doesn't seem able to detect the nesting and generate the proper path for the post :create action.

This is my spec code:

# 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

For this code, the get :index works just fine, but when post :create is called, the following error is generated: undefined method 'crm_report_adjustment_url'

Why would RSpec be smart enough to figure things out with get :index, but not with post :create? How do I get RSpec to properly load the right route, which is admin_crm_report_adjustments_url?

Thanks in advance.

like image 938
jstrong Avatar asked Nov 13 '22 04:11

jstrong


1 Answers

Try posting to the url instead:

post admin_crm_report_adjustments_url

# or 

post "/admin/crm/report_adjustments"
like image 117
psparrow Avatar answered Nov 15 '22 05:11

psparrow