Rspec errors for administrative "remote" links fail. Michael Hartl ROR 3.2 Study Guide - Chapter 9.4.2

I follow the Michael ROR tutorial and create a user authentication system. There is an admin privilige that allows users to delete other users. Special "delete" links appear on the user list page when registering as a privileged user. My application works fine, but the rspec tests do not work for a reason unknown to me.

I separated the tests to another spec/requests/sat_spec.rb and I am trying to use pry gem to debug it, but have not achieved anything so far.

 describe "delete links" do describe "as admin user" do let(:admin) { FactoryGirl.create(:admin) } before do sign_in admin visit users_path binding.pry end it { should have_link('delete', href: user_path(User.first)) } it "should be able to delete another user" do expect { click_link('delete') }.to change(User, :count).by(-1) end end 

Test failures:

 1) separated admin tests delete links as admin user Failure/Error: it { should have_link('delete', href: user_path(User.first)) } expected link "delete" to return something # ./spec/requests/sat_spec.rb:25:in `block (4 levels) in <top (required)>' 2) separated admin tests delete links as admin user should be able to delete another user Failure/Error: expect { click_link('delete') }.to change(User, :count).by(-1) Capybara::ElementNotFound: no link with title, id or text 'delete' found # (eval):2:in `click_link' # ./spec/requests/sat_spec.rb:28:in `block (5 levels) in <top (required)>' # ./spec/requests/sat_spec.rb:28:in `block (4 levels) in <top (required)>' 

What could be connected with the problem here or , more importantly, how to debug it?

You can unlock my code here https://github.com/tomek-rusilko/miniatury_katalog_2

+6
source share
2 answers

You expect your /users page to contain a list of users with a delete link next to them. But you did not fill your TEST db with simple users. It just contains one user, administrator. But, according to your users/_user.html.erb , this type of user does not have a delete link. So, add at least one user create statement and try again.

+9
source
 let(:admin) { FactoryGirl.create(:admin) } 

This is normal as you have it (in spec / factories.rb)

 factory :admin do admin true end 

But for now in /user.rb models:

 attr_accessible :name, :email, :password, :password_confirmation 

My bet is that the factory girl uses the mass assignment to set: admin => true, which is then discarded by attr_accessible.

But more importantly, how do you debug it? By asking "what's going on?" and "what am I expecting?" and variations on it until you find that the answers to both questions are in the middle. In this case, the questions I'm starting with are: what does this make the link display? Is this true for the model I created in the spec? Is the spec a hit on the pages I expect? Is the model the same when I hit the page with a browser, as it is in the spec?

But all this is just a variation of "what is happening?" and "What am I expecting?"

0
source

Source: https://habr.com/ru/post/916749/


All Articles