简体   繁体   中英

Rails 5.1: Rspec can't see Capybara features

I have some feature specs in my project that look roughly like this:

# specs/features/canvas_integration.rb
require 'spec_helper'

feature 'Routes to default page' do
  let!(:mode) { create(:mode) }

  scenario 'as anyone' do
    visit root_path
    expect(page).to have_content('Your home')
  end
end

But running the specs results in nothing:

rspec spec/features
No examples found.

My spec_helper.rb looks like this:

# frozen_string_literal: true

require 'simplecov'
SimpleCov.start 'rails'
require File.expand_path('../../config/environment', __FILE__)
require 'codeclimate-test-reporter'
require 'rspec/rails'
require 'webmock/rspec'
require 'devise'
require 'codeship'
require 'capybara/rspec'
require 'capybara/rails'

WebMock.disable_net_connect!(allow_localhost: true)
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

RSpec.configure do |config|
  config.include Devise::Test::ControllerHelpers, type: :controller
  config.include Devise::Test::ControllerHelpers, type: :view
  config.include FactoryGirl::Syntax::Methods
  config.extend ControllerMacros, type: :controller
  config.include Macros

  config.color = true
  config.fixture_path = "#{::Rails.root}/spec/factories/fixtures"
  config.infer_spec_type_from_file_location!
  config.infer_base_class_for_anonymous_controllers = false
  config.order = 'random'
  config.warnings = false
  config.default_formatter = 'doc' if config.files_to_run.one?
  config.profile_examples = 10
  config.use_transactional_examples = false
  config.use_transactional_fixtures = false

  config.before(:suite) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:truncation)
  end

  # config.before(:each, type: :system) { driven_by :rack_test }

  config.mock_with :rspec do |mocks|
    mocks.verify_doubled_constant_names = true
  end

  config.expect_with :rspec do |c|
    c.syntax = :expect
  end
end

Why can't Rspec find my features?

Your files need to end in _spec.rb in order for RSpec to load them when specifying a directory. So rename to specs/features/canvas_integration_spec.rb

Also note that in Rails 5.1 database cleaner is probably not necessary (if you're using one of the usual databases) - so you should be able to remove/comment out that and re-enable transactional testing. Additionally use_transactional_examples is an alias of use_transactional_fixtures so specifying both doesn't make sense.

I think your test spec filename should be specs/features/canvas_integration_spec.rb instead of specs/features/canvas_integration.rb

Why?

The files read by the rspec-rails gem are those that end in _spec.rb

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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