How to write a controller spec for a Rails 3.1 Engine
This week I've been building a Rails gem which mounts as an engine.
When I started to spec out the controllers I kept hitting ActionController::RoutingErrors telling me that no route matches the controller and action I was testing.
I knew this had to be a problem within my specs or with Rspec itself because routes worked fine in the browser.
On poking around the Rails ActionDispatch::Routing code I discovered the :use_route option.
Applying this to the controller specs was the fix I was looking for!
Here's a quick demo:
# in my_gem/spec/controllers/my_gem/my_controller.rb
require "spec_helper"
# Where MyGem is the namespace I've isolated my Gem under
describe MyGem::MyController do
describe "GET /index" do
# get the index action
def do_get
# :my_gem should be the name of your engine
get :index, :use_route => :my_gem
end
it "should not raise an error when I try a simple GET request!" do
lambda { do_get }.should_not raise_exception
end
end
end
If you have any interesting Rails 3.1 engines that you're working on, leave a comment with a link below :)
Filed under: Ruby on Rails, gem, Rspec, BDD, routing
0 comments