|
5 | 5 | subject(:builder_class) { described_class } |
6 | 6 |
|
7 | 7 | it { is_expected.to be < DnsMock::Record::Builder::Base } |
| 8 | + it { is_expected.to be_const_defined(:MX_RECORD_REGEX_PATTERN) } |
8 | 9 | it { is_expected.to be_const_defined(:RECORD_PREFERENCE_STEP) } |
9 | 10 | end |
10 | 11 |
|
| 12 | + describe 'MX_RECORD_REGEX_PATTERN' do |
| 13 | + subject(:regex_pattern) { described_class::MX_RECORD_REGEX_PATTERN } |
| 14 | + |
| 15 | + context 'when host with priority' do |
| 16 | + subject(:string) { "#{host}:#{priority}" } |
| 17 | + |
| 18 | + let(:host) { random_hostname } |
| 19 | + let(:priority) { rand(100).to_s } |
| 20 | + |
| 21 | + it { expect(regex_pattern.match?(string)).to be(true) } |
| 22 | + it { expect(string[regex_pattern, 1]).to eq(host) } |
| 23 | + it { expect(string[regex_pattern, 2]).to eq(priority) } |
| 24 | + end |
| 25 | + |
| 26 | + context 'when host without priority' do |
| 27 | + subject(:string) { random_hostname } |
| 28 | + |
| 29 | + it { expect(regex_pattern.match?(string)).to be(true) } |
| 30 | + it { expect(string[regex_pattern, 3]).to eq(string) } |
| 31 | + end |
| 32 | + end |
| 33 | + |
11 | 34 | describe '.call' do |
12 | 35 | subject(:builder) { described_class.call(target_factory, records_data) } |
13 | 36 |
|
14 | 37 | let(:target_factory) { class_double('TargetFactory') } |
15 | | - let(:target_class_instance) { instance_double('TargetClass') } |
16 | | - let(:target_factory_instance) { instance_double('TargetFactory', create: target_class_instance) } |
17 | | - let(:records_data) { (0..2) } |
18 | | - |
19 | | - it 'returns array of target class instances' do |
20 | | - [10, 20, 30].zip(records_data).each do |record_preference, record_data| |
21 | | - expect(target_factory) |
22 | | - .to receive(:new) |
23 | | - .with(record_data: [record_preference, record_data]) |
24 | | - .and_return(target_factory_instance) |
| 38 | + |
| 39 | + describe 'Success' do |
| 40 | + context 'when valid records_data nested type' do |
| 41 | + let(:target_class_instance) { instance_double('TargetClass') } |
| 42 | + let(:target_factory_instance) { instance_double('TargetFactory', create: target_class_instance) } |
| 43 | + let(:mx_host) { random_hostname } |
| 44 | + let(:mx_priority) { 0 } |
| 45 | + let(:records_data) { ["#{mx_host}:#{mx_priority}", 'b', 'c'] } |
| 46 | + |
| 47 | + it 'returns array of target class instances' do |
| 48 | + [10, 20, 30].zip(records_data).each_with_index do |(record_preference, record_data), index| |
| 49 | + expect(target_factory) |
| 50 | + .to receive(:new) |
| 51 | + .with(record_data: index.zero? ? [mx_priority, mx_host] : [record_preference, record_data]) |
| 52 | + .and_return(target_factory_instance) |
| 53 | + end |
| 54 | + expect(builder).to eq(::Array.new(records_data.size) { target_class_instance }) |
| 55 | + end |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + describe 'Failure' do |
| 60 | + context 'when invalid records_data nested type' do |
| 61 | + let(:record_data_object) { 42 } |
| 62 | + let(:records_data) { [record_data_object] } |
| 63 | + |
| 64 | + it do |
| 65 | + expect { builder }.to raise_error( |
| 66 | + DnsMock::Error::RecordContextType, |
| 67 | + "#{record_data_object.class} is invalid record context type for MX record. Should be a String" |
| 68 | + ) |
| 69 | + end |
25 | 70 | end |
26 | | - expect(builder).to eq(::Array.new(records_data.size) { target_class_instance }) |
27 | 71 | end |
28 | 72 | end |
29 | 73 | end |
0 commit comments