Allows you to use block_is_expected similarly to how you would use is_expected if a block was wrapping the subject.

Total Downloads Downloads Today Test Coverage Maintainability Network Stars Version Build Documentation Depfu Open Source Helpers Chat License: MIT

This gem does one very simple thing very well. It allows you to use block_is_expected similarly to how you would use is_expected if a block was wrapping the subject. Supports the same versions of Ruby that RSpec does, 1.8.7 - current ruby-head, as well as the JRuby equivalents.

subject { Integer(nil) }
it('raises') { block_is_expected.to raise_error(TypeError) }

If you only ever want to test subjects wrapped in blocks, and are comfortable with losing the standard is_expected behavior, see an alternative to this gem here.

Installation

Add this line to your application’s Gemfile:

gem 'rspec-block_is_expected', group: :test

And then execute:

$ bundle

Or install it yourself as:

$ gem install rspec-block_is_expected

Configuration

There is no configuration needed if you your test suite loads the bundle group (e.g. test) that you added the gem to.

Otherwise, you may load it manually near the top of your spec_helper.rb, and it will self configure.

require 'rspec/block_is_expected'

Usage

The spec suite for this gem has some examples of usage, lightly edited here.

RSpec.describe 'TestyMcTest' do
  context 'errors raised' do
    subject { Integer(nil) }
    it('can be tested') do
      # Where you used to have:
      # expect { subject }.to raise_error(TypeError)
      block_is_expected.to raise_error(TypeError)
    end
  end
  context 'execution' do
    let(:mutex) { Mutex.new }
    subject { mutex.lock }
    it('can change state') do
      expect(mutex.locked?).to eq(false)
      # Where you used to have:
      # expect { subject }.to_not raise_error
      block_is_expected.to_not raise_error
      expect(mutex.locked?).to eq(true)
    end
  end
  context 'changed state' do
    let(:mutex) { Mutex.new }
    subject { mutex.lock }
    it('can be tested') do
      # Where you used to have:
      # expect { subject }.to change { mutex.locked? }.from(false).to(true)
      block_is_expected.to change { mutex.locked? }.from(false).to(true)
    end
  end
end