Reputation: 18506
I'm trying to mimic something similar to http://relishapp.com/rspec/rspec-rails/v/2-2/dir/routing-specs/access-to-named-routes-in-routing-specs
I have a unit test passing:
require 'test_helper'
class RoutesTest < ActionController::TestCase
test "book name is sent to store#index' do
assert_routing 'book/mytitle', {:controller => 'book', :action => 'index', :title => 'mytitle'}
end
end
I'm trying to covert this to an RSpec test (running Rspec 2.2 under Rails3.0.3)
Here's the test:
require 'spec_helper'
include RSpec::Rails::Matchers::RoutingMatchers
include ActionDispatch::Assertions::RoutingAssertions
describe "book specific routes" do
it "should recognize title in path" do
{:get => "book/mytitle"}.should route_to(:controller => "book", :action => "index", :title => "mytitle")
end
end
But this results in:
Failures:
1) book specific routes should recognize title in path
Failure/Error: {:get => "book/mytitle"}.should route_to(:controller => "book", :action => "index", :title => "mytitle")
undefined method `recognize_path' for nil:NilClass
# ./spec/route_spec.rb:9:in `block (2 levels) in <top (required)>'
Any idea where the nilClass is coming from? Any help would be appreciated.
Upvotes: 1
Views: 672
Reputation: 47548
It's the double include of ActionDispatch::Assertions::RoutingAssertions
which causes the failure -- not sure why. Remove the two include
statements and all should be well. The spec file should live in /spec/routing
. You can wrap the example with describe BooksController
for style points, but it will work without it.
Upvotes: 2
Reputation: 16274
I'm guessing the matchers need to be used inside a controller spec. They should already be there, so there would be no need to include them manually. Just make sure you're describing a controller.
describe BooksController do
it "should recognize title in path" do
# ...
end
end
Upvotes: 1