Can't Execute $ time rspec spec/requests/static_pages_spec.rb - ruby-on-rails

I'm following the Ruby on Rails Tutorial by Example by Michael Hartl but I got stuck.
Everytime I try to run the following test I got an error:
time rspec spec/requests/static_pages_spec.rb
My error:
/home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `load': cannot load such file -- /home/i/rails_projects/sample_app/spec/spec/requests/static_pages_spec.rb (LoadError)
from /home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `block in load_spec_files'
from /home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `each'
from /home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `load_spec_files'
from /home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:102:in `setup'
from /home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:88:in `run'
from /home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:73:in `run'
from /home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:41:in `invoke'
from /home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/gems/rspec-core-3.3.2/exe/rspec:4:in `<top (required)>'
from /home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/bin/rspec:23:in `load'
from /home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/bin/rspec:23:in `<main>'
from /home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/bin/ruby_executable_hooks:15:in `eval'
from /home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/bin/ruby_executable_hooks:15:in `<main>'
real 0m0.752s
user 0m0.699s
sys 0m0.044s
static_pages_spec.rb :
require 'spec_helper'
describe "StaticPages" do
describe "Home page" do
it "It should have the content 'Sample App'" do
visit '/static_pages/home'
expect(page).to have_content('Sample App')
end
it "should have the right title" do
visit '/static_pages/home'
expect(page).to have_title("Ruby on Rails Tutorial Sample App | Home")
end
end
describe "Help page" do
it "It should have the content 'Help'" do
visit '/static_pages/help'
expect(page).to have_content('Help')
end
it "should have the right title" do
visit '/static_pages/help'
expect(page).to have_title("Ruby on Rails Tutorial Sample App | Help")
end
end
describe "About page" do
it "It should have the content 'About Us'" do
visit '/static_pages/about'
expect(page).to have_content('About Us')
end
end
it "should have the right title" do
visit '/static_pages/about'
expect(page).to have_title("Ruby on Rails Tutorial Sample App | About Us")
end
describe "Contact" do
it "It should have the content 'Contact'" do
visit '/static_pages/contact'
expect(page).to have_content('Contact')
end
end
it "should have the right title" do
visit '/static_pages/contact'
expect(page).to have_title("Ruby on Rails Tutorial Sample App | Contact")
end
end
Gemfile: ## http://pastebin.com/1RhbaCiK
OS: Ubuntu 14.04
I've been searching for solution, but have not found one. Please keep in mind that I'm new to Ruby on Rails and web development.

Your error is saying:
cannot load such file -- /home/i/rails_projects/sample_app/spec/spec/requests/static_pages_spec.rb (LoadError)
which means RSpec can't find the spec. I see you have spec twice in the file path: ../sample_app/spec/spec/...
You should keep your specs under the spec directory of your Rails app. So, the path should be: /sample_app/spec/ like this. Fix that and try again. It should work.
Update
So, after running your code locally, I found the issue. There was a gem dependency issue with gem guard-spork.
You just need to upgrade this gem. In your Gemfile:
Change gem 'guard-spork', '1.5.0' to gem 'guard-spork', '2.1.0'
Then, bundle install
And, then run time rspec
Everything will run smoothly :-)

Related

Can't get rspec test from railstutorial.org chapter 3 to pass

I'm doing the rails tutorial from Michael Hartl, and I can't get the test in chapter 3 to pass.
The static_pages.rb file:
require 'spec_helper'
describe "Static pages" do
describe "Home page" do
it "should have the content 'Sample App'" do
visit '/static_pages/home'
expect(page).to have_content('Sample App')
end
end
end
The home.html.erb file:
<h1>Sample App</h1>
<p>
This is the home page for the
Ruby on Rails Tutorial
sample application.
</p>
And this is the result of the test:
An error occurred in an after hook
ArgumentError: rack-test requires a rack application, but none was given
occurred at C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/capybara-2.1.
0/lib/capybara/rack_test/driver.rb:16:in `initialize'
←[31mF←[0m
Failures:
1) Static pages Home page should have the content 'Sample App'
←[31mFailure/Error:←[0m ←[31mvisit '/static_pages/home'←[0m
←[31mArgumentError←[0m:
←[31mrack-test requires a rack application, but none was given←[0m
←[36m # ./spec/requests/static_pages_spec.rb:8:in `block (3 levels) in <top
(required)>'←[0m
Finished in 0.001 seconds
←[31m1 example, 1 failure←[0m
Failed examples:
←[31mrspec ./spec/requests/static_pages_spec.rb:7←[0m ←[36m# Static pages Home p
age should have the content 'Sample App'←[0m
My current version of Capybara is 2.1.0 but I tried changing version to 1.1.2 and bundle update, but didn't work aswell.
Also tried different syntax for the test.
page.should have_title("Sample App")
page.should have_selector('h1', :text => 'Sample App')
page.should have_selector('title', text: 'Sample App')
My system is Windows 7, and my rails version is 4.0.8 and Ruby version is 1.9.3.
Tried searching around for the "rack-test requires a rack application, but none was given" error, and it seems I somehow need to change so that rack is not the default driver.
Well this is how my config file looks like.
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'capybara/rspec'
RSpec.configure do |config|
config.include Capybara::DSL
end

undefined method `visit` rspec/capybara

I am following Michael Hartls tutorial on rspec and I am getting this error
bundle exec rspec spec/requests/static_pages_spec.rb
F
Failures:
1) Static pages Home page should have the content 'Sample App'
Failure/Error: visit '/static_pages/home'
NoMethodError:
undefined method `visit' for #<RSpec::ExampleGroups::StaticPages::HomePage:0x007fdddbdf6a90>
# ./spec/requests/static_pages_spec.rb:6:in `block (3 levels) in <top (required)>'
Finished in 0.00052 seconds (files took 0.1562 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/requests/static_pages_spec.rb:5 # Static pages Home page should have the content 'Sample App'
for the rspec test.
require 'spec_helper'
describe "Static pages" do
describe "Home page" do
it "should have the content 'Sample App'" do
visit '/static_pages/home'
page.should have_content('Sample App')
end
end
end
I don't believe that the reason I am getting this error is the test case is failing but rather this:
NoMethodError:
undefined method `visit' for #<RSpec:....
I am not sure why there is an undefined method visit
maybe you just missed:
config.include Capybara::DSL
also you could look to the same qestion here

RSpec Failure: Page should have title

I'm trying to run very simple RSpec tests following the Rails Tutorial and they surprisingly fail when they're not supposed to.
Fiona somewhere else suggested to move the file application.html.erb in app/views/layouts but mine is already there.
Zetetic suggested to add "render views" but I did and nothing changed.
The versions of the sw i'm using are as follows:
rvm 1.25.26
rspec 3.0.1
ruby 1.9.3p547
Rails 4.1.1
I get the following failure message:
$>rspec spec/requests/pages_spec.rb
F
Failures:
1) Pages Home page should have the h1 'Sample App'
Failure/Error: page.should have_selector('h1', :text => 'Sample App')
expected #has_selector?("h1", {:text=>"Sample App"}) to return true, got false
# ./spec/requests/pages_spec.rb:9:in `block (3 levels) in <top (required)>'
Deprecation Warnings:
Requiring `rspec/autorun` when running RSpec via the `rspec` command is deprecated. Called from /home/iammyr/.rvm/gems/ruby-1.9.3-p547/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in `require'.
Using `should` from rspec-expectations' old `:should` syntax without explicitly enabling the syntax is deprecated. Use the new `:expect` syntax or explicitly enable `:should` instead. Called from /home/iammyr/railsgirls-app/projects/galway_june2014/spec/requests/pages_spec.rb:9:in `block (3 levels) in <top (required)>'.
Failed examples:
rspec ./spec/requests/pages_spec.rb:7 # Pages Home page should have the h1 'Sample App'
One difference from the tutorial is that rather than generating those static pages with "rails generate static_pages" I did run "rails generate controller pages home help" but that shouldn't have to do with the rspec result, imho.
The files look as follow.
pages_spec.rb
require 'spec_helper'
describe "Pages" do
render_views
describe "Home page" do
it "should have the h1 'Sample App'" do
visit '/pages/home'
page.should have_selector('h1', :text => 'Sample App')
end
end
end
home.html.erb
<% provide(:title, 'Home') %>
<h1>Sample App</h1>
<p>
This is the home page.
</p>
Thanks a million to whomever would like to help me! thank you! ;)
You should first move the spec from requests to features folder see changes in capybara
I guess it will fix it (remember having a similar one)
If not debug it with pry and in Firefox with selenium driver
#Gemfile
gem 'pry-rails'
#spec
it "should have the h1 'Sample App'", :js => true do
visit '/pages/home'
binding.pry
page.should have_selector('h1', :text => 'Sample App')
end
Firefox should start and render the page
Then in debug mode check the capybara selector
page.all(:css, 'h1')
Perhaps you didn't define capybara or another driver. Then you try content instead of text:
page.should have_selector('h1', :content => 'Sample App')

I have install rspec-rails gem ruby and rail when i run this error comming

I have install rspec-rails like this and create a file static_pages_spec.rb
gem list
272 rails g rspec:install
274 rails generate integrateion_test static_pages
after this i have write down this code in static_pages_soec,rb
require 'spec_helper'
describe "StaticPages" do
describe "Home page" do
it "should have the content 'sample app'" do
visit '/static_pages/home'
page.should have content('Sample App')
end
end
end
after that run this command but getting error
jaskaran#jaskaran-Vostro-1550:~/rails_project$ bundle exec rspec spec/requests/static pages spec.rb
/home/jaskaran/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.14.8/lib/rspec
/core/configuration.rb:896:in `load': cannot load such file -- /home/jaskaran/rails_project/pages (LoadError)
from /home/jaskaran/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `block in load_spec_files'
from /home/jaskaran/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `each'
from /home/jaskaran/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `load_spec_files'
from /home/jaskaran/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:22:in `run'
from /home/jaskaran/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:80:in `run'
from /home/jaskaran/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:17:in `block in autorun'
Looks like there is a typo in your code.
page.should have content('Sample App')
should be
page.should have_content('Sample App')
If you are ever in doubt with Mike Hartl's tutorial you can always check the complete version here
The command you're running is incorrect.
You made a file called static_pages_spec.rb (at least I hope you did - your question is riddled with typos) but your command is looking for static pages spec.rb
Fix the command by adding the underscores.

Rspec test should pass but fails

I have this test from michael hartl book:
require 'spec_helper'
describe "Static pages" do
let(:base_title) { "Ruby on Rails Tutorial Sample App" }
describe "Home page" do
it "should have the h1 'Sample App'" do
visit '/static_pages/home'
page.should have_selector('h1', :text => 'Sample App')
end
it "should have the title 'Home'" do
visit '/static_pages/home'
page.should have_selector('title', :text => "#{base_title} | Home")
end
end
end
And the view:
<% provide(:title, 'Home') %>
<h1>Sample App</h1>
<p>
This is the home page for the
Ruby on Rails Tutorial
sample application.
</p>
When I run the test it says:
....
Finished in 1.91 seconds
4 examples, 0 failures
Randomized with seed 42247
.F...
Failures:
1) Static pages Home page should have the title 'Home'
Failure/Error: page.should have_selector('title', :text => "#{base_title} | Home")
expected #has_selector?("title", {:text=>"Ruby on Rails Tutorial Sample App | Home"}) to return true, got false
# ./spec/requests/static_pages_spec.rb:16:in `block (3 levels) in <top (required)>'
Finished in 1.91 seconds
5 examples, 1 failure
Failed examples:
rspec ./spec/requests/static_pages_spec.rb:14 # Static pages Home page should have the title 'Home'
Randomized with seed 17491
But It should pass, because when I view the page in browser the title is: Ruby on Rails Tutorial Sample App | Sample App, which is correct!
Make sure you're using capybara 1.1.2 in your Gemfile. Starting from 2.0 capybara does not works for title testing (https://github.com/jnicklas/capybara/issues/844)
...
group :test do
gem 'capybara', '1.1.2'
end
For the time being, you should do what #dimuch suggests and make sure you specify the same Capybara version Michael Hartl uses in the tutorial (1.1.2).
If you want to upgrade to Capybara 2.0 in the future and keep your tests for titles, have a look at this StackOverflow answer for a guide to creating a RSpec matcher that will do what you're expecting.
Using capubara 2.0 you should use
page.should have_title("The title")
But in aint work if you dont add
<style type="text/css">head, head title { display: block }</style>
To your application.html
page.title # => "The title"
page.has_title?("The title") # => true
page.should have_title("The title")
I've been using the following and they have been posting green. I dropped have_selector and went with have_title.
it { should have_title( full_title('Sign up') ) }
-- and --
it { should have_title(user.name) }
This is with capybara 2.2.0.

Resources