0
votes

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"

1
user_path(admin) is a rails helper...checkout guides.rubyonrails.org/routing.html and look for 'path and url helps' (section 2.3)Neil Billingham
sign_in admin could possibly be a devise helper (are you using devise?). Checkout spec_helper.rb in your specs directory; you should be able to see any helpers that are included to run with your specs.Neil Billingham
This test is really just checking if certain links appear on the UI and that one of them actually deletes a User. It looks like it's capybara right? These tests are made to test behaviour of stuff (i.e. they're BDD orientated). In this case that the UI will render in a certain way and that a link will delete something. But it looks like you want to test that a data value is something?Neil Billingham
Oh, apologies, one more thing; FactoryGirl is creating the admin user, probably in the test database (and it will probably get deleted at the end of the test, so you might not be able to see it). You should have a directory in the specs directory called factories and probably a file in there called users.rb right?Neil Billingham
Cool, having all the factories in one file is ok; some people (like me) just separate them to organise stuff. Are you still stuck on this? If so is there any chance you could clarify your model/table structure? I'm guessing you have something like 1 user has many roles? Maybe you could copy/paste the model classes into your question?Neil Billingham

1 Answers

0
votes

I think you may be confusing what you need to do with your test and what you need to do in your production code.

If you change the definition of FactoryGirl(:admin) as you have done, then your test is fine as is.

However, you need to change your production code so that "admin-ness" is defined in terms of role as well, and you haven't mentioned that.