Contents

each element of subject

Use the each() method as a short-hand to generate a nested example group with a single example that specifies the expected value of each element of the subject. This can be used with an implicit or explicit subject.

each() requires a block representing the example.

each { is_expected.to be_visible }

Scenario: specify value of each element

  • Given a file named "example_spec.rb" with:
    ruby
    require "rspec-subject-extensions"
    
    class Post
      def visible?
        true
      end
    end
    
    describe Post do
      subject { [Post.new, Post.new] }
      each { is_expected.to be_visible }
    end
  • When I run `rspec example_spec.rb --format documentation`
  • Then the output should contain:
    text
    Post
      each instance
        should be visible

Source: features/class_methods/each_element_of_subject.feature at commit f0406474, Rspec Subject Extensions (MIT license).