繁体   English   中英

使用capybara和rspec时没有方法错误

[英]No method error when using capybara and rspec

我真的很喜欢capybara和rpsec,我可以用它们编写一些出色的集成测试。 但是,我对某些事情感到困惑。 我在这里可能是错的,但是看来,一旦我安装了capybara,并且我在spec/features有了我的规格,capybara方法(例如visit就可以在spec/features (例如spec/features/controllers/statuses_spec.rb ), 这些规范现在无法使用rspec方法,例如route_to

这是什么意思? 水豚是否提供弥补我现在不可用的rspec方法的方法? 例如, visit ,而不是get

这似乎不是很直观。 希望我在设置方面做错了:

宝石文件:

source 'https://rubygems.org'

group :development do
    gem 'capistrano'
    gem 'guard-rspec'
    gem 'rb-fsevent'
    gem 'debugger'
end

group :development, :test do
    gem 'rspec-rails', '~> 2.14.0'
    gem 'sqlite3'
end

group :test do
    gem 'factory_girl_rails'
    gem 'capybara', '~> 2.2.0'
#   gem "capybara-webkit"
    gem 'launchy'
    gem 'database_cleaner'
end

group :production do
    gem 'pg'
end

gem 'rails', '4.0.1'
gem 'sass-rails', '~> 4.0.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 1.2'

group :doc do
  gem 'sdoc', require: false
end

gem 'devise'
# Use puma as the app server
# gem 'puma'

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'
require 'capybara/rspec'

# 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 }

# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

RSpec.configure do |config|
  # ## Mock Framework
  #
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
  #
  # config.mock_with :mocha
  # config.mock_with :flexmock
  # config.mock_with :rr
  config.before(:suite) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each) do
    Capybara.run_server = true
    Capybara.javascript_driver = :webkit
    Capybara.default_selector = :css
    Capybara.server_port = 7171
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  config.include RSpec::Rails::RequestExampleGroup, type: :feature

  # 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

  # Run specs in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by providing
  # the seed, which is printed after each run.
  #     --seed 1234
  config.order = "random"
end

规格/ 功能 /controllers/statuses_spec.rb

require 'spec_helper'

describe StatusesController do
    describe "routing" do
    # contains only capybara methods and so it passes
    it "contains welcome message" do
      visit("/statuses")
      page.should have_content("All of our statuses ")
    end

    # contains rspec methods and so I recieve a no method failure
    it "responds with 200" do
      get("/statuses").should respond_with 200
    end

  end
end

输出:

13:17:44 - INFO - Running: spec/features/controllers/statuses_spec.rb
.F

Failures:

  1) StatusesController routing responds with 200
     Failure/Error: get("/statuses").should respond_with 200
     NoMethodError:
       undefined method `respond_with' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x00000101787560>
     # ./spec/features/controllers/statuses_spec.rb:12:in `block (3 levels) in <top (required)>'

Finished in 0.40626 seconds
2 examples, 1 failure

我敢打赌,这与将这些规范放置在“功能”目录中有关,但我需要将它们放置在“功能”目录中以利用水豚方法。

因此,我需要将所有使用水豚方法的规范放在“功能”中,而将所有使用rspec方法的规范放在“规范”中? 希望不是。 我应该如何设置我的文件?

这里有完整的解释

使用RSpec请求规范以HTTP API的形式测试与您的应用程序的交互。 要做到这一点,请使用类似的方法getpostputdelete并断言反对response

使用RSpec功能规范(与capybara一起)来测试您的应用程序,因为用户可能会与它交互。 为此,请使用诸如visit和对page断言之类的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM