Method: RSpec::Core::Configuration#include

Defined in:
lib/rspec/core/configuration.rb

#include(mod, *filters) ⇒ void

Note:

Filtered module inclusions can also be applied to individual examples that have matching metadata. Just like Ruby's object model is that every object has a singleton class which has only a single instance, RSpec's model is that every example has a singleton example group containing just the one example.

Tells RSpec to include mod in example groups. Methods defined in mod are exposed to examples (not example groups). Use filters to constrain the groups or examples in which to include the module.

Examples:


module AuthenticationHelpers
  def (user)
    # ...
  end
end

module PreferencesHelpers
  def preferences(user, preferences = {})
    # ...
  end
end

module UserHelpers
  def users(username)
    # ...
  end
end

RSpec.configure do |config|
  config.include(UserHelpers) # included in all groups

  # included in examples with `:preferences` metadata
  config.include(PreferenceHelpers, :preferences)

  # included in examples with `:type => :request` metadata
  config.include(AuthenticationHelpers, :type => :request)

  # included in examples where the `:type` metadata matches a proc condition
  config.include(AuthenticationHelpers, :type => proc { |type, | [:request, :controller].include?(type) })
end

describe "edit profile", :preferences, :type => :request do
  it "can be viewed by owning user" do
     preferences(users(:jdoe), :lang => 'es')
    get "/profiles/jdoe"
    assert_select ".username", :text => 'jdoe'
  end
end

See Also:



1435
1436
1437
1438
1439
# File 'lib/rspec/core/configuration.rb', line 1435

def include(mod, *filters)
  define_mixed_in_module(mod, filters, @include_modules, :include) do |group|
    safe_include(mod, group)
  end
end