readings_controller_spec.rb
3.02 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
require 'spec_helper'
describe ReadingsController do
let(:reading_group) { FactoryGirl.build(:reading_group) }
describe 'new' do
before :each do
sign_in FactoryGirl.create(:user)
end
context 'when the current user owns the reading group' do
before :each do
subject.expects(:reading_owner?).returns true
get :new, reading_group_id: reading_group.id
end
it { should respond_with(:success) }
it { should render_template(:new) }
end
context "when the current user doesn't owns the reading group" do
before :each do
get :new, reading_group_id: reading_group.id
end
it { should redirect_to(reading_group_url(reading_group.id)) }
it { should respond_with(:redirect) }
end
end
describe 'create' do
let(:reading) { FactoryGirl.build(:reading) }
let(:reading_params) { Hash[FactoryGirl.attributes_for(:reading).map { |k,v| [k.to_s, v.to_s] }] } #FIXME: Mocha is creating the expectations with strings, but FactoryGirl returns everything with symbols and integers
before do
sign_in FactoryGirl.create(:user)
end
context 'when the current user owns the reading group' do
before :each do
subject.expects(:reading_owner?).returns true
end
context 'with valid fields' do
before :each do
Reading.any_instance.expects(:save).returns(true)
post :create, reading_group_id: reading_group.id, reading: reading_params
end
it { should respond_with(:redirect) }
end
context 'with invalid fields' do
before :each do
Reading.any_instance.expects(:save).returns(false)
post :create, reading_group_id: reading_group.id, reading: reading_params
end
it { should render_template(:new) }
end
end
end
describe 'edit' do
let(:reading) { FactoryGirl.build(:reading) }
context 'with an User logged in' do
before do
sign_in FactoryGirl.create(:user)
end
context 'when the user owns the reading' do
before :each do
subject.expects(:reading_owner?).returns true
Reading.expects(:find).at_least_once.with(reading.id).returns(reading)
get :edit, id: reading.id, reading_group_id: reading_group.id.to_s
end
it { should render_template(:edit) }
end
context 'when the user does not own the reading' do
before do
Reading.expects(:find).at_least_once.with(reading.id).returns(reading)
get :edit, id: reading.id, reading_group_id: reading_group.id.to_s
end
it { should redirect_to(reading_group_url(reading_group.id)) }
it { should respond_with(:redirect) }
it { should set_the_flash[:notice].to("You're not allowed to do this operation") }
end
end
context 'with no user logged in' do
before :each do
get :edit, id: reading.id, reading_group_id: reading_group.id.to_s
end
it { should redirect_to new_user_session_path }
end
end
end