ruby on rails - rails - método indefinido `visitar ''cuando se usa RSpec y Capybara en rieles
rspec rails (5)
No puedo hacer que el carpincho trabaje con rspec. Me da este error:
undefined method `visit'' for #<RSpec::Core::ExampleGroup::Nested_1:0x16529f8 @example=nil>
Sé que hay muchas publicaciones al respecto, pero ninguna de las soluciones me funciona. La mayoría de ellos implican que las especificaciones no están en / spec / features, que es la mía.
Primero el error:
$bundle exec rspec spec
F
Failures:
1) security signs users in
Failure/Error: visit "/sessions/new"
NoMethodError:
undefined method `visit'' for #<RSpec::Core::ExampleGroup::Nested_1:0x16529f8 @example=nil>
# ./spec/features/security_spec.rb:4:in `(root)''
Finished in 0.006 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/features/security_spec.rb:3 # security signs users in
Creo que es importante tener en cuenta que al principio estaba usando el URL Helper ''new_sessions_path'' y me seguía dando un error de undefined local variable or method ''new_sessions_path''
. Sé que es válido porque:
$ rake routes
logout_sessions GET /sessions/logout(.:format) sessions#logout
sessions POST /sessions(.:format) sessions#create
new_sessions GET /sessions/new(.:format) sessions#new
contracts POST /contracts(.:format) contracts#create
new_contracts GET /contracts/new(.:format) contracts#new
edit_contracts GET /contracts/edit(.:format) contracts#edit
GET /contracts(.:format) contracts#show
PUT /contracts(.:format) contracts#update
DELETE /contracts(.:format) contracts#destroy
root / contracts#index
Mi Gemfile:
source ''https://rubygems.org''
gem ''rails'', ''3.2.11''
gem ''execjs''
group :assets do
gem ''sass-rails'', ''~> 3.2.3''
gem ''coffee-rails'', ''~> 3.2.1''
gem ''uglifier'', ''>= 1.0.3''
end
gem ''jquery-rails''
gem ''activerecord-oracle_enhanced-adapter'', ''~> 1.4.1''
gem ''jruby-openssl''
gem ''therubyrhino''
gem ''kaminari''
gem ''nokogiri''
group :development do
gem ''warbler''
end
group :test do
gem ''rspec-rails''
gem ''capybara''
gem ''activerecord-jdbcsqlite3-adapter''
end
spec_helper.rb dentro de my_app / spec:
# This file is copied to spec/ when you run ''rails generate rspec:install''
ENV["RAILS_ENV"] ||= ''test''
require File.expand_path("../../config/environment", __FILE__)
require ''rspec/rails''
require ''rspec/autorun''
# Capybara integration
require ''capybara/rspec''
require ''capybara/rails''
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
# Remove this line if you''re not using ActiveRecord or ActiveRecord fixtures
# config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
# Include path helpers
config.include Rails.application.routes.url_helpers
end
my_app / spec / features / security_spec.rb:
describe "security", :type => :feature do
it "signs users in" do
visit "/sessions/new"
fill_in "username", :with => "user"
fill_in "password", :with => "pass"
click_button "Sign In"
page.should have_content(''Login Successful'')
end
end
Intenté definir la prueba anterior tanto con como sin :type => :feature
. No importa de ninguna manera. ¿Alguna idea de lo que debería probar ahora?
Agregar require ''rails_helper''
en la parte superior de mi función terminó arreglando mi problema:
require ''rails_helper''
describe "security", :type => :feature do
it "signs users in" do
visit new_sessions_path
fill_in "username", :with => "user"
fill_in "password", :with => "pass"
click_button "Sign In"
page.should have_content(''Login Successful'')
end
end
Esto me parece extraño ya que cada ejemplo que he visto para rspec y capibara no requería eso, pero bueno. Problema resuelto.
Respuesta original (versiones anteriores de rspec)
require ''spec_helper''
sea utilizado por versiones anteriores de RSpec. La mejor respuesta sería require ''rails_helper''
.
Desde Capybara 2.0, uno tiene que usar las características / características de la carpeta. Los comandos de Capybara ya no funcionan en las especificaciones / solicitudes de la carpeta.
Intenta agregar:
config.include Capybara::DSL
a tu bloque de configuración
# This file is copied to spec/ when you run ''rails generate rspec:install''
ENV["RAILS_ENV"] ||= ''test''
require File.expand_path("../../config/environment", __FILE__)
require ''rspec/rails''
require ''rspec/autorun''
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
# Remove this line if you''re not using ActiveRecord or ActiveRecord fixtures
# config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
# Include path helpers
config.include Rails.application.routes.url_helpers
config.include Capybara::DSL
end
Intenta realizar toda tu configuración en un bloque before
:
spec / features / security_spec.rb
describe "security" do
before do
visit "/sessions/new"
fill_in "username", :with => "user"
fill_in "password", :with => "pass"
click_button "Sign In"
end
it "signs users in" do
page.should have_content(''Login Successful'')
end
end
Yo también tuve este problema,
Agregar requerir ''rails_helper'' en la parte superior de mi función terminó arreglando mi problema:
require ''rails_helper''
RSpec.describe "Products", type: :request do
describe "GET /products" do
it "display tasks" do
Product.create!(:name => "samsung")
visit products_path
page.should have_content("samsung")
#expect(response).to have_http_status(200)
end
end
end
Y agrega el ''config.include Capybara :: DSL'' en rails_helper.rb
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
config.include Capybara::DSL
end