Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec with Rails 3.1 No route matches error, except that the route exists

Does anybody have any idea why when I run my specs it says that this route does not exist, when it clearly does?

Here is the relevant code in the controller:

class JobsController < ApplicationController
  before_filter :find_job, :only => [:show, :edit]
  respond_to :html, :json
  def show
    respond_with @job
  end
  def find_job
    @job = Job.find(params[:id])
  end
end

And in routes.rb:

resources :jobs

And in the specs:

  def valid_attributes
    {}
  end

  describe "POST create" do
    context "with valid params" do
      it "redirects to the jobs path" do
        post :create, :job => valid_attributes
        response.should redirect_to job_path
      end
    end
  end

The error:

  1) JobsController when logged in as administrator POST create with valid params redirects to the jobs path
     Failure/Error: response.should redirect_to job_path
     ActionController::RoutingError:
       No route matches {:action=>"show", :controller=>"jobs"}

When I run rake routes I get:

    jobs GET    /jobs(.:format)                       {:action=>"index", :controller=>"jobs"}
         POST   /jobs(.:format)                       {:action=>"create", :controller=>"jobs"}
 new_job GET    /jobs/new(.:format)                   {:action=>"new", :controller=>"jobs"}
edit_job GET    /jobs/:id/edit(.:format)              {:action=>"edit", :controller=>"jobs"}
     job GET    /jobs/:id(.:format)                   {:action=>"show", :controller=>"jobs"}
         PUT    /jobs/:id(.:format)                   {:action=>"update", :controller=>"jobs"}
         DELETE /jobs/:id(.:format)                   {:action=>"destroy", :controller=>"jobs"}
like image 913
Travis Avatar asked Jan 18 '23 11:01

Travis


1 Answers

job_path is not a valid route without an :id parameter. This should work:

job = assigns(:job)
response.should redirect_to job_path(job)
like image 113
Thilo Avatar answered Jan 31 '23 10:01

Thilo