trebek1
trebek1

Reputation: 775

Rspec undefined method `route_to' when testing one route?

I am new to Rspec and am trying to test the one route in my application. I have installed Rspec and have included the routing file in spec/routing/routes_spec.rb.

My spec is as follows:

require "spec_helper"

describe "Routes" do
  it "routes get index" do
    expect(:get => "simulations").to route_to(
      :controller => "simulations",
      :action => "index"
    )
  end
end

I get this error:

Routes routes get index
    Failure/Error: expect(:get => "simulations").to route_to(
    NoMethodError:
      undefined method `route_to' for #<RSpec::ExampleGroups::Routes:0x007fc32d2f70b8 @__memoized=nil>
    # ./spec/routing/routes_spec.rb:6:in `block (2 levels) in <top (required)>'

Any ideas as to why route_to would be undefined? I have verified that the route actually works.

Upvotes: 3

Views: 3248

Answers (3)

Matilda Smeds
Matilda Smeds

Reputation: 1504

As Fire-Dragon-DoL suggests above, you might like to check that the rspec-rails gem is in place.

When you don't have rspec-rails installed and required, and you use to_route method, you will get the same error when running your specs: NoMethodError: undefined method 'route_to'

Given the same setup, when you use be_routable matcher, then you get another error, in the style of: expected {:get=>"/my_models/} to respond to 'routable?'

To remedy these errors

  1. Add rspec-rails to Gemfile
  2. Run bundle install
  3. Add require 'rspec/rails' to spec_helper (or rails_helper)

Upvotes: 1

trebek1
trebek1

Reputation: 775

In Rspec 3 you should require 'rails_helper' rather than require 'spec_helper'.

Upvotes: 7

Francesco Belladonna
Francesco Belladonna

Reputation: 11719

Based on documentation:

Routing specs are marked by :type => :routing or if you have set config.infer_spec_type_from_file_location! by placing them in spec/routing.

So, unless you set the previous option, you should begin your spec with:

describe "Routes", :type => :routing do

Upvotes: 3

Related Questions