click and current_path with rspec-rails and capybara - capybara

this code
describe "HomePages" do
subject {page}
before { visit root_path }
describe "navigation" do
describe "click signup" do
before {find_button('Sign up').click}
current_path.should == sign_up_path
end
end
end
Gives me this error
#<NameError: undefined local variable or methodcurrent_path' for #>`
I'm using capybara version 1.1.2
What am I doing wrong?

I moved them into a describe block and it worked.

Related

Test layout in rails 4 with Rspec 3

I put a bootstrap navbar in my layout and want to test whether all the links are correct.
require 'rails_helper'
describe "layouts/application.html.erb" do
it "should have the right links in navbar" do
render
expect(page).to have_link('Home', href: "/home/index")
expect(page).to have_link('Games', href: "/games/index")
expect(page).to have_link('Programming', href: "/programming/index")
expect(page).to have_link('Bananenmannfrau', href: "/pages/about")
end
end
This just returns error:
layouts/application.html.erb should have the right links in navbar
Failure/Error: render
NameError:
undefined local variable or method `render' for #<RSpec::ExampleGroups::LayoutsApplicationHtmlErb:0x00000005a8f750>
# ./spec/layouts/application.html.erb_spec.rb:8:in `block (2 levels) in <top (required)>'
Is this even the correct way of testing it and when yes what am I missing?
You should use capybara gem for testing front end!
Capybara
Then you will be able to use it like:
require 'rails_helper'
describe "layouts/application.html.erb" do
it "should have the right links in navbar" do
visit root_path
within(".nav-bar-selector") do
expect(page).to have_content('Home')
expect(page).to have_content('Games')
expect(page).to have_content('Programming')
expect(page).to have_content('Bananenmannfrau')
end
end
end

Rspec / Capybara issues -- Capybara won't visit page

Ok so I'm kinda new, but have somewhat of an idea of what I'm doing, but this one just has me stumped for the past couple hours, so any help is very much appreciated. I'm building a site and have been using Rspec and Capybara to test my site as I'm moving along. I ended up needing to remove turbolinks to improve functionality for my jscripts. The next time I try to run tests, literally 50% of my test suite just magically broke. I've narrowed it down to that the commonality between all the failures was that the "visit" either appeared in or just before the code block. So basically removing Turbolinks somehow blew Capybara up or Rspec. I'm really having some trouble figuring this one out. I tried updating the gems, that didn't work. I guess the next step is either skip the TDD concept, which I don't want to do, or start uninstalling the gems and do a reinstall and pray that that doesn't render my app useless... Any help anyone can provide is much appreciated, and if you're in NYC I'll buy you a beer.
There are also other tests that are failing that don't require any sort of authentication, just to check the page for title and content and those are failing to. I bring that up only to say I don't think that FactoryGirl is causing the problem.
Cheers.
The errors
2) User pages profile page
Failure/Error: before { visit user_path(user) }
NoMethodError:
undefined method `user_path' for #<RSpec::ExampleGroups::UserPages::ProfilePage:0x00000004290410>
# ./spec/requests/user_pages_spec.rb:10:in `block (3 levels) in <top (required)>'
3) User pages profile page
Failure/Error: before { visit user_path(user) }
NoMethodError:
undefined method `user_path' for #<RSpec::ExampleGroups::UserPages::ProfilePage:0x00000004207c00>
# ./spec/requests/user_pages_spec.rb:10:in `block (3 levels) in <top (required)>'
4) User pages signup page
Failure/Error: before { visit signup_path }
NameError:
undefined local variable or method `signup_path' for #<RSpec::ExampleGroups::UserPages::SignupPage:0x000000041b3088>
# ./spec/requests/user_pages_spec.rb:18:in `block (3 levels) in <top (required)>'
The test suite code
require 'spec_helper'
describe "User pages" do
subject { page }
#Profile tests
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
before { visit user_path(user) }
it { should have_content(user.name) }
it { should have_title(user.name) }
end
#Signup page tests
describe "signup page" do
before { visit signup_path }
it { should have_content('Sign up') }
it { should have_title(full_title('Sign up')) }
end
describe "signup" do
before { visit signup_path }
let(:submit) { "Create my account" }
describe "with invalid information" do
it "should not create a user" do
expect { click_button submit }.not_to change(User, :count)
end
end
describe "with valid information" do
before do
fill_in "Name", with: "Example User"
fill_in "Email", with: "user#example.com"
fill_in "Password", with: "foobar00"
fill_in "Confirmation", with: "foobar00"
end
it "should create a user" do
expect { click_button submit }.to change(User, :count).by(1)
end
end
end
end
try to remove your visiting pages from before blocks .
So , you just need :
describe "signup page" do
feature "should have content 'Sign up' "
visit signup_path
it { should have_content('Sign up') }
end
feature "should have full title 'Sign up' "
visit signup_path
it { should have_title(full_title('Sign up')) }
end
end
And you need to do like this in every describe block .
Ok so to anyone else that runs into this issue, I did some more digging it's not a so uncommon occurrence for Capybara to get weird at times. But I got the route errors to fix and pass green by adding "config.include Rails.application.routes.url_helpers" to the spec_helper.rb file.
Rspec.configure do |config|
*
*
*
config.include Rails.application.routes.url_helpers
end
Unfortunately I can't provide any insight why that line needed to be added, when previously it was not needed. I'll have to leave that answer to someone with more knowledge than myself.

launchy gem terminates tests

I've got a simple spec
describe "Workers" do
describe "user attempts to view workers" do
context "admin logged in" do
end
context "non-logged in user" do
before do
visit workers_path
end
it "should redirect to home page" do
page.should have_content 'You are not authorized to access this page.'
end
end
end
end
This passes just fine. If I add save_and_open_page, the test run terminates and the output says "Process finished with exit code 0", but says nothing of the test status.
describe "Workers" do
describe "user attempts to view workers" do
context "admin logged in" do
end
context "non-logged in user" do
before do
visit workers_path
end
it "should redirect to home page" do
save_and_open_page
page.should have_content 'You are not authorized to access this page.'
end
end
end
end
Capybara: 2.1.0
launchy: 2.3.0
rspec: 2.13.0
rails: 3.2.13
Must have been a bad install of launchy. I downgraded to launchy 2.2.0 and everything worked, then I upgraded (back) to launchy 2.3.0 and things seem to be working fine.

using Rspec and internationalization with nice routing (current_path.should == new_post_path )

I'm creating a multilanguage CMS with Rails, Rspec (for test) and capybara.
the problem appear when i wrote this test
it "redirect and create a new post" do
visit posts_path
create_new_post = I18n.t('posts.index.create_new_post')
click_link create_new_post
current_path.should == new_post_path
end
problem is in last line and i got this
expected: "/posts/new"
got: "/en/posts/new" (using ==)
i want to use something like "new_post_path", not "/en/posts/new"
the main problem depicted when i use something like this
it "redirect to show link" do
visit posts_path
page.should have_content 'show'
click_link 'show'
current_path.should == post_path(#post)
end
any help is appreciate.
Check this StackOverflow question for some ideas on this subject. My preferred way is this answer on the same question. But, using your current code, to get your expectations right you would perhaps do something like this (assuming you want to test all of your locales):
I18n.available_locales.each do |locale|
it "should redirect and create a new post" do
visit posts_path(locale)
create_new_post = I18n.t('posts.index.create_new_post')
click_link create_new_post
current_path.should == new_post_path(locale)
end
it "should redirect to show link" do
visit posts_path(locale)
page.should have_content 'show'
click_link 'show'
current_path.should == post_path(locale, #post)
end
end

Testing links using rspec?

If I have something like <%= link_to "Help", help_path %> in the view file, how do I test the following using rspec?
It goes to help_path.
The content reads "Help".
I am currently using capybara.
edit
While answers below can work, I think I found a simpler way of doing this.
describe "some link on whatever page" do
subject { page }
before { visit whatever_path }
it { should have_link('Help', href: help_path) }
end
If you are using capybara then
my_link = find(:css, "a:contains('Help')")
my_link.click
and get page status, should be 200 or simply check
my_link.href
But also you could simply add routes specs to your integration specs in rspec. Like this to be always sure this routes exists
describe HelpController do
it "should have proper routes" do
{:get => "/halp/772/parser/18/edit" }.should be_routable
{:post => "/halp/772/parser" }.should be_routable
{:delete => "/halp/772/parser/18" }.should be_routable
end
end
Your file could look like this:
require 'spec_helper'
feature "Help" do
scenario "creation" do
visit help_path # access your help path
page.should have_content("Help") # verify content
end
end
If you need some help with testing with Capybara you could find some here:
Railscasts #257 Request Specs and Capybara or
Capybara repo at Github
Use have_tag matcher. Here is a cheatsheet.
response.should have_tag("a[href$=#{help_path}]", :text => "Help")
P.S. Did you read rails guides?
click_link "Help"
Gist of a capybara cheatsheet: https://gist.github.com/zhengjia/428105

Resources