1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
require 'spec_helper.rb'
describe AttrRequired do
before do
@a, @b, @c = A.new, B.new, C.new
end
describe '.attr_required' do
it 'should define accessible attributes' do
expect(@a).to respond_to :attr_required_a
expect(@a).to respond_to :attr_required_a=
expect(@b).to respond_to :attr_required_b
expect(@b).to respond_to :attr_required_b=
end
it 'should be inherited' do
expect(@b).to respond_to :attr_required_a
expect(@b).to respond_to :attr_required_a=
end
context 'when already optional' do
it 'should be optional' do
expect(@c.attr_required?(:attr_optional_b)).to be_truthy
expect(@c.attr_optional?(:attr_optional_b)).to be_falsey
end
end
context 'when AttrOptional not included' do
it 'should do nothing' do
expect(OnlyRequired.required_attributes).to eq([:only_required])
end
end
end
describe '.attr_required?' do
it 'should answer whether the attributes is required or not' do
expect(A.attr_required?(:attr_required_a)).to be_truthy
expect(B.attr_required?(:attr_required_a)).to be_truthy
expect(B.attr_required?(:attr_required_b)).to be_truthy
expect(B.attr_required?(:to_s)).to be_falsey
end
end
describe '#attr_required?' do
it 'should answer whether the attributes is required or not' do
expect(@a.attr_required?(:attr_required_a)).to be_truthy
expect(@b.attr_required?(:attr_required_a)).to be_truthy
expect(@b.attr_required?(:attr_required_b)).to be_truthy
expect(@a.attr_required?(:attr_required_b)).to be_falsey
expect(@b.attr_required?(:to_s)).to be_falsey
end
end
describe '#attr_missing?' do
it 'should answer whether any attributes are missing' do
expect(@a.attr_missing?).to be_truthy
expect(@b.attr_missing?).to be_truthy
@a.attr_required_a = 'attr_required_a'
@b.attr_required_a = 'attr_required_a'
expect(@a.attr_missing?).to be_falsey
expect(@b.attr_missing?).to be_truthy
@b.attr_required_b = 'attr_required_b'
expect(@b.attr_missing?).to be_falsey
end
end
describe '#attr_missing!' do
it 'should raise AttrMissing error when any attributes are missing' do
expect(lambda { @a.attr_missing! }).to raise_error(AttrRequired::AttrMissing)
expect(lambda { @b.attr_missing! }).to raise_error(AttrRequired::AttrMissing)
@a.attr_required_a = 'attr_required_a'
@b.attr_required_a = 'attr_required_a'
expect(lambda { @a.attr_missing! }).not_to raise_error
expect(lambda { @b.attr_missing! }).to raise_error(AttrRequired::AttrMissing)
@b.attr_required_b = 'attr_required_b'
expect(lambda { @b.attr_missing! }).not_to raise_error
end
end
describe '#attr_missing' do
it 'should return missing attributes keys' do
expect(@a.attr_missing).to eq([:attr_required_a])
expect(@b.attr_missing).to eq([:attr_required_a, :attr_required_b])
@a.attr_required_a = 'attr_required_a'
@b.attr_required_a = 'attr_required_a'
expect(@a.attr_missing).to eq([])
expect(@b.attr_missing).to eq([:attr_required_b])
end
end
describe '.required_attributes' do
it 'should return all required attributes keys' do
expect(A.required_attributes).to eq([:attr_required_a])
expect(B.required_attributes).to eq([:attr_required_a, :attr_required_b])
end
end
describe '#required_attributes' do
it 'should return required attributes keys' do
expect(@a.required_attributes).to eq([:attr_required_a])
expect(@b.required_attributes).to eq([:attr_required_a, :attr_required_b])
end
end
describe '.undef_required_attributes' do
it 'should undefine accessors and remove from required attributes' do
expect(C.required_attributes).not_to include :attr_required_a
expect(@c.required_attributes).not_to include :attr_required_a
expect(@c).not_to respond_to :attr_required_a
expect(@c).not_to respond_to :attr_required_a=
end
end
end
|