reading_group_spec.rb
3.75 KB
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
require 'rails_helper'
describe ReadingGroup, :type => :model do
describe 'methods' do
describe 'persisted?' do
before :each do
@subject = FactoryGirl.build(:reading_group, :with_id)
end
end
describe 'update' do
before :each do
@qt = FactoryGirl.build(:reading_group, :with_id)
@qt_params = @qt.to_hash
end
context 'with valid attributes' do
before :each do
@qt.expects(:save).with(@qt_params).returns(true)
end
it 'should return true' do
expect(@qt.save(@qt_params)).to eq(true)
end
end
context 'with invalid attributes' do
before :each do
@qt.expects(:save).with(@qt_params).returns(false)
end
it 'should return false' do
expect(@qt.save(@qt_params)).to eq(false)
end
end
end
describe 'readings' do
subject { FactoryGirl.build(:reading_group, :with_id) }
let(:reading) { FactoryGirl.build(:reading_with_id) }
it 'should call readings_of on the Reading model' do
subject.expects(:readings).returns([reading])
expect(subject.readings).to include(reading)
end
end
end
describe 'class methods' do
describe 'public_or_owned_by_user' do
let!(:user) { FactoryGirl.build(:user) }
let!(:owned_private_attrs) { FactoryGirl.build(:reading_group_attributes, :private, user_id: user.id) }
let!(:owned_public_attrs) { FactoryGirl.build(:reading_group_attributes, user_id: user.id) }
let!(:not_owned_private_attrs) { FactoryGirl.build(:reading_group_attributes, :private, user_id: user.id+1) }
let!(:not_owned_public_attrs) { FactoryGirl.build(:reading_group_attributes, user_id: user.id+1) }
let!(:public_attrs) { [owned_public_attrs, not_owned_public_attrs] }
let(:public_reading_groups) { public_attrs.map(&:reading_group) }
let!(:owned_or_public_attrs) { public_attrs + [owned_private_attrs] }
let!(:owned_or_public_reading_groups) { owned_or_public_attrs.map(&:reading_group) }
let(:all_reading_groups) { owned_or_public_reading_groups + [not_owned_private_attrs.reading_group] }
context 'when reading groups exist' do
before :each do
# Make sure the reading groups are found when looked up by the Attributes by their id
all_reading_groups.each do |reading_group|
ReadingGroup.stubs(:find).with(reading_group.id).returns(reading_group)
end
ReadingGroupAttributes.expects(:where).with(public: true).returns(public_attrs)
end
context 'when user is not provided' do
it 'should find all public reading groups' do
expect(ReadingGroup.public).to eq(public_reading_groups)
end
end
context 'when user is provided' do
before do
ReadingGroupAttributes.expects(:where).with(user_id: user.id, public: false).returns([owned_private_attrs])
end
it 'should find all public and owned reading groups' do
p all_reading_groups
expect(ReadingGroup.public_or_owned_by_user(user)).to eq(owned_or_public_reading_groups)
end
end
end
context 'when no reading groups exist' do
before :each do
# Map the reading group attributes to the corresponding Reading Group
all_reading_groups.each do |reading_group|
ReadingGroup.stubs(:find).with(reading_group.id).raises(KalibroClient::Errors::RecordNotFound)
end
ReadingGroupAttributes.expects(:where).with(public: true).returns(public_attrs)
end
it 'is expected to be empty' do
expect(ReadingGroup.public).to be_empty
end
end
end
end
end