I have a problem understanding where a specific part of code in MHartls tutorial comes from (and because of that I can't seem to change it to better fit my needs...) The part I am talking about is an rspec test:
describe "delete links" do
it { should_not have_link('delete') }
describe "as an admin user" do
let(:admin) { FactoryGirl.create(:admin) }
before do
sign_in admin
visit users_path
end
it { should have_link('delete', href: user_path(User.first)) }
it "should be able to delete another user" do
expect do
click_link('delete', match: :first)
end.to change(User, :count).by(-1)
end
it { should_not have_link('delete', href: user_path(admin)) }
end
Right now the test checks if a an admin attribute of a user is true or false. I am instead trying to check if the role attribute of a user contains the word 'Administrator'. The model is working, the views show everything correctly - but I don't know how to rewrite the test, right now it is failing.
One part that puzzles me specifically is "sign_in admin" and "user_path(admin)" - where does this come from? How does rspec know who the admin is? And if it's an attribute that is somewhere defined, can I simply change it from admin = true/false to admin is true if role is administrator ?
Many thanks for your help!
Updated:
I do have a file called factories.rb, here is the content according to Mhartls tutorial:
FactoryGirl.define do
factory :user do
sequence(:name) { |n| "Person #{n}" }
sequence(:email) { |n| "person_#{n}@example.com"}
password "foobar"
password_confirmation "foobar"
factory :admin do
admin true
end
end
end
I tried changing it into:
FactoryGirl.define do
factory :user do
sequence(:first_name) { |n| "First #{n}" }
sequence(:last_name) { |n| "Last #{n}" }
sequence(:primary_email) { |n| "person_#{n}@example.com"}
password "foobar"
password_confirmation "foobar"
factory :role do
role "Administrator"
end
end
end
Additional information in response to your comments:
- I am not using Devise - the user model is build based on the railstutorial with minor modifications (more form fields plus the admin field is in my case a field "role" with a string)
- It is clear what user_path is - but why is there an (admin) behind it?
- I am trying to test exactly what is in the test - the problem is that FactoryGirl tests with a user whose admin attribute is set to true.
Just to summarise:
I do not want to change what the test is testing - I only want to change how an administrator gets identified by factorygirl. Right now it tests if the attribute "admin" is set to true or false. I want it to check if the attribute "role" has the content "Administrator"