Module: RSpecSubjectExtensions::ClassMethods

Defined in:
lib/rspec-subject-extensions/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#each(attribute = nil) { ... } ⇒ Object

Creates a nested example group named by each and the submitted attribute, or the instances themselves if none given, and then generates an example for each of them using the submitted block.

Examples:

Using the singular name of the attributes

# This ...
describe Object do
  each(:item) { should be_an(Integer) }
end

# ... generates the same runtime structure as this:
describe Object do
  describe "each item" do
    it "should be an Interger" do
      subject.items.each do |item|
        item.should be_an(Integer)
      end
    end
  end
end

Using no attribute. Ideal to test scopes

# This ...
describe Object do
  subject { Object.visible }
  each { should be_visible }
end

# ... generates the same runtime structure as this:
describe Object do
  describe "each instance" do
    it "should be visible" do
      subject.each do |element|
        element.should be_visible
      end
    end
  end
end

Parameters:

  • optional (Symbol, String)

    attribute The singular name of the subject method containing all the attributes.

Yields:

  • Example to run against each attribute (or instance if no attribute given).

Raises:

  • (NoMethodError)

    The subject doesn’t respond to the pluralized version of the attribute or it doesn’t respond to each.



52
53
54
55
56
57
58
# File 'lib/rspec-subject-extensions/class_methods.rb', line 52

def each(attribute = nil, &block)
  if attribute
    each_with_attribute(attribute, &block)
  else
    each_without_attribute(&block)
  end
end