I made it up to Chapter 9 of the Ruby on Rails tutorial, and added functionality of my own to lock a user when they first sign up, such that an admin has to go in and approve (“unlock”) their id before a new user has access to the site. I added a :locked boolean attribute that works just like the :admin attribute of the User object. I have that all working now, but I’m having trouble writing a simple test for it. I added the following test to user_pages_spec.rb, just under the hierarchy “pagination” – “as an admin user”:
describe "as an admin user to unlock new users" do
let(:admin) { FactoryGirl.create(:admin) }
let(:locked_user) { FactoryGirl.create(:locked) }
before do
sign_in admin
visit users_path
end
it { should have_link('unlock', href: user_path(locked_user)) }
it { should_not have_link('unlock', href: user_path(admin)) }
end
and to support the creation of a “locked” user, added this to factories.rb:
factory :locked do
locked true
end
I can confirm manually through Firefox that the unlocking link shows up, but I’m still getting the following failure:
1) User pages index pagination as an admin user to unlock new users
Failure/Error: it { should have_link('unlock', href: user_path(locked_user)) }
expected link "unlock" to return something
# ./spec/requests/user_pages_spec.rb:64:in `block (5 levels) in <top (required)>'
I’m interested in knowing a) why this fails :), but also b) how to go about debugging a problem like this. How do I tell what the test is actually “seeing”? I tried rails-pry with a different problem as suggested by another stackoverflow user, but in this case I’ve found it of limited use.
Any ideas or suggestions would be much appreciated. Thanks in advance!
-Matt
You are supposed to write the test first 😉
Using your test as a start, I have been working through the process. I have gotten to the point of getting the same error as you. Using the pry-rails gem and putting binding.pry in the test:
(After messing around a lot) I copy and paste from the test to the command prompt:
and get the error. Changing it to:
works. Entering locked_user at the prompt shows me the user record. Next I entered page.body and it shows me my locked user doesn’t even show up on the page. (Confirmed by entering User.count and found it to be 33 so it was on page 2.) You may not have this problem depending on how deeply embedded your test is the specs. I realized I had embedded it inside another spec accidentally. When I moved it out (User.count == 2), it still didn’t work. My locked_user still wasn’t on the page. User.all didn’t include the user either. Hartl mentions in chapter 10,
Changed the let to let! and it worked. (User.count == 3, including the locked_user this time.) Here is the test block.
My code doesn’t unlock anything yet (I’ll need another test for that…) but the link shows up when it should. 🙂