Skip to content

Instantly share code, notes, and snippets.

@asaaki
Created March 2, 2018 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asaaki/baa6408fbd60f267e68c21f26d576c6f to your computer and use it in GitHub Desktop.
Save asaaki/baa6408fbd60f267e68c21f26d576c6f to your computer and use it in GitHub Desktop.
callables in specs
require 'spec_helper'
module CallableSubject
def callable_subject(&_block)
subject { -> { yield } }
end
end
RSpec.configure do |c|
c.extend(CallableSubject)
end
RSpec.describe 'callable subject' do
class Callable
def initialize(shall_raise = false)
@shall_raise = shall_raise
end
def call
shall_raise? ? raise('oops') : 'okay'
end
private def shall_raise?
@shall_raise
end
end
let(:callable) { Callable.new(true) }
context 'official approach (lambda)' do
subject { -> { callable.call } }
it { is_expected.to raise_error(RuntimeError) }
end
context 'callable.method(:call)' do
subject { callable.method(:call).to_proc }
it { is_expected.to raise_error(RuntimeError) }
end
context 'custom callable_subject helper' do
# needs to be tweaked to get the `let` vars reachable
callable_subject { Callable.new(true).call }
it { is_expected.to raise_error(RuntimeError) }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment