Reputation: 9381
I have a test in RSpec that checks for an edit link if one of the articles in the list belongs to the logged in user:
let(:user) { Fabricate(:user) }
before do
visit new_user_session_path
fill_in 'Email', :with => user.email
fill_in 'Password', :with => user.password
click_button 'Sign in'
end
describe 'when user has a self-authored article' do
let(:article) { Fabricate(:article, :author_id => user.id) }
before { visit articles_path }
it { should have_link('Edit article', :href => edit_article_path(article)) }
end
These are the views that it tests:
# articles/new.html.erb
<% provide(:title, 'Articles') %>
<h1>Articles</h1>
<% if user_signed_in? %>
<%= link_to 'Post article', new_article_path %>
<% end %>
<%= render @articles %>
<%= will_paginate %>
This is the specific view that it tests:
# articles/_article.html.erb
<% if user_signed_in? %>
<% if current_user.id == article.author.id %>
<%= link_to 'Edit article', edit_article_path(article) %>
<% end %>
<% end %>
When I run the test, I get the following error:
Failure/Error: it { should have_link('Edit article', :href => edit_article_path(article)) }
expected link "Edit article" to return something
I checked my browser to confirm that the test is right, but the link seems to appear as intended. Where did I go wrong? How do I make the test pass?
Upvotes: 1
Views: 97
Reputation: 9381
I have figured it out. It turns out that I had a misunderstanding of how let
works. I thought it could be executed before before
, but I was wrong. I fixed the issue by fabricating the object inside the before
block
describe 'when user has a self-authored article' do
before do
@article = Fabricate(:article, :author_id => user.id)
visit articles_path
end
it { should have_link('Edit article', :href => edit_article_path(@article)) }
end
end
Upvotes: 0
Reputation: 9000
I'm guessing that let(:article) { Fabricate(:user, :author_id => user.id) }
is creating a user, not an article.
Upvotes: 2