gpt4 book ai didi

ruby-on-rails - 在 rails 中使用 RSpec 和 Capybara 时未定义的方法 `visit'

转载 作者:数据小太阳 更新时间:2023-10-29 06:18:36 25 4
gpt4 key购买 nike

我无法让 capybara 使用 rspec。它给了我这个错误:

undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1:0x16529f8 @example=nil>

我知道有很多关于此的帖子,但没有一个解决方案适合我。其中大部分涉及不在/spec/features 中的规范 - 我的在其中。

首先是错误:

$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

我认为重要的是要注意,起初我使用的是 URL Helper“new_sessions_path”,它一直给我一个错误 undefined local variable or method 'new_sessions_path'。我知道它是有效的,因为:

$ 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

我的 gem 文件:

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

my_app/spec 中的 spec_helper.rb:

# 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

我已经尝试在使用和不使用 :type => :feature 的情况下定义上面的测试。这两种方式都没有区别。有什么想法我接下来应该尝试什么吗?

最佳答案

尝试添加:

  config.include Capybara::DSL

到你的配置 block 。

# 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

关于ruby-on-rails - 在 rails 中使用 RSpec 和 Capybara 时未定义的方法 `visit',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15148585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com