Module: RSpec::Core::MemoizedHelpers

### Home Classes Methods

### In Files

- lib/rspec/core/memoized_helpers.rb

### Namespace

- MODULE RSpec::Core::MemoizedHelpers::ClassMethods
- CLASS RSpec::Core::MemoizedHelpers::ContextHookMemoizedHash

### Methods

- #is_expected
- #should
- #should_not
- #subject

### Included Modules

- Module

### Class/Module Index

Quicksearch

- RSpec
- RSpec::Core
- RSpec::Core::AnonymousExampleGroup
- RSpec::Core::Backports
- RSpec::Core::Backports::Random
- RSpec::Core::Backports::Random::Implementation
- RSpec::Core::Backports::Random::MT19937
- RSpec::Core::BacktraceFormatter
- RSpec::Core::Configuration
- RSpec::Core::Configuration::DeprecationReporterBuffer
- RSpec::Core::Configuration::ExposeCurrentExample
- RSpec::Core::Configuration::MustBeConfiguredBeforeExampleGroupsError
- RSpec::Core::ConfigurationOptions
- RSpec::Core::DRbOptions
- RSpec::Core::DRbRunner
- RSpec::Core::DSL
- RSpec::Core::DSL::RSpec
- RSpec::Core::Example
- RSpec::Core::Example::ExecutionResult
- RSpec::Core::Example::Procsy
- RSpec::Core::ExampleGroup
- RSpec::Core::ExclusionRules
- RSpec::Core::FilterManager
- RSpec::Core::FilterRules
- RSpec::Core::FilterableItemRepository
- RSpec::Core::FilterableItemRepository::QueryOptimized
- RSpec::Core::FilterableItemRepository::UpdateOptimized
- RSpec::Core::FlatMap
- RSpec::Core::Formatters
- RSpec::Core::Formatters::BaseFormatter
- RSpec::Core::Formatters::BaseTextFormatter
- RSpec::Core::Formatters::ConsoleCodes
- RSpec::Core::Formatters::DeprecationFormatter
- RSpec::Core::Formatters::DeprecationFormatter::DelayedPrinter
- RSpec::Core::Formatters::DeprecationFormatter::FileStream
- RSpec::Core::Formatters::DeprecationFormatter::ImmediatePrinter
- RSpec::Core::Formatters::DeprecationFormatter::RaiseErrorStream
- RSpec::Core::Formatters::DocumentationFormatter
- RSpec::Core::Formatters::Helpers
- RSpec::Core::Formatters::HtmlFormatter
- RSpec::Core::Formatters::HtmlPrinter
- RSpec::Core::Formatters::JsonFormatter
- RSpec::Core::Formatters::Loader
- RSpec::Core::Formatters::ProfileFormatter
- RSpec::Core::Formatters::ProgressFormatter
- RSpec::Core::Formatters::Protocol
- RSpec::Core::Formatters::SnippetExtractor
- RSpec::Core::Formatters::SnippetExtractor::CoderayConverter
- RSpec::Core::Formatters::SnippetExtractor::NullConverter
- RSpec::Core::HashImitatable
- RSpec::Core::HashImitatable::ClassMethods
- RSpec::Core::Hooks
- RSpec::Core::Hooks::AfterContextHook
- RSpec::Core::Hooks::AfterHook
- RSpec::Core::Hooks::AroundHook
- RSpec::Core::Hooks::BeforeHook
- RSpec::Core::Hooks::Hook
- RSpec::Core::Hooks::HookCollections
- RSpec::Core::InclusionRules
- RSpec::Core::LegacyExampleGroupHash
- RSpec::Core::MemoizedHelpers
- RSpec::Core::MemoizedHelpers::ClassMethods
- RSpec::Core::MemoizedHelpers::ContextHookMemoizedHash
- RSpec::Core::MemoizedHelpers::ContextHookMemoizedHash::After
- RSpec::Core::MemoizedHelpers::ContextHookMemoizedHash::Before
- RSpec::Core::Metadata
- RSpec::Core::Metadata::ExampleGroupHash
- RSpec::Core::Metadata::ExampleHash
- RSpec::Core::Metadata::HashPopulator
- RSpec::Core::MetadataFilter
- RSpec::Core::MinitestAssertionsAdapter
- RSpec::Core::MockingAdapters
- RSpec::Core::MockingAdapters::Flexmock
- RSpec::Core::MockingAdapters::Mocha
- RSpec::Core::MockingAdapters::Null
- RSpec::Core::MockingAdapters::RR
- RSpec::Core::MockingAdapters::RSpec
- RSpec::Core::Notifications
- RSpec::Core::Notifications::DeprecationNotification
- RSpec::Core::Notifications::ExampleNotification
- RSpec::Core::Notifications::ExamplesNotification
- RSpec::Core::Notifications::FailedExampleNotification
- RSpec::Core::Notifications::NullColorizer
- RSpec::Core::Notifications::NullNotification
- RSpec::Core::Notifications::PendingExampleFailedAsExpectedNotification
- RSpec::Core::Notifications::PendingExampleFixedNotification
- RSpec::Core::Notifications::PendingExampleNotificationMethods
- RSpec::Core::Notifications::ProfileNotification
- RSpec::Core::Notifications::SeedNotification
- RSpec::Core::Notifications::SkippedExampleNotification
- RSpec::Core::Notifications::SummaryNotification
- RSpec::Core::NullReporter
- RSpec::Core::Ordering
- RSpec::Core::Ordering::ConfigurationManager
- RSpec::Core::Ordering::Custom
- RSpec::Core::Ordering::Identity
- RSpec::Core::Ordering::Random
- RSpec::Core::Ordering::Registry
- RSpec::Core::Parser
- RSpec::Core::Pending
- RSpec::Core::Pending::PendingExampleFixedError
- RSpec::Core::Pending::SkipDeclaredInExample
- RSpec::Core::ProjectInitializer
- RSpec::Core::RakeTask
- RSpec::Core::RandomNumberGenerator
- RSpec::Core::Reporter
- RSpec::Core::RubyProject
- RSpec::Core::Runner
- RSpec::Core::Sandbox
- RSpec::Core::SharedContext
- RSpec::Core::SharedExampleGroup
- RSpec::Core::SharedExampleGroup::Registry
- RSpec::Core::SharedExampleGroup::TopLevelDSL
- RSpec::Core::SharedExampleGroupInclusionStackFrame
- RSpec::Core::SharedExampleGroupModule
- RSpec::Core::SuiteHookContext
- RSpec::Core::TestUnitAssertionsAdapter
- RSpec::Core::Time
- RSpec::Core::Version
- RSpec::Core::Warnings
- RSpec::Core::World
- RSpec::ExampleGroups
- Object

No matching classes.

# RSpec::Core::MemoizedHelpers

This module is included in {ExampleGroup}, making the methods available to be called from within example blocks.

@see ClassMethods

### Public Instance Methods

is_expected() click to toggle source

Wraps the `subject` in `expect` to make it the target of an expectation. Designed to read nicely for one-liners.

@example

    describe [1, 2, 3] do
      it { is_expected.to be_an Array }
      it { is_expected.not_to include 4 }
    end

@see subject @see should @see should_not

@note This only works if you are using rspec-expectations.

    # File lib/rspec/core/memoized_helpers.rb, line 118
    def is_expected
      expect(subject)
    end

should(matcher=nil, message=nil) click to toggle source

When `should` is called with no explicit receiver, the call is delegated to the object returned by `subject`. Combined with an implicit subject this supports very concise expressions.

@example

    describe Person do
      it { should be_eligible_to_vote }
    end

@see subject @see is_expected

@note This only works if you are using rspec-expectations. @note If you are using RSpec's newer expect-based syntax you may

    want to use `is_expected.to` instead of `should`.

    # File lib/rspec/core/memoized_helpers.rb, line 80
    def should(matcher=nil, message=nil)
      RSpec::Expectations::PositiveExpectationHandler.handle_matcher(subject, matcher, message)
    end

should_not(matcher=nil, message=nil) click to toggle source

Just like `should`, `should_not` delegates to the subject (implicit or explicit) of the example group.

@example

    describe Person do
      it { should_not be_eligible_to_vote }
    end

@see subject @see is_expected

@note This only works if you are using rspec-expectations. @note If you are using RSpec's newer expect-based syntax you may

    want to use `is_expected.to_not` instead of `should_not`.

    # File lib/rspec/core/memoized_helpers.rb, line 99
    def should_not(matcher=nil, message=nil)
      RSpec::Expectations::NegativeExpectationHandler.handle_matcher(subject, matcher, message)
    end

subject() click to toggle source

@note `subject` was contributed by Joe Ferris to support the one-liner

    syntax embraced by shoulda matchers:

        describe Widget do
          it { is_expected.to validate_presence_of(:name) }
          # or
          it { should validate_presence_of(:name) }
        end

    While the examples below demonstrate how to use `subject`
    explicitly in examples, we recommend that you define a method with
    an intention revealing name instead.

@example

    # Explicit declaration of subject.
    describe Person do
      subject { Person.new(:birthdate => 19.years.ago) }
      it "should be eligible to vote" do
        subject.should be_eligible_to_vote
        # ^ ^ explicit reference to subject not recommended
      end
    end

    # Implicit subject => { Person.new }.
    describe Person do
      it "should be eligible to vote" do
        subject.should be_eligible_to_vote
        # ^ ^ explicit reference to subject not recommended
      end
    end

    # One-liner syntax - expectation is set on the subject.
    describe Person do
      it { is_expected.to be_eligible_to_vote }
      # or
      it { should be_eligible_to_vote }
    end

@note Because `subject` is designed to create state that is reset

    between each example, and `before(:context)` is designed to setup
    state that is shared across _all_ examples in an example group,
    `subject` is _not_ intended to be used in a `before(:context)` hook.

@see should @see should_not @see is_expected

    # File lib/rspec/core/memoized_helpers.rb, line 55
    def subject
      __memoized.fetch(:subject) do
        __memoized[:subject] = begin
          described = described_class || self.class.metadata.fetch(:description_args).first
          Class === described ? described.new : described
        end
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.