Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJoshua Shroy
9,943 PointsCapybara not finding link in support file.
I'm following along this course as a guide while developing my own app. I'm currently trying to add the spec/support/authentication_helpers.rb helper file.
In the video, Jason uses stubs to stub out a current_user
and user_id
. From my understanding, you are no longer able to stub in a before
block, so I'm having my helper file manually sign_in using Capybara.
Basically, I'm copying the sign_in process from the spec/features/users/registration_spec.rb file. That Capybara spec (with the same syntax) passes with no issues, however, my authentication_helpers.rb
fails..
1) PostsController#GET create with valid attributes creates a post by the current_user
Failure/Error: sign_in(build_stubbed(:user))
Capybara::ElementNotFound:
Unable to find link "Sign Up"
# ./spec/support/authentication_helpers.rb:7:in `sign_in'
# ./spec/controllers/posts_controller_spec.rb:44:in `block (4 levels) in <top (required)>'
spec/support/authentication_helper.rb
require 'rails_helper'
module AuthenticationHelpers
def sign_in(x)
visit root_path
click_link "Sign Up"
fill_in "First Name", with: x.first_name
fill_in "Last Name", with: x.last_name
fill_in "Email", with: x.email
fill_in "Password", with: x.password
fill_in "Password Confirmation", with: x.password
click_button "Sign Up"
end
end
spec/controllers/posts_controller_spec.rb
before :all do
sign_in(build_stubbed(:user))
end
describe '#GET create' do
context 'with valid attributes' do
before :each do
post :create, post: attributes_for(:post)
end
it { expect(Post.count).to eq(1) }
it { expect(flash[:success]).to eq('Your post has been saved!') }
it { expect(assigns(:post)).to be_a(Post) }
it { expect(assigns(:post)).to be_persisted }
it { expect(response).to redirect_to Post.first }
it "creates a post by the current_user" do
@post = Post.last
expect(@post.user).to eq(user)
end
end
_header.html.erb
<div class="cbp-af-header">
<div class="cbp-af-inner">
<nav>
<a href="#">About</a>
<a href="#">Services</a>
<a href="#"><%= link_to "Sign Up", new_user_path %></a>
<a href="#"><%= link_to "Log In", new_user_session_path %></a>
</nav>
</div>
</div>
rake routes
Prefix Verb URI Pattern Controller#Action
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
user_sessions POST /user_sessions(.:format) user_sessions#create
new_user_session GET /user_sessions/new(.:format) user_sessions#new
root GET / posts#index
post_comments GET /posts/:post_id/comments(.:format) comments#index
POST /posts/:post_id/comments(.:format) comments#create
new_post_comment GET /posts/:post_id/comments/new(.:format) comments#new
edit_post_comment GET /posts/:post_id/comments/:id/edit(.:format) comments#edit
post_comment GET /posts/:post_id/comments/:id(.:format) comments#show
PATCH /posts/:post_id/comments/:id(.:format) comments#update
PUT /posts/:post_id/comments/:id(.:format) comments#update
DELETE /posts/:post_id/comments/:id(.:format) comments#destroy
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
admin GET /admin(.:format) admin/dashboard#index
1 Answer
Maciej Czuchnowski
36,441 PointsTry changing this:
<a href="#"><%= link_to "Sign Up", new_user_path %></a>
to this:
<%= link_to "Sign Up", new_user_path %>
The link_to helper generates its own a
tag, so putting a link inside a link may confuse Capybara. Same for the Log In link.
Joshua Shroy
9,943 PointsJoshua Shroy
9,943 PointsFor some reason, it decided to work once I came back to the project before trying your answer. Still not sure what was going on but I still appreciate the advice you gave. I'm sure it will come handy at some point. Thank you.