我的规范支持文件未加载

时间:2017-05-23 20:15:58

标签: ruby-on-rails rspec capybara

 undefined method `login_as' for #<RSpec::ExampleGroups::InvitingUsers:0x007feea1d19f88>

我在spec / support / macro.rb中的方法或支持下的任何其他文件似乎都没有加载,因为我的功能规范中没有这些方法。

这是我的spec_helper.rb文件

    require 'factory_girl_rails'
RSpec.configure do |config|
  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end
  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end
  config.shared_context_metadata_behavior = :apply_to_host_groups
end

这是我的rails_helper.rb文件

     ENV["RAILS_ENV"] ||= "test"
require File.expand_path("../../config/environment", __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require "spec_helper"
require "rspec/rails"
require "capybara/rspec"
require 'shoulda/matchers'

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end

# Add additional requires below this line. Rails is not loaded until this point!
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

ActiveRecord::Migration.maintain_test_schema!
Capybara.register_driver :selenium_chrome do |app|
 Capybara::Selenium::Driver.new(app, browser: :chrome)
end
Capybara.javascript_driver = :selenium_chrome
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 = false
  config.infer_spec_type_from_file_location!
  config.filter_rails_from_backtrace!
  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
  end
  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end
  config.before(:each, js: true) do
    DatabaseCleaner.strategy = :truncation
  end
  config.include FactoryGirl::Syntax::Methods
  # This block must be here, do not combine with the other `before(:each)` block.
  # This makes it so Capybara can see the database.
  config.before(:each) do
    DatabaseCleaner.start
  end

  config.around(:each) do |example| 
    DatabaseCleaner.cleaning do
      example.run
      Apartment::Tenant.reset
    end

    connection = ActiveRecord::Base.connection.raw_connection
    schemas = connection.query(%Q{
      SELECT 'drop schema "' || nspname || '" cascade;'
      from pg_namespace
      where nspname != 'public'
      AND nspname NOT LIKE 'pg_%'
      AND nspname != 'information_schema';
    })
     schemas.each do |query| 
          connection.query(query.values.first)
      end 
  end
  config.after(:each) do 
    Capybara.app_host = "http://lvh.me"
  end
end

这是我的宝石文件

 `source 'https://rubygems.org'

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
  "https://github.com/#{repo_name}.git"
end


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.2'
# Use postgresql as the database for Active Record
gem 'pg', '~> 0.18'
# Use Puma as the app server
gem 'puma', '~> 3.0'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
gem 'devise'
gem 'apartment'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
gem 'simple_form'

gem 'bootstrap-sass', '~> 3.3.6'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platform: :mri
  gem 'guard-rspec', require: false
  gem 'spring-commands-rspec'
  gem 'terminal-notifier-guard', '~> 1.6.1'

  gem "database_cleaner"
  gem "rspec-rails" ,'~> 3.5.2'
  gem 'factory_girl_rails', '~> 4.5'

  gem "capybara"
  gem "selenium-webdriver"
  gem 'shoulda-matchers', '~> 3.0', require: false
  gem 'faker', '~> 1.6.1'
end



group :development do
  # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '~> 3.0.5'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
`

这是我在spec / support / subdomain_helper.rb下保存的subdomain_helper.rb

module SubdomainHelpers
    def set_subdomain(subdomain)
        site = "#{subdomain}.lvh.me"
        Capybara.app_host = "http://#{site}" 
        Capybara.always_include_port = true

        default_url_options[:host] = "#{site}" 
    end
end

RSpec.configure do |c|
    c.include SubdomainHelpers, type: :feature

    c.before type: :feature do 
      Capybara.app_host = "http://lvh.me"
    end
end

1 个答案:

答案 0 :(得分:0)

问题是您尝试访问的方法包装在模块中。从模块中删除它们对我有用。

#module SubdomainHelpers
    def set_subdomain(subdomain)
        site = "#{subdomain}.lvh.me"
        Capybara.app_host = "http://#{site}" 
        Capybara.always_include_port = true

        default_url_options[:host] = "#{site}" 
    end
#end