I just upgraded to Capybara 2.0.0.beta4 with rspec-rails 2.11.4 and I moved my request spec (I only have one) to spec/features as advised by the Capybara-Readme in the RSpec-Rails repository.
When I run the tests now it does not find any paths. So for the following test block:
it "should be able to access the signup page through the front page" do
visit root_path
click_link "Signup For Free Now"
page.should have_content("Signup")
end
I get the error message:
Failure/Error: visit root_path
NameError: undefined local variable or method `root_path' for #<RSpec...>
When I try to run the test with visit "/" it works fine. Other gem versions are:
rails 3.2.1
rspec 2.11.0
rack-test 0.6.2
Any ideas for a reason for the path problem?
Running "bundle update rspec-rails" to get version 2.12.0 solved the same issue for me.
Related
Just trying to get through the last bit of chapter 5 in Michael Hartl's Ruby on Rails Tutorial and getting another error running RSpec tests.
The output is :
Static pages should have the right links on the layout
Failure/Error: expect(page).to have_title('About Us')
NoMethodError:
undefined method has_title?' for #<Capybara::Session>
# ./spec/requests/static_pages_spec.rb:59:inblock (2 levels) in '
and results from the line starting expect(page) in the following code in static_pages_spec.rb :
it "should have the right links on the layout" do
visit root_path
click_link "About"
expect(page).to have_title(full_title('About Us'))
end
Note : This happens running with or without Spork
Can anyone point me in the right direction please ?
Thanks,
Bazza
The have_title function is supported from Capybara 2.1. I suppose you have an older version of Capybara in your Gemfile. So, update your Gemfile with
gem 'capybara', '2.1.0'
then update Capybara like this
bundle update capybara
and rerun the specs using rspec. It should work now
Refer to this post for other options
I'm currently in section 10.3.2 of the Rails Tutorial (beta) and ran into some problems with RSpec.
rspec spec/ runs perfectly, however if I do rspec spec/requests/micropost_pages_spec.rb as described in the tutorial I get the following error:
Failures:
1) Micropost pages micropost creation with invalid information should not create a micropost
Failure/Error: before { visit root_path }
ActionView::Template::Error:
undefined method `model_name' for NilClass:Class
# ./app/views/shared/_micropost_form.html.erb:1:in `_app_views_shared__micropost_form_html_erb__567210303736562711_70234980477780'
# ./app/views/static_pages/home.html.erb:8:in `_app_views_static_pages_home_html_erb___61006871036382679_70234970424160'
# ./spec/requests/micropost_pages_spec.rb:11:in `block (3 levels) in <top (required)>'
Performing the test manually in the web browser seems to work perfectly as well.
Not sure if I did something wrong or if there's a "bug" in the tutorial. Can anyone give a hint?
UPDATE 1: Here's a link to the whole project on GitHub.
UPDATE 2: I now found out that when I restart the rails server I get the error, too, when I hit the page the first time. But when I load the page a second time (reload) then it works perfectly.
Found the problem. Rather stupid mistake. In my static_pages_controller.rb I had
def home
def home
#micropost = current_user.microposts.build if signed_in?
end
end
Thanks anyway for the tips.
Using Rails 3.2.2, I just installed rspec and tried my first test:
require 'spec_helper'
describe "Mains" do
describe "GET index" do
it "gets the page which has the proper content" do
get 'index.html'
response.status.should be(200)
response.body.should include("one")
end
end
end
When I run the spec I get
Failures:
1) Mains GET index gets the page which has the proper content
Failure/Error: response.body.should include("one")
NoMethodError:
undefined method `chomp' for ["one"]:Array
This comes strait out of Ryan Bates' Railscast 257, so I'm a bit lost
thanks in advance,
[EDIT] The issue is corrected in rspec-expectations, on master branch. You can fix it by using this in your Gemfile:
gem 'rspec-rails', '~> 2.9'
gem 'rspec-expectations', :git => "https://github.com/rspec/rspec-expectations.git", :branch => 'master'
I encountered the same issue on Rails 3.0.12 with rspec 2.9.0. Reverting to rspec 2.8.0 / rspec-rails 2.8.1 did the trick.
I raised an issue on Github, rspec/rspec-expectations, we'll see if it's really a bug.
For reference, this is the step definition:
Then /^(?:I|they) should see "([^"]*?)" in the email body$/ do |text|
current_email.default_part_body.to_s.should include(text)
end
... the step in the feature:
step %{I should see "changer de mot de passe" in the email body}
... which was raising:
undefined method `chomp' for ["changer de mot de passe"]:Array (NoMethodError)
./features/step_definitions/email_steps.rb:110:in `/^(?:I|they) should see "([^"]*?)" in the email body$/'
I am using Cucumber with Capybara to build my testing framework for a site. I have found a way using hooks to switch between the Capybara driver in Cucumber. I want to be able just to use RackTest when I am not testing for JavaScript because I was lead to believe RackTest is faster since it doesn't render the page. Right now I have create 3 identical test that jump to Google and check that the title says Google. Each test uses a different driver. The Selenium and WebKit test work fine but the RackTest fails on the "I should see the title "Google"" test. I have attached a copy of my feature and steps and testing gems to this post.
Feature:
Feature: Rack Test
In order to run test faster
As a tester
I want to use Rack Test to preform all non-javascript related test
Scenario: Visit a page and check title
When I go to "http://www.google.com"
Then I should see the title "Google"
#firefox
Scenario: Visit a page and check title in Firefox
When I go to "http://www.google.com"
Then I should see the title "Google"
#webkit
Scenario: Visit a page and check title in WebKit
When I go to "http://www.google.com"
Then I should see the title "Google"
Steps:
When /^I go to "(http:\/\/[^"]*)"$/ do |url|
visit url
end
Then /^I should see the title "([^"]*)"$/ do |title_cont|
page.should have_selector "title", text: title_cont
end
Hooks:
Before('#firefox') do
Capybara.current_driver = :selenium
end
Before('#webkit') do
Capybara.current_driver = :webkit
end
After('#firefox, #webkit') do
Capybara.use_default_driver
end
Gemfile:
group :test do
gem 'capybara'
gem 'capybara-webkit'
gem 'cucumber'
gem 'cucumber-rails'
gem 'database_cleaner'
gem 'factory_girl_rails'
gem "rack-test", require: "rack/test"
gem 'rspec'
gem 'selenium-webdriver'
end
That's because rack-test can only test Rack applications (e.g. your Ruby On Rails website). It's not browser-based solution, while both Selenium and WebKit are. That's why rack-test can't access external websites (Google in your case).
Hi
I know this question have been asked before but the answers there isn't working for me.
I still get the, when redirecting back to my site.
/auth/failure?message=invalid_response
I have ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.6.0] installed, using rails 3.0.7 and the required gems installed. I read on another thread that you should have pure_json added to the gemfile to make it work. But that didn't help me either.
I'm clueless... Thanks in advance
authenticationscontroller
def index
#authentications = current_user.authentications if current_user
end
def create
#render :text => request.env["omniauth.auth"].to_yaml
auth = request.env["omniauth.auth"]
current_user.authentications.find_or_create_by_provider_and_uid(auth['provider'], auth['uid'])
flash[:notice] = "Authentication successful."
redirect_to authentications_url
end
I was able to get this to work by specifying the following in my Gemfile
gem 'omniauth', '0.2.0'
Version 0.2.6 wouldn't work for me. I also updated by .rvmrc to rvm use 1.9.2#rails3. Oh, also make sure you're logged in - do note that in your code above you are assuming that current_user exists. See Ryan's Railscast part two for allowing user creation via Omniauth.
I've got a demo working here, but do note I'm doing authentication from scratch rather than using Devise.