Commit 94503e7bf9a175b67d14a0ff1cd82cb9d39f44e5
Committed by
Diego Camarinha
1 parent
a682686e
Exists in
colab
and in
4 other branches
Fixed tests for finding visible reading groups and configurations
Signed-off-by: Daniel Miranda <danielkza2@gmail.com>
Showing
3 changed files
with
88 additions
and
10 deletions
Show diff stats
spec/factories/kalibro_configurations.rb
| ... | ... | @@ -0,0 +1,61 @@ |
| 1 | +require 'rails_helper' | |
| 2 | + | |
| 3 | +describe KalibroConfiguration, :type => :model do | |
| 4 | + describe 'methods' do | |
| 5 | + | |
| 6 | + describe 'class methods' do | |
| 7 | + describe 'public_or_owned_by_user' do | |
| 8 | + def build_attrs(kc_iter, *traits, **params) | |
| 9 | + kalibro_configuration = kc_iter.next | |
| 10 | + attr = FactoryGirl.build(:kalibro_configuration_attributes, *traits, params.merge(kalibro_configuration: kalibro_configuration)) | |
| 11 | + kalibro_configuration.stubs(:attributes).returns(attr) | |
| 12 | + attr | |
| 13 | + end | |
| 14 | + | |
| 15 | + let!(:kalibro_configurations) { FactoryGirl.build_list(:kalibro_configuration, 4, :with_sequential_id) } | |
| 16 | + let!(:kc_iter) { kalibro_configurations.each } | |
| 17 | + | |
| 18 | + let!(:one_user) { FactoryGirl.build(:user) } | |
| 19 | + let!(:other_user) { FactoryGirl.build(:another_user) } | |
| 20 | + | |
| 21 | + let!(:ones_private_attrs) { build_attrs(kc_iter, :private, user: one_user) } | |
| 22 | + let!(:others_private_attrs) { build_attrs(kc_iter, :private, user: other_user) } | |
| 23 | + let!(:ones_public_attrs) { build_attrs(kc_iter, user: one_user) } | |
| 24 | + let!(:others_public_attrs) { build_attrs(kc_iter, user: other_user) } | |
| 25 | + | |
| 26 | + let!(:public_attrs) { [ones_public_attrs, others_public_attrs] } | |
| 27 | + let(:public_kalibro_configurations) { public_attrs.map(&:kalibro_configuration) } | |
| 28 | + | |
| 29 | + let(:ones_or_public_attrs) { public_attrs + [ones_private_attrs] } | |
| 30 | + let(:ones_or_public_kalibro_configurations) { ones_or_public_attrs.map(&:kalibro_configuration) } | |
| 31 | + | |
| 32 | + before :each do | |
| 33 | + # Map the reading group attributes to the corresponding Reading Group | |
| 34 | + kalibro_configurations.each do |kc| | |
| 35 | + KalibroConfiguration.stubs(:find).with(kc.id).returns(kc) | |
| 36 | + end | |
| 37 | + end | |
| 38 | + | |
| 39 | + context 'when user is not provided' do | |
| 40 | + before do | |
| 41 | + KalibroConfigurationAttributes.expects(:where).with(public: true).returns(public_attrs) | |
| 42 | + end | |
| 43 | + | |
| 44 | + it 'should find all public reading groups' do | |
| 45 | + expect(KalibroConfiguration.public).to eq(public_kalibro_configurations) | |
| 46 | + end | |
| 47 | + end | |
| 48 | + | |
| 49 | + context 'when user is provided' do | |
| 50 | + before do | |
| 51 | + KalibroConfigurationAttributes.expects(:where).with(kind_of(String), one_user.id).returns(ones_or_public_attrs) | |
| 52 | + end | |
| 53 | + | |
| 54 | + it 'should find all public and owned reading groups' do | |
| 55 | + expect(KalibroConfiguration.public_or_owned_by_user(one_user)).to eq(ones_or_public_kalibro_configurations) | |
| 56 | + end | |
| 57 | + end | |
| 58 | + end | |
| 59 | + end | |
| 60 | + end | |
| 61 | +end | ... | ... |
spec/models/reading_group_spec.rb
| ... | ... | @@ -50,7 +50,10 @@ describe ReadingGroup, :type => :model do |
| 50 | 50 | describe 'class methods' do |
| 51 | 51 | describe 'public_or_owned_by_user' do |
| 52 | 52 | def build_attrs(rg_iter, *traits, **params) |
| 53 | - FactoryGirl.build(:reading_group_attributes, :with_id, *traits, params.merge(reading_group: rg_iter.next)) | |
| 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 | |
| 54 | 57 | end |
| 55 | 58 | |
| 56 | 59 | let!(:reading_groups) { FactoryGirl.build_list(:reading_group, 4, :with_id) } |
| ... | ... | @@ -59,10 +62,10 @@ describe ReadingGroup, :type => :model do |
| 59 | 62 | let!(:one_user) { FactoryGirl.build(:user) } |
| 60 | 63 | let!(:other_user) { FactoryGirl.build(:another_user) } |
| 61 | 64 | |
| 62 | - let!(:ones_private_attrs) { build_attrs(rgs_iter, user: one_user) } | |
| 63 | - let!(:others_private_attrs) { build_attrs(rgs_iter, user: other_user) } | |
| 64 | - let!(:ones_public_attrs) { build_attrs(rgs_iter, :public, user: one_user) } | |
| 65 | - let!(:others_public_attrs) { build_attrs(rgs_iter, :public, user: other_user) } | |
| 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) } | |
| 66 | 69 | |
| 67 | 70 | let!(:public_attrs) { [ones_public_attrs, others_public_attrs] } |
| 68 | 71 | let(:public_reading_groups) { public_attrs.map(&:reading_group) } |
| ... | ... | @@ -72,18 +75,28 @@ describe ReadingGroup, :type => :model do |
| 72 | 75 | |
| 73 | 76 | before :each do |
| 74 | 77 | # Map the reading group attributes to the corresponding Reading Group |
| 75 | - ReadingGroup.expects(:find).with(kind_of(Integer)) do |id| | |
| 76 | - reading_groups.find { |rg| rg.id == id } | |
| 78 | + reading_groups.each do |rg| | |
| 79 | + ReadingGroup.stubs(:find).with(rg.id).returns(rg) | |
| 77 | 80 | end |
| 78 | 81 | end |
| 79 | 82 | |
| 80 | - context 'when user is nil' do | |
| 83 | + context 'when user is not provided' do | |
| 81 | 84 | before do |
| 82 | 85 | ReadingGroupAttributes.expects(:where).with(public: true).returns(public_attrs) |
| 83 | 86 | end |
| 84 | 87 | |
| 85 | - xit 'should find all public reading groups' do | |
| 86 | - expect(ReadingGroupAttributes.public_or_owned_by_user(nil)).to eq(public_reading_groups) | |
| 88 | + it 'should find all public reading groups' do | |
| 89 | + expect(ReadingGroup.public).to eq(public_reading_groups) | |
| 90 | + end | |
| 91 | + end | |
| 92 | + | |
| 93 | + context 'when user is provided' do | |
| 94 | + before do | |
| 95 | + ReadingGroupAttributes.expects(:where).with(kind_of(String), one_user.id).returns(ones_or_public_attrs) | |
| 96 | + end | |
| 97 | + | |
| 98 | + it 'should find all public and owned reading groups' do | |
| 99 | + expect(ReadingGroup.public_or_owned_by_user(one_user)).to eq(ones_or_public_reading_groups) | |
| 87 | 100 | end |
| 88 | 101 | end |
| 89 | 102 | end | ... | ... |