Skip to content

Commit cffb06e

Browse files
committed
add more test coverage for has_many relation on one-sided association
1 parent 4933eda commit cffb06e

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

lib/active_poro/relations.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ module Relations
44

55
module ClassMethods
66
def has_many(target_name)
7+
8+
9+
710
# define getter method
811
define_method target_name do
912
instance_variable_get("@#{target_name}") || []

spec/active_poro_spec.rb

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,26 +131,47 @@ class Flea < BaseTestClass
131131

132132
end
133133

134-
context 'when a flea jumps between dogs' do
134+
context 'when fleas jump between dogs' do
135135

136136
let(:small_dog){ Dog.new('Small dog') }
137137

138138
it 'cannot be in two dogs at the same time' do
139139
flea_a.dog = big_dog
140+
flea_b.dog = big_dog
140141
expect(flea_a.dog).to eq(big_dog)
141-
expect(big_dog.fleas).to eq([flea_a])
142+
expect(flea_b.dog).to eq(big_dog)
143+
expect(big_dog.fleas).to eq([flea_a, flea_b])
142144

143-
# flea jumps from big dog to small dog
145+
# flea A jumps from big dog to small dog
144146
flea_a.dog = small_dog
145147
expect(flea_a.dog).to eq(small_dog)
146148
expect(small_dog.fleas).to eq([flea_a])
147149

148150
# flea A no longer in big dog
149151
expect(big_dog.fleas).to_not include(flea_a)
150-
expect(big_dog.fleas).to be_empty # sanity check
152+
expect(big_dog.fleas).to eq([flea_b])# sanity check
153+
end
154+
155+
end
156+
157+
context 'when a dog catches some fleas from another dog' do
158+
159+
let(:small_dog){ Dog.new('Small dog') }
160+
161+
it 'the other dog losses part of its fleas' do
162+
163+
big_dog.fleas = [flea_a, flea_b]
164+
165+
# small_dog is close enough to bid_dog and it caches a flea
166+
small_dog.fleas = [flea_a]
167+
168+
# flea A no longer in big dog
169+
expect(big_dog.fleas).to_not include(flea_a)
170+
expect(big_dog.fleas).to eq([flea_b])# sanity check
151171
end
152172

153173
end
174+
154175
end
155176

156177
end

0 commit comments

Comments
 (0)