|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +RSpec.describe 'be_invalid_on matcher' do |
| 4 | + let(:matchers) { MatchersMock.new } |
| 5 | + before { ValidationExamplesMatcher.register(matchers) } |
| 6 | + let(:model) { TestModel.new } |
| 7 | + |
| 8 | + let(:match_block) { matchers.match_blocks[:be_invalid_on] } |
| 9 | + let(:chain_blocks) { matchers.chain_blocks[:be_invalid_on] } |
| 10 | + let(:with_chain_block) { chain_blocks[:with] } |
| 11 | + let(:on_context_chain_block) { chain_blocks[:on_context] } |
| 12 | + let(:failure_message_block) { matchers.failure_message_blocks[:be_invalid_on] } |
| 13 | + |
| 14 | + it 'is registered with match block' do |
| 15 | + expect(match_block).to be_kind_of(Proc) |
| 16 | + end |
| 17 | + |
| 18 | + describe 'match block' do |
| 19 | + let(:model) { double('model') } |
| 20 | + before { matchers.instance_exec(:a_value, &with_chain_block) } |
| 21 | + |
| 22 | + context 'without on_context chain' do |
| 23 | + it "calls model's setter and valid?" do |
| 24 | + expect(model).to receive(:attr=).with(:a_value) |
| 25 | + expect(model).to receive(:invalid?).with(nil) |
| 26 | + matchers.instance_exec(model, &match_block) |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + context 'with on_context chain' do |
| 31 | + before { matchers.instance_exec(:a_context, &on_context_chain_block) } |
| 32 | + it "calls model's setter and valid?" do |
| 33 | + expect(model).to receive(:attr=).with(:a_value) |
| 34 | + expect(model).to receive(:invalid?).with(:a_context) |
| 35 | + matchers.instance_exec(model, &match_block) |
| 36 | + end |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + it 'is registered with :with chain block' do |
| 41 | + expect(with_chain_block).to be_kind_of(Proc) |
| 42 | + end |
| 43 | + |
| 44 | + describe 'with chain block' do |
| 45 | + it 'sets @value' do |
| 46 | + matchers.instance_exec(:a_value, &with_chain_block) |
| 47 | + expect(matchers.value).to eq :a_value |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + it 'is registered with :on_context chain block' do |
| 52 | + expect(on_context_chain_block).to be_kind_of(Proc) |
| 53 | + end |
| 54 | + |
| 55 | + describe 'with on_context block' do |
| 56 | + it 'sets @context' do |
| 57 | + matchers.instance_exec(:a_context, &on_context_chain_block) |
| 58 | + expect(matchers.context).to eq :a_context |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + |
| 63 | + it 'is registered with failure_message block' do |
| 64 | + expect(failure_message_block).to be_kind_of(Proc) |
| 65 | + end |
| 66 | + |
| 67 | + describe 'failure_message block' do |
| 68 | + context 'the model is valid' do |
| 69 | + before { model.attr = 'ok' } |
| 70 | + it 'returns message saying "it should be invalid"' do |
| 71 | + expect(matchers.instance_exec(model, &failure_message_block)) |
| 72 | + .to eq 'expect TestModel to be invalid, but it is VALID.' |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + context 'the model is invalid but it has no errors' do |
| 77 | + let(:model) { double('model') } |
| 78 | + it 'returns message saying "it should have errors"' do |
| 79 | + expect(model).to receive(:valid?) { false } |
| 80 | + expect(matchers.instance_exec(model, &failure_message_block)) |
| 81 | + .to eq 'expect RSpec::Mocks::Double to have errors on attr, but EMPTY.' |
| 82 | + end |
| 83 | + end |
| 84 | + end |
| 85 | +end |
0 commit comments