Commit 8f8736f2b588468bf358062ffd2da2f053f9b198
1 parent
7048a0b6
Exists in
colab
and in
4 other branches
Refactor ReadingGroup model visibility tests
Showing
1 changed file
with
21 additions
and
28 deletions
Show diff stats
spec/models/reading_group_spec.rb
| ... | ... | @@ -49,35 +49,26 @@ describe ReadingGroup, :type => :model do |
| 49 | 49 | |
| 50 | 50 | describe 'class methods' do |
| 51 | 51 | describe 'public_or_owned_by_user' do |
| 52 | - def build_attrs(rg_iter, *traits, **params) | |
| 53 | - reading_group = rg_iter.next | |
| 54 | - attr = FactoryGirl.build(:reading_group_attributes, *traits, params.merge(reading_group: reading_group)) | |
| 55 | - reading_group.stubs(:attributes).returns(attr) | |
| 56 | - attr | |
| 57 | - end | |
| 58 | - | |
| 59 | - let!(:reading_groups) { FactoryGirl.build_list(:reading_group, 4, :with_id) } | |
| 60 | - let!(:rgs_iter) { reading_groups.each } | |
| 61 | - | |
| 62 | - let!(:one_user) { FactoryGirl.build(:user) } | |
| 63 | - let!(:other_user) { FactoryGirl.build(:another_user) } | |
| 52 | + let!(:user) { FactoryGirl.build(:user) } | |
| 64 | 53 | |
| 65 | - let!(:ones_private_attrs) { build_attrs(rgs_iter, :private, user: one_user) } | |
| 66 | - let!(:others_private_attrs) { build_attrs(rgs_iter, :private, user: other_user) } | |
| 67 | - let!(:ones_public_attrs) { build_attrs(rgs_iter, user: one_user) } | |
| 68 | - let!(:others_public_attrs) { build_attrs(rgs_iter, user: other_user) } | |
| 54 | + let!(:owned_private_attrs) { FactoryGirl.build(:reading_group_attributes, :private, user_id: user.id) } | |
| 55 | + let!(:owned_public_attrs) { FactoryGirl.build(:reading_group_attributes, user_id: user.id) } | |
| 56 | + let!(:not_owned_private_attrs) { FactoryGirl.build(:reading_group_attributes, :private, user_id: user.id+1) } | |
| 57 | + let!(:not_owned_public_attrs) { FactoryGirl.build(:reading_group_attributes, user_id: user.id+1) } | |
| 69 | 58 | |
| 70 | - let!(:public_attrs) { [ones_public_attrs, others_public_attrs] } | |
| 59 | + let!(:public_attrs) { [owned_public_attrs, not_owned_public_attrs] } | |
| 71 | 60 | let(:public_reading_groups) { public_attrs.map(&:reading_group) } |
| 72 | 61 | |
| 73 | - let(:ones_or_public_attrs) { public_attrs + [ones_private_attrs] } | |
| 74 | - let(:ones_or_public_reading_groups) { ones_or_public_attrs.map(&:reading_group) } | |
| 62 | + let!(:owned_or_public_attrs) { public_attrs + [owned_private_attrs] } | |
| 63 | + let!(:owned_or_public_reading_groups) { owned_or_public_attrs.map(&:reading_group) } | |
| 75 | 64 | |
| 76 | - context 'when the reading group exists' do | |
| 65 | + let(:all_reading_groups) { owned_or_public_reading_groups + [not_owned_private_attrs.reading_group] } | |
| 66 | + | |
| 67 | + context 'when reading groups exist' do | |
| 77 | 68 | before :each do |
| 78 | - # Map the reading group attributes to the corresponding Reading Group | |
| 79 | - reading_groups.each do |rg| | |
| 80 | - ReadingGroup.stubs(:find).with(rg.id).returns(rg) | |
| 69 | + # Make sure the reading groups are found when looked up by the Attributes by their id | |
| 70 | + all_reading_groups.each do |reading_group| | |
| 71 | + ReadingGroup.stubs(:find).with(reading_group.id).returns(reading_group) | |
| 81 | 72 | end |
| 82 | 73 | |
| 83 | 74 | ReadingGroupAttributes.expects(:where).with(public: true).returns(public_attrs) |
| ... | ... | @@ -91,20 +82,22 @@ describe ReadingGroup, :type => :model do |
| 91 | 82 | |
| 92 | 83 | context 'when user is provided' do |
| 93 | 84 | before do |
| 94 | - ReadingGroupAttributes.expects(:where).with(user_id: one_user.id, public: false).returns([ones_private_attrs]) | |
| 85 | + ReadingGroupAttributes.expects(:where).with(user_id: user.id, public: false).returns([owned_private_attrs]) | |
| 95 | 86 | end |
| 96 | 87 | |
| 97 | 88 | it 'should find all public and owned reading groups' do |
| 98 | - expect(ReadingGroup.public_or_owned_by_user(one_user)).to eq(ones_or_public_reading_groups) | |
| 89 | + p all_reading_groups | |
| 90 | + | |
| 91 | + expect(ReadingGroup.public_or_owned_by_user(user)).to eq(owned_or_public_reading_groups) | |
| 99 | 92 | end |
| 100 | 93 | end |
| 101 | 94 | end |
| 102 | 95 | |
| 103 | - context 'when the reading group does not' do | |
| 96 | + context 'when no reading groups exist' do | |
| 104 | 97 | before :each do |
| 105 | 98 | # Map the reading group attributes to the corresponding Reading Group |
| 106 | - reading_groups.each do |rg| | |
| 107 | - ReadingGroup.stubs(:find).with(rg.id).raises(KalibroClient::Errors::RecordNotFound) | |
| 99 | + all_reading_groups.each do |reading_group| | |
| 100 | + ReadingGroup.stubs(:find).with(reading_group.id).raises(KalibroClient::Errors::RecordNotFound) | |
| 108 | 101 | end |
| 109 | 102 | |
| 110 | 103 | ReadingGroupAttributes.expects(:where).with(public: true).returns(public_attrs) | ... | ... |