Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialGeoffrey Emerson
18,726 PointsSpec Error: "undefined method `id' for nil:NilClass". The hacky workaround is not working for me.
The error refers to spec/controllers/users_controller_spec.rb
as discussed in the first few minutes of this video.
it "sets the session user_id to the created user" do
post :create, {:user => valid_attributes}, valid_session
expect(session[:user_id]).to eq(User.find_by(email: valid_attributes["email"]).id)
end
$ bin/rspec --format=documentation spec/controllers/users_controller_spec.rb
1) UsersController POST create with valid params sets the session user_id to the created user
Failure/Error: expect(session[:user_id]).to eq(User.find_by(email: valid_attributes["email"]).id)
NoMethodError:
undefined method `id' for nil:NilClass
# ./spec/controllers/users_controller_spec.rb:103:in `block (4 levels) in <top (required)>'
I can only assume that for some reason it doesn't know that a User has an id
.
I suspect I'm getting this error because earlier in the course I updated the rspec gem to get around another error. I've been able to work past all the other little problems this update has caused, but this one is beyond me. A google search indicated that stubbing might help get past it, but I really can't figure out how stubbing works.
Ruby v2.0
Rails v4.0.1
Full project code: git@bitbucket.org:GeoffreyEmerson/odot.git
1 Answer
Chris Ward
12,129 PointsGlad to see you solved the problem. It's important to remember that the two naming forms aren't interchangeable, a variable starting with a colon can be stored differently than one with quotes. Coming from C++ and other languages to Ruby, this was one area that tripped me up. Hashes can help speed up searches and the value of adding them when you ordinarily wouldn't shouldn't be over looked. You could use a trapdoor function like md5 or sha256/512. Even a rudimentary partitioning attempt will suffice like combining the first four bytes of a file with its final four bytes. Well, good luck on your programming journey!
Geoffrey Emerson
18,726 PointsGeoffrey Emerson
18,726 PointsFigured it out ten minutes after posting the question. Sheesh.
For some reason the gems that I'm using don't like to return hash values with the key in quotes.
Changed
valid_attributes["email"]
to:
valid_attributes[:email]
and it started working fine.