Rspec + Capybara: controller missing, integration spec passes anyway - ruby-on-rails

I started a fresh new rails app (using rspec-rails 2.99 & capybara 2.3.0), and wrote a silly simple integration spec to get started with TDD:
# spec/integration/visit_home_page_spec.rb
require 'spec_helper'
feature "view the home page" do
scenario "user visits home page" do
visit root_path
expect(page).to have_content 'Password'
end
end
As expected, the test fails due to root_path being undefined - so I went ahead and:
# config/routes.rb
root 'users#new'
And here's the problem: instead of the spec failing due to UsersController being undefined, it's green. It appears that rspec is evaluating the error page itself, and since that page happens to contain the word "password", the spec passes.
To make sure that's what's happening, I have modified the spec to:
# spec/integration/visit_home_page_spec.rb
...
expect(page).to have_content 'theresnowaythisisinthepage'
...
Now the test fails, but not because it's picking up the undefined UsersController - just because the content is not in the error page.
I've started many an app with this approach, and it's the first time I've seen rspec mistaking an error page for a valid one - what did I break?
I suspect my forehead will have a big hand-shaped mark when I find the answer.
--
Here's my Gemfile
source 'https://rubygems.org'
gem 'rails', '~> 4.1.1'
gem 'mysql2', '~> 0.3.16'
gem 'sass-rails', '~> 4.0.2'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'active_model_serializers', '~> 0.8.1'
gem 'slim-rails', '~> 2.1.4'
gem 'bootstrap-sass', '~> 3.1.1.1'
gem 'bootstrap-generators', '~> 3.1.1.3'
group :test, :development do
gem 'rspec-rails', '~> 2.99'
gem 'capybara', '~> 2.3.0'
end
group :development do
gem "better_errors"
gem "binding_of_caller" # required by better-errors
gem "launchy"
end
group :doc do
gem 'sdoc', require: false
end
Here's spec_helper
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
config.infer_spec_type_from_file_location!
config.include Capybara::DSL
end

Related

undefined method `authenticate' on Devise + Rspec + render_view

I have Rails 4.2.5 and rspec 3.4.1
When I add render_views some first controllers test are failed.
I use render_views because I don't know any methods to watch what happens on a page.
Gemfile
source 'https://rubygems.org'
gem 'rails', '4.2.5'
gem 'sqlite3'
gem 'haml-rails', '~> 0.9'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'twitter-bootstrap-rails'
gem 'less-rails'
gem 'therubyracer', platforms: :ruby
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
group :development, :test do
gem 'rspec-rails', '~> 3.0'
gem 'cucumber-rails', :require => false
gem 'database_cleaner'
gem 'factory_girl_rails'
gem 'shoulda-matchers'
gem 'capybara'
gem 'capybara-screenshot'
gem 'byebug'
end
group :development do
gem 'web-console', '~> 2.0'
gem 'spring'
end
gem 'devise'
gem 'cancan'
gem "role_model"
gem 'paperclip', '~> 4.3'
gem 'jquery-fileupload-rails'
gem 'stringex'
gem 'will_paginate'
gem 'russian', '~> 0.6.0'
spec/controllers/users_controller_spec.rb
RSpec.describe UsersController, type: :controller do
render_views
let(:valid_attributes) {
{email: "admin#example.com",
password: "password",
password_confirmation: "password"}
}
let(:invalid_attributes) {
}
let(:valid_session) { {} }
describe "GET #index" do
it "says 'Users'" do
get :index
end
end
...
end
spec/rails_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'devise'
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.include Devise::TestHelpers, type: :controller
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
config.filter_rails_from_backtrace!
end
rspec spec/controllers/page_controller_spec.rb
PageController
GET #index
returns http success (FAILED - 1)
Failures:
1) PageController GET #index returns http success
Failure/Error: - if user_signed_in?
ActionView::Template::Error:
undefined method `authenticate' for nil:NilClass
# /home/ilp/.rvm/gems/ruby-2.1.4/gems/devise-3.5.3/lib/devise/controllers/helpers.rb:124:in `current_user'
# /home/ilp/.rvm/gems/ruby-2.1.4/gems/devise-3.5.3/lib/devise/controllers/helpers.rb:120:in `user_signed_in?'
# ./app/views/layouts/main.html.haml:36:in `_app_views_layouts_main_html_haml___2814915178728912122_59040000'
# ./spec/controllers/page_controller_spec.rb:11:in `block (3 levels) in <top (required)>'
# ------------------
# --- Caused by: ---
# NoMethodError:
# undefined method `authenticate' for nil:NilClass
# /home/ilp/.rvm/gems/ruby-2.1.4/gems/devise-3.5.3/lib/devise/controllers/helpers.rb:124:in `current_user'
Finished in 0.26407 seconds (files took 1.9 seconds to load)
1 example, 1 failure
app/controllers/page_controller.rb
class PageController < ApplicationController
def index
end
end
If I call sign_in or sign_our methods tests are success.
You need to call the sign_in method before rendering as it check the current_user object and calls the authenticate! method on current_user object which is nil so throwing error!
Do it like this
before do
sign_in_user
end
this will set the current_user object.

Im getting error : No DRb server is running. Running in local process instead

I'm following the ROR tutorials , Im testing with rspec spec/requests/static_pages_spec.rb , Error occurred " No DRb server is running. Running in local process instead"' I did some research , they said it's because Spork server is not running , so i added Spork.each_run do to the spc file, it dosen't help , I also modified the gem file from gem 'guard-spork' to gem 'guard-spork', :github => 'guard/guard-spork' Still the same, Anyone can help? Thanks in Advance!
Spec file:
require 'spec_helper'
Spork.each_run do
end
describe "Static pages" do
let(:base_title) { "Ruby on Rails Tutorial Sample App" }
describe "Home page" do
it "should have the content 'Sample App'" do
visit '/static_pages/home'
expect(page).to have_content('Sample App')
end
it "should have the base title" do
visit '/static_pages/home'
expect(page).to have_title("Ruby on Rails Tutorial Sample App")
end
it "should not have a custom page title" do
visit '/static_pages/home'
expect(page).not_to have_title('| Home')
end
describe "Contact page" do
it "should have the content 'Contact'" do
visit '/static_pages/contact'
expect(page).to have_content('Contact')
end
it "should have the title 'Contact'" do
visit '/static_pages/contact'
expect(page).to have_title("Ruby on Rails Tutorial Sample App | Contact")
end
end
end
end
Gemfile :
source 'https://rubygems.org'
ruby '2.0.0'
gem 'rails', '4.0.2'
gem 'bootstrap-sass', '2.3.2.0'
gem 'pg', '0.15.1'
group :development, :test do
gem 'guard-spork', :github => 'guard/guard-spork'
gem 'sprockets', '2.11.0'
gem 'rspec-rails', '2.13.1'
gem 'guard-rspec', '2.5.0'
gem 'spork-rails', '4.0.0'
gem 'childprocess', '0.3.6'
end
group :test do
gem 'selenium-webdriver', '2.35.1'
gem 'capybara', '2.1.0'
end
gem 'sass-rails', '4.0.1'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '2.2.1'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'
group :doc do
gem 'sdoc', '0.3.20', require: false
end
group :production do
gem 'rails_12factor', group: :production
end
# Use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.1.2'
# Use unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]
You should run spork in different process with command spork. Also, remove Spork part from test and modify you spec_helper file with something like this (move all in Spork.prefork block)
The other thing is that Spork had been replaced (not directly) by spring that does the same thing, but without additional configuration.

How do I get rid of this mixture of errors when I run bin/rspec?

I'm working on a Rails project and I am setting up RSpec with FactoryGirl, Faker, and DatabaseCleaner. This is my Gemfile so far:
source 'https://rubygems.org'
ruby '2.1.2'
gem 'rails', '4.1.1'
gem 'pg'
gem 'sass-rails', '~> 4.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0'
gem 'jquery-rails'
gem 'haml', '~> 4.0'
gem 'devise', '~> 3.2'
gem 'jbuilder', '~> 2.0'
group :development do
gem 'better_errors'
gem 'binding_of_caller'
gem 'lograge'
gem 'pry-rails'
end
group :test do
gem 'rspec-rails', '~> 3.0'
gem 'factory_girl_rails'
gem 'faker'
end
gem 'sdoc', '~> 0.4', group: :doc
gem 'spring', group: :development
gem 'rails_12factor', group: :production
This is my spec/rails_helper.rb:
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
end
Now, when I run the specs via the bin/rspec executable, I get this mixture of warnings and errors (I had to put it in a Gist because of the character limit on StackOverflow). Also, there seems to be some database warnings logged in my log/test.log file which is also in a Gist.
If you look right at the end, the one spec I've written (which creates a new Organisation factory) actually passes. When $VERBOSE = nil the warnings and errors do not appear and all I get is:
.
Finished in 0.35145 seconds (files took 1.61 seconds to load)
1 example, 0 failures
I can't seem to figure out what the warnings mean and I was wondering whether you know how to fix/get rid of them?

Ruby on rails: Undefined method 'let'

I am a newbie in Ruby on Rails, when I try method let in static_pages_specs.rb
require `spec_helper`
describe "Static pages" do
let(:base_title) { "Ruby on Rails Tutorial Sample App" }
describe "Home page" do
it "should have the title 'Home'" do
visit '/static_pages/home'
expect(page).to have_title("#{base_title} | Home")
end
end
end
I get an error
Failure/Error: visit '/static_pages/home'
ActionView::Template::Error:
undefined method `let' for Class:0x000000034b2230>:0x0000000374a6c8>
My Gemfile look like this
source 'https://rubygems.org'
ruby '1.9.3'
gem 'rails', '4.0.4'
group :development, :test do
gem 'sqlite3'
gem 'rspec-rails'
end
group :test do
gem 'selenium-webdriver'
gem 'capybara'
end
gem 'sass-rails', '~> 4.0.2'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 1.2'
group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', require: false
end
group :production do
gem 'pg', '0.15.1'
gem 'rails_12factor', '0.0.2'
end
Also my spec_helper.rb look like this
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
config.include Capybara::DSL
end
Do you have any ideas what am I missing? I am using Ubuntu 13.04.
Edit: Already tried let bang method but it still gets the same error.
In the previous post I forgot to put
require 'spec_helper'
in, but my code actually DID have it.
Sorry about that but it was not actually the problem there.
Solved: Thank you guys I've just solved it, it was just a silly typing in one of my templates.
Your static_pages_specs.rb is missing require 'spec_helper':
require `spec_helper`
describe "Static pages" do
...
end

undefined method `visit' for #<RSpec

i'm sure this is answered somewhere on stackoverflow but for the life of me, i can't find why my situation is different than what is already written so here goes.
using rspec w/ capybara
1) report_cards#index must have 'Report Cards Index'
Failure/Error: visit '/'
NoMethodError:
undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_4:0x007fcc3aa33960>
# ./spec/views/report_cards_view_spec.rb:7:in `block (2 levels) in <top (required)>'
/spec/views/report_cards_view_spec.rb looks like
require 'spec_helper'
describe "report_cards#index" do
it "must have 'Report Cards Index'" do
visit '/'
page.should have_content("something")
end
end
top lines from spec_helper.rb looks like
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rails'
require 'capybara/rspec'
gemfile looks like
`source 'https://rubygems.org'`
gem 'rails', '3.2.7'
gem 'jquery-rails'
gem 'pg', '~> 0.14.0'
gem 'devise', '~> 2.1.2'
gem "quiet_assets", "~> 1.0.1"
gem 'thin'
gem 'bourbon'
gem "haml-rails"
gem "httparty", "~> 0.8.3"
gem "activerecord-import", "~> 0.2.10"
group :test, :development do
gem "rspec-rails", "~> 2.0"
gem 'capybara', '~>1.1.2'
gem "fabrication", "~> 2.2.0"
gem "launchy", "~> 2.1.2"
end
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
end`
i'm pretty junior so go easy :) - THX !
capybara is not included in views spec, and it is for integration testing.
try to move your spec file into spec/requests directory
I had a similar issue and all i had to do was to add
config.include Capybara::DSL
to the spec_helper.rb file

Resources