黄瓜失败,错误未定义方法`访问'

时间:2014-11-26 12:25:50

标签: ruby-on-rails-4 cucumber capybara

正在运行

rake cucumber 

甚至通过未经测试的功能。 在跑步时

cucumber features/something.feature

抛出

undefined method `visit' for #<Object:0x00000001b13950> (NoMethodError)

我已经搜索了一些github问题,他们谈论它但没有解脱。 这个Running Capybara without rack produces errors when using url parameters很有帮助,但没有解决我的问题

更新 我确实从capybara readme

触及了以下内容
  

使用Capybara和黄瓜

     

cuc-rails gem内置了Capybara支持。如果你   没有使用Rails,手动加载水豚/黄瓜模块:

require 'capybara/cucumber' 
Capybara.app = MyRackApp

但是在哪个文件中包含以上内容? 我尝试将上面的内容添加到env.rb并收到此错误:

  

未初始化的常量ActionController(NameError)

现在评论之后,我仍然得到同样的错误。

这是gemfile:

source 'https://rubygems.org'

#add dependency
gem 'diff-lcs', ">= 1.2.0"
gem 'rspec-expectations', "~> 3.0.0"

#add cucumber
group :test do
  gem 'cucumber-rails', :require => false
  # database_cleaner is not required, but highly recommended
  #gem 'database_cleaner', "~> 1.2.0"
  gem 'database_cleaner'
end

#add rspec
group :development, :test do
  gem 'rspec-rails', '~> 3.0'
  gem "capybara"
  gem 'factory_girl_rails'
  gem 'watir-webdriver'
  gem 'selenium-webdriver', '2.35.0'
  gem 'rubyzip'
  gem 'zip-zip'
end

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.7'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.3'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer',  platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0',          group: :doc

# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring',        group: :development

这是spec / spec_helper.rb(截断)

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'capybara'
include Capybara::DSL # Adding this line solved the error
require 'rspec/rails'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
ActiveRecord::Migration.maintain_test_schema!
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"
  config.infer_spec_type_from_file_location!
  config.include Capybara::DSL
end

这是env.rb

require 'capybara'
require 'capybara/dsl'
require 'capybara/cucumber'
#require 'capybara/rails'
#require 'capybara/session'
ActionController::Base.allow_rescue = false
begin
  DatabaseCleaner.strategy = :transaction
rescue NameError
  raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
end
Cucumber::Rails::Database.javascript_strategy = :truncation

1 个答案:

答案 0 :(得分:0)

愿这个答案帮助一些失去的灵魂。

最后得到了答案:Cucumber headless xvfb ubuntu

对于任何想要无头浏览的人来说,这救了我:

env.rb中包含以下内容:

require 'capybara'
require 'capybara/cucumber'
require 'cucumber/rails'
require 'capybara/rails'
require 'capybara/dsl'

require 'selenium/webdriver'



$port = <port_number>

#Capybara.app_host = '<localhost>:<port>'
Capybara.configure do |config|
  config.run_server = true
  #Capybara.default_host = "<localhost>:<port>"
  config.default_driver = :selenium
  #config.app = "make sure this isn't nil"
  config.app_host = "<hostname>:#{$port.to_s}"
  config.server_port = $port
end

#To add chrome webdriver do the following in your machine
#chmod +x chromedriver
#sudo mv chromedriver /usr/local/share/
#sudo ln -s /usr/local/share/chromedriver /usr/local/bin/chromedriver
#sudo ln -s /usr/local/share/chromedriver /usr/bin/chromedriver
#Register chrome as default Capybara webdriver
Capybara.register_driver :firefox do |app|
  # optional
  client = Selenium::WebDriver::Remote::Http::Default.new
  # optional
  #client.timeout = 120
  Capybara::Selenium::Driver.new(app, :browser => :firefox, :http_client => client)
end
#set default js driver
Capybara.javascript_driver = :firefox

#Include headless
require_relative 'headless'

headless是一个相对的rb文件headless.rb:

if Capybara.current_driver == :selenium || Capybara.default_driver == :selenium
  require 'headless'

  headless = Headless.new
  headless.start

  at_exit do
    headless.destroy
  end
end

env.rb和headless.rb都在feature / support文件夹中

我可以进行bdd和网络测试。