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
|
require 'spec_helper.rb'
describe AttrOptional do
before do
@a, @b, @c = A.new, B.new, C.new
end
describe '.attr_optional' do
it 'should define accessible attributes' do
expect(@a).to respond_to :attr_optional_a
expect(@a).to respond_to :attr_optional_a=
expect(@b).to respond_to :attr_optional_b
expect(@b).to respond_to :attr_optional_b=
end
it 'should be inherited' do
expect(@b).to respond_to :attr_optional_a
expect(@b).to respond_to :attr_optional_a=
end
context 'when already required' do
it 'should be optional' do
expect(@c.attr_required?(:attr_required_b)).to be_falsey
expect(@c.attr_optional?(:attr_required_b)).to be_truthy
end
end
context 'when AttrRequired not included' do
it 'should do nothing' do
expect(OnlyOptional.optional_attributes).to eq([:only_optional])
end
end
end
describe '.attr_optional?' do
it 'should answer whether the attributes is optional or not' do
expect(A.attr_optional?(:attr_optional_a)).to be_truthy
expect(B.attr_optional?(:attr_optional_a)).to be_truthy
expect(B.attr_optional?(:attr_optional_b)).to be_truthy
expect(B.attr_optional?(:to_s)).to be_falsey
end
end
describe '#attr_optional?' do
it 'should answer whether the attributes is optional or not' do
expect(@a.attr_optional?(:attr_optional_a)).to be_truthy
expect(@b.attr_optional?(:attr_optional_a)).to be_truthy
expect(@b.attr_optional?(:attr_optional_b)).to be_truthy
expect(@b.attr_optional?(:to_s)).to be_falsey
end
end
describe '.optional_attributes' do
it 'should return all optional attributes keys' do
expect(A.optional_attributes).to eq([:attr_optional_a])
expect(B.optional_attributes).to eq([:attr_optional_a, :attr_optional_b])
end
end
describe '#optional_attributes' do
it 'should return optional attributes keys' do
expect(@a.optional_attributes).to eq([:attr_optional_a])
expect(@b.optional_attributes).to eq([:attr_optional_a, :attr_optional_b])
end
end
describe '.undef_optional_attributes' do
it 'should undefine accessors and remove from optional attributes' do
expect(C.optional_attributes).not_to include :attr_optional_a
expect(@c.optional_attributes).not_to include :attr_optional_a
expect(@c).not_to respond_to :attr_optional_a
expect(@c).not_to respond_to :attr_optional_a=
end
end
end
|