Created
March 2, 2018 09:44
-
-
Save asaaki/baa6408fbd60f267e68c21f26d576c6f to your computer and use it in GitHub Desktop.
callables in specs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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