Question

I am new on ruby on rails and working on a sample project to build a forum. I try to set a super admin which could delete other admins. But always cannot pass the spec.

Here is my code views/user/_user.html.erb

<li>
<%= gravatar_for user, size: 52 %>
<%= link_to user.name, user %>
<% if (current_user.admin? && !current_user?(user) && !user.admin?) ||  current_user.sadmin? %>
| <%= link_to "delete", user, method: :delete,
                data: { confirm: "You sure?" } %>
<% end %>
</li>

spec/requests/user_page_spec

 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

  describe "as an sadmin user" do
    let(:sadmin) { FactoryGirl.create(:sadmin) }
    let(:admin) {FactoryGirl.create(:admin)}
    before do
      sign_in sadmin
      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 have_link('delete', href: user_path(admin))}
    it { should_not have_link('delete', href: user_path(sadmin)) }
  end
end
end

when I try to run the spec the error is

 1) User pages index delete links as an sadmin user
     Failure/Error: it { should_not have_link('delete', href: user_path(sadmin)) }
       expected #has_link?("delete", {:href=>"/users/422"}) to return false, got true
     # ./spec/requests/user_pages_spec.rb:66:in `block (5 levels) in <top (required)>'

  2) User pages index delete links as an sadmin user
     Failure/Error: it { should have_link('delete', href: user_path(admin))}
       expected #has_link?("delete", {:href=>"/users/423"}) to return true, got false
   # ./spec/requests/user_pages_spec.rb:65:in `block (5 levels) in <top (required)>'

I am not sure if it means the syntax I use in erb file is wrong or some other things I messed up? Thanks in advance

Was it helpful?

Solution

In

    <% if (current_user.admin? && !current_user?(user) && !user.admin?) ||  current_user.sadmin? %>
        | <%= link_to "delete", user, method: :delete,
                data: { confirm: "You sure?" } %>
    <% end %>

, it mean if "current_user.sadmin? " is true, the link will be displayed no matter other conditions. So ther is the failure

Failure/Error: it { should_not have_link('delete', href: user_path(sadmin)) }
       expected #has_link?("delete", {:href=>"/users/422"}) to return false, got true

I guess

it { should have_link('delete', href: user_path(admin))}

failed, is becuase the admin user has been deleted in

it "should be able to delete another user" do
      expect do
        click_link('delete', match: :first)
      end.to change(User, :count).by(-1)
    end

part?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top