Rspec failure with :get. :get documentation?

Rspec failure with :get. :get documentation? I’m trying to simultaneously learn Rspec as well as implement a Rails app. I am a complete Rspec novice.

I’m converting some code from Rails 4 to Rails 5.1.1 so that may be where the following problems lies.

Part of the Rspec messages I’m getting is:

  1. ArticlesController GET #new returns a success response

    Failure/Error: get :new, {}, valid_session

    ArgumentError:

    wrong number of arguments (given 2, expected 1)

    ./spec/controllers/articles_controller_spec.rb:64:in `block (3 levels) in <top (required)>’

  2. User profile page user cannot see another user’s profile

    Failure/Error: expect(page).to have_content ‘Access denied.’

    expected to find text “Access denied.” in “Home Menu Users Log out You are not authorized to perform this action. hello”

    ./spec/features/user_show_spec.rb:18:in `block (2 levels) in <top (required)>’

. . .

The lines around spec/controllers/articles_controller_spec.rb:62 are

if true

describe “GET #new” do

it "returns a success response" do

  byebug

  get :new, {}, valid_session

  expect(response).to be_success

end

end

end

As you can see, there is a byebug I put into articles_controller_spec.rb. When I trace into (what I think is) the get, I end up with this:

[61, 70] in spec/controllers/articles_controller_spec.rb

61:

62: if true

63: describe “GET #new” do

64: it “returns a success response” do

65: byebug

=> 66: get :new, {}, valid_session

67: expect(response).to be_success

68: end

69: end

70: end

(byebug) s

[293, 302] in /home/real-estate-data-mining/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/rspec-core-3.6.0/lib/rspec/core/memoized_helpers.rb

293: # Apply the memoization. The method has been defined in an ancestor

294: # module so we can use super here to get the value.

295: if block.arity == 1

296: define_method(name) { __memoized.fetch_or_store(name) { super(RSpec.current_example, &nil) } }

297: else

=> 298: define_method(name) { __memoized.fetch_or_store(name) { super(&nil) } }

299: end

300: end

301:

302: # Just like let, except the block is invoked by an implicit before

(byebug)

Questions:

  1. Does the message “wrong number of arguments (given 2, expected 1)” refer to get?

  2. Where can I find documentation on get?

  3. Why does byebug’s ‘s’ end up on line 298 in method let? (Yes, let, not get.) and not on the first executable statement in let?

  4. Am I missing any relevant info in this posting?

  5. And, finally, what is wrong with line 66 in articles_controller_spec.rb?

I'm converting some code from Rails 4 to Rails 5.1.1 so that may be where the following problems lies.

Part of the Rspec messages I'm getting is: 1) ArticlesController GET #new returns a success response     Failure/Error: get :new, {}, valid_session

    ArgumentError:       wrong number of arguments (given 2, expected 1)

describe "GET #new" do    it "returns a success response" do      byebug      get :new, {}, valid_session

replace the line above with:

       get :new, params: {}, session: valid_session

     expect(response).to be_success    end end

I *believe* if you'd gone through converting to Rails 5.0 first you'd have seen deprecation warnings about that. I think :slight_smile:

In any case, HTH.

Thank you so much, Hassan.

I didn’t see any warnings.

Is there documentation I can look at that explains what “get” does?

Ralph

I'll assume not meaning "get" the HTTP verb, but rather how rspec implements it for test purposes? Dunno about the latter, probably need to dig into the source for that.

H*

Questions:

1) Does the message "wrong number of arguments (given 2, expected 1)" refer to get?

Yes

2) Where can I find documentation on get?

I would start here: Testing Rails Applications — Ruby on Rails Guides

3) Why does byebug's 's' end up on line 298 in method let? (Yes, let, not get.) and not on the first executable statement in let?

Ruby evaluates the arguments to get before calling get itself. In your case this is the valid_session,which was dynamically define by let, which is why you jump into let related code first. Were it not the the number of arguments error you would end up in get if you kept stepping

4) Am I missing any relevant info in this posting?

5) And, finally, what is wrong with line 66 in articles_controller_spec.rb?

Hassan's given you the answer there. Controller tests changed very substantially in rails 5 (rspec's controller specs are just a very thin layer on top of rails controller tests, so they've inherited these changes). There have been quite a few blog posts written about those changes (in addition to what is in the rails release notes), I am sure a quick Google will dig them up.

Fred