|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe RSpec::Mock::Context do |
| 4 | + let(:example_group) { double('ExampleGroup') } # rubocop:disable RSpec/VerifiedDoubles |
| 5 | + let(:context) { described_class.new(example_group) } |
| 6 | + |
| 7 | + describe '#initialize' do |
| 8 | + it 'stores the example group' do |
| 9 | + expect(context.instance_variable_get(:@example_group)).to eq(example_group) |
| 10 | + end |
| 11 | + end |
| 12 | + |
| 13 | + describe '#method_missing' do |
| 14 | + context 'when example group responds to the method' do |
| 15 | + it 'delegates the method to example group' do |
| 16 | + expect(example_group).to receive(:respond_to?).with(:some_method).and_return(true) |
| 17 | + expect(example_group).to receive(:some_method).with('arg1', 'arg2').and_return('result') |
| 18 | + |
| 19 | + result = context.some_method('arg1', 'arg2') |
| 20 | + expect(result).to eq('result') |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + context 'when example group does not respond to the method' do |
| 25 | + it 'raises NoMethodError' do |
| 26 | + expect(example_group).to receive(:respond_to?).with(:unknown_method) |
| 27 | + expect { context.unknown_method }.to raise_error(NoMethodError) |
| 28 | + end |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + describe '#respond_to_missing?' do |
| 33 | + context 'when example group responds to the method' do |
| 34 | + it 'returns true' do |
| 35 | + expect(example_group).to receive(:respond_to?).with(:some_method, false).and_return(true) |
| 36 | + expect(context.respond_to?(:some_method)).to be(true) |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + context 'when example group does not respond to the method' do |
| 41 | + it 'returns false' do |
| 42 | + expect(example_group).to receive(:respond_to?).with(:unknown_method, false).and_return(false) |
| 43 | + expect(context.respond_to?(:unknown_method)).to be(false) |
| 44 | + end |
| 45 | + end |
| 46 | + end |
| 47 | +end |
0 commit comments