Reputation: 2665
I can't seem to get these Rspec tests to pass. I just don't know what exactly I'm doing wrong. Any advice? These tests make sure that every hunt has an edit and delete link when admins are logged in. I'm using FactoryGirl to generate a test admin user and a test hunt.
Here are the tests.
describe "for users who are admins" do
before(:each) do
admin = FactoryGirl.create(:user, :email => "[email protected]", :admin => true)
test_sign_in(admin)
end
it "should show edit link" do
get 'index'
Hunt.each do |hunt|
response.should have_selector('a', :href => "edit",
:content => "edit")
end
end
it "should show delete link" do
get 'index'
Hunt.each do |hunt|
response.should have_selector('a', :href => "delete" ,
:content => "delete")
end
end
Here's the code I'm testing. It's from views/hunts.html.erb
<h1>All Hunts</h1>
<%= will_paginate %>
<ul>
<% @hunts.each do |hunt| %>
<li>
<%= link_to hunt.name, hunt %>
<% if current_user && current_user.admin? %>
| <%= link_to "edit", edit_hunt_path(hunt) %>
| <%= link_to "delete", hunt, :method => :delete, :confirm => "You
sure?", :title => "Delete #{hunt.name}" %>
<% end %>
</li>
<% end %>
</ul>
<%= will_paginate %>
<%= link_to( "Create New Hunt", '/hunts/new') %>
And here's the relevant part of the hunt controller.
class HuntsController < ApplicationController
def index
@title = "All Hunts"
@hunts = Hunt.paginate(:page => params[:page])
end
....
end
When I run rspec, this is what appears.
</li>
<li>
<a href="/hunts/30">---
- optio
- et
- sunt
</a>
| <a href="/hunts/30/edit">edit</a>
| <a href="/hunts/30" data-confirm="You sure?" data-method="delete" rel="nofollow" title="Delete ---
- optio
- et
- sunt
">delete</a>
Finally, here's my factories.rb file.
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
factory :hunt do
name "Test Hunt"
end
end
UPDATE: So I dropped the "a" from the original tests. I also rewrote the test so that it looks at each hunt on page 1.
describe "for users who are admins" do
before(:each) do
admin = FactoryGirl.create(:user, :email => "[email protected]", :admin => true)
test_sign_in(admin)
end
it "should show edit link" do
get 'index'
Hunt.paginate(:page => 1).each do |hunt|
response.should have_selector(:href => "edit",
:content => "edit")
end
end
it "should show delete link" do
get 'index'
Hunt.paginate(:page => 1).each do |hunt|
response.should have_selector(:href => "delete",
:content => "delete")
end
end
end
So now I've got a different error:
HuntsController GET 'index' for users who are admins should show edit link
Failure/Error: response.should have_selector(:href => "edit",
Nokogiri::CSS::SyntaxError:
unexpected '{' after ''
I tried googling this error, but haven't had much luck. Seen it before? I'm not sure what syntax I'm messing up.
Upvotes: 1
Views: 1327
Reputation: 359
What if you change the selector to the following:
it "should show an 'edit' link" do
get :index
...
response.should have_selector('a', :href => edit_hunt_path(hunt), :content => 'edit')
...
end
If you are using Capybara then you can simply use have_link() instead of have_selector().
Upvotes: 1