3

I tried testing routes and just copied the example from the rspec-rails documentation.

describe "routing to profiles" do
  it "routes /profile/:username to profile#show for username" do
    expect(:get => "/profiles/jsmith").to route_to(
      :controller => "profiles",
      :action => "show",
      :username => "jsmith"
    )
  end
end

I got the following error when running RSpec:

Failures:

  1) routing to profiles routes /profile/:username to profile#show for username
     Failure/Error: expect(:get => "/profiles/jsmith").to route_to(
     ArgumentError:
       wrong number of arguments (1 for 0)
     # ./spec/routing/test_spec.rb:11:in `block (2 levels) in <top (required)>'

Finished in 0.001 seconds
1 example, 1 failure

What's going wrong here?

Thanks for any help.

vision810
  • 113
  • 3
  • 7

3 Answers3

2

expect takes a block. This means use curly brackets:

expect{ :get => "/profiles/jsmith" }.to route_to(

Reference: RSpec Expectations 2.0

I don't think you need expect anyway. Code from Testing an RSpec controller action that can't be accessed directly

describe "routing" do
  it "routes /auth/:provider/callback" do
    { :post => "/auth/twitter/callback" }.should route_to(
      :controller => "authentications",
      :action => "create",
      :provider => "twitter")
  end
end
Community
  • 1
  • 1
B Seven
  • 44,484
  • 66
  • 240
  • 385
  • 2
    This is only true for old versions of rspec prior to 2.11, in which `expect{}.to` was just sugar for `lambda{}.should`. As of 2.11 `expect` can take arguments for value assertions, just as in the OP's example. See: http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax#unification_of_block_vs_value_syntaxes – pje Nov 04 '12 at 01:29
  • 2
    My version of RSpec turned out to be the problem, since it was an older one. Having said that, the rspec-rails documentation on http://github.com/rspec/rspec-rails differs from http://rubydoc.info/gems/rspec-rails/frames, so its a bit confusing... – vision810 Nov 11 '12 at 11:36
  • It was my rspec version that was causing the same problem for me. Thanks for posting! – BenU Mar 11 '13 at 19:59
0

Your it block lists /profile/:username while your example lists /profiles/jsmith (note the plural on profile(s)). I'm guessing it should be /profile/jsmith.

Jason Noble
  • 3,756
  • 19
  • 21
0

The syntax for the get command is get your_action, params => your_params. I would try using get :show, username => "user" in conjunction with curly braces as B Seven suggested. You also may need to wrap your spec in a describe block such as describe MyController do, or possibly pass in the name of the controller as parameter

ki4jnq
  • 91
  • 1
  • 4