Photo-1 Photo-2 Photo-3 Photo-4 Photo-5 Photo-6

@brentmc79

@brentmc79

Full-time web developer. Part-time smart ass.

I'm Brent Collier.

After a year and a half as an engineer on Twitter's Trust & Safety team, I'm looking for my next gig. Contact me if you know of something interesting.

#

Engine Controller Spec: No Route Matches... WTF?

Posted on 10/09/2015

I was working on a mountable Rails engine, and just as I began writing some controller specs I hit a small road block...

1) MyEngine::PostsController POST create creates a post
   Failure/Error: post :create
   ActionController::UrlGenerationError:
     No route matches {:action=>"create", :controller=>"my_engine/posts"}
   # ./spec/controllers/posts_controller_spec.rb:29:in `block (3 levels) in <top (required)>'
   # ./spec/spec_helper.rb:40:in `block (3 levels) in <top (required)>'
   # ./spec/spec_helper.rb:39:in `block (2 levels) in <top (required)>'

My controller spec looked something like this:

describe MyEngine::PostsController, type: :controller do
  context 'POST create' do
    it 'creates a post' do
      post :create
      expect(response.status).to eq(200)
    end
  end
end

After much cursing and googling, I discovered the solution was to add the following line to your controller spec:

routes { MyEngine::Engine.routes }

Once that was in place, everything worked as expected.