Skip to content

Commit 2586b00

Browse files
committed
Merge pull request #297 from rohitpaulk/fix-protips-controller-specs
Fix protip controller specs, add routing specs
2 parents 37590a0 + 0e15f8f commit 2586b00

File tree

3 files changed

+45
-32
lines changed

3 files changed

+45
-32
lines changed

app/models/protip.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ class Protip < ActiveRecord::Base
9797
validates :body, presence: true
9898
validates :kind, presence: true, inclusion: { in: KINDS }
9999
validates :topic_list, length: { minimum: 1 }
100+
validates :slug, presence: true
100101

101102
after_validation :tag_user
102103
before_create :assign_random_id

spec/controllers/protips_controller_spec.rb

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
RSpec.describe ProtipsController, type: :controller, skip: true do
1+
RSpec.describe ProtipsController, type: :controller do
22
let(:current_user) { Fabricate(:user) }
33

44
before { controller.send :sign_in, current_user }
@@ -7,23 +7,19 @@ def valid_attributes
77
{
88
title: 'hello world',
99
body: "somethings that's meaningful and nice",
10-
topics: %w(java javascript),
10+
topic_list: "java, javascript",
1111
user_id: current_user.id
1212
}
1313
end
1414

15-
def valid_session
16-
{}
17-
end
18-
1915
describe 'GET user' do
2016
describe 'banned' do
2117
it 'should assign user @protips for page, despite not being in search index' do
2218
current_user.update_attribute(:banned_at, Time.now)
2319
expect(current_user.banned?).to eq(true)
2420
Protip.rebuild_index
2521
protip = Protip.create! valid_attributes
26-
get :user, { username: current_user.username }, valid_session
22+
get :user, { username: current_user.username }
2723
expect(assigns(:protips).first.title).to eq(protip.title)
2824
end
2925
end
@@ -32,28 +28,33 @@ def valid_session
3228
it 'should assign user @protips for page' do
3329
Protip.rebuild_index
3430
protip = Protip.create! valid_attributes
35-
get :user, { username: current_user.username }, valid_session
31+
get :user, { username: current_user.username }
3632
expect(assigns(:protips).results.first.title).to eq(protip.title)
3733
end
38-
3934
end
40-
4135
end
4236

4337
# describe "GET topic" do
4438
# it "assigns all protips as @protips" do
4539
# Protip.rebuild_index
4640
# protip = Protip.create! valid_attributes
47-
# get :topic, {tags: "java"}, valid_session
41+
# get :topic, {tags: "java"}
4842
# expect(assigns(:protips).results.first.title).to eq(protip.title)
4943
# end
5044
# end
5145

5246
describe 'GET show using public_id' do
53-
it 'redirects to GET show using slug' do
47+
it 'redirects to GET show if slug is empty' do
48+
protip = Protip.create! valid_attributes
49+
protip.save
50+
get :show, { id: protip.to_param }
51+
expect(response).to redirect_to slug_protips_path(protip, protip.friendly_id)
52+
end
53+
54+
it 'redirects to GET show if slug is invalid' do
5455
protip = Protip.create! valid_attributes
5556
protip.save
56-
get :show, { id: protip.to_param }, valid_session
57+
get :show, { id: protip.to_param, slug: "an_invalid_slug" }
5758
expect(response).to redirect_to slug_protips_path(protip, protip.friendly_id)
5859
end
5960
end
@@ -62,7 +63,7 @@ def valid_session
6263
it 'assigns the requested protip as @protip' do
6364
protip = Protip.create! valid_attributes
6465
protip.save
65-
get :show, { id: protip.public_id, slug: protip.friendly_id }, valid_session
66+
get :show, { id: protip.public_id, slug: protip.friendly_id }
6667
expect(assigns(:protip)).to eq(protip)
6768
end
6869
end
@@ -71,26 +72,26 @@ def valid_session
7172
before { allow_any_instance_of(User).to receive(:skills).and_return(['skill']) } # User must have a skill to create protips
7273

7374
it 'assigns a new protip as @protip' do
74-
get :new, {}, valid_session
75+
get :new, {}
7576
expect(assigns(:protip)).to be_a_new(Protip)
7677
end
7778

7879
it 'allows viewing the page when you have a skill' do
79-
get :new, {}, valid_session
80+
get :new, {}
8081
expect(response).to render_template('new')
8182
end
8283

8384
it "prevents viewing the page when you don't have a skill" do
8485
allow_any_instance_of(User).to receive(:skills).and_return([])
85-
get :new, {}, valid_session
86+
get :new, {}
8687
expect(response).to redirect_to badge_path(username: current_user.username, anchor: 'add-skill')
8788
end
8889
end
8990

9091
describe 'GET edit' do
9192
it 'assigns the requested protip as @protip' do
9293
protip = Protip.create! valid_attributes
93-
get :edit, { id: protip.to_param }, valid_session
94+
get :edit, { id: protip.to_param }
9495
expect(assigns(:protip)).to eq(protip)
9596
end
9697
end
@@ -101,18 +102,18 @@ def valid_session
101102
describe 'with valid params' do
102103
it 'creates a new Protip' do
103104
expect do
104-
post :create, { protip: valid_attributes }, valid_session
105+
post :create, { protip: valid_attributes }
105106
end.to change(Protip, :count).by(1)
106107
end
107108

108109
it 'assigns a newly created protip as @protip' do
109-
post :create, { protip: valid_attributes }, valid_session
110+
post :create, { protip: valid_attributes }
110111
expect(assigns(:protip)).to be_a(Protip)
111112
expect(assigns(:protip)).to be_persisted
112113
end
113114

114115
it 'redirects to the created protip' do
115-
post :create, { protip: valid_attributes }, valid_session
116+
post :create, { protip: valid_attributes }
116117
expect(response).to redirect_to(Protip.last)
117118
end
118119
end
@@ -121,21 +122,21 @@ def valid_session
121122
it 'assigns a newly created but unsaved protip as @protip' do
122123
# Trigger the behavior that occurs when invalid params are submitted
123124
allow_any_instance_of(Protip).to receive(:save).and_return(false)
124-
post :create, { protip: {} }, valid_session
125+
post :create, { protip: {} }
125126
expect(assigns(:protip)).to be_a_new(Protip)
126127
end
127128

128129
it "re-renders the 'new' template" do
129130
# Trigger the behavior that occurs when invalid params are submitted
130131
allow_any_instance_of(Protip).to receive(:save).and_return(false)
131-
post :create, { protip: {} }, valid_session
132+
post :create, { protip: {} }
132133
expect(response).to render_template('new')
133134
end
134135
end
135136

136137
it "prevents creating when you don't have a skill" do
137138
allow_any_instance_of(User).to receive(:skills).and_return([])
138-
post :create, { protip: valid_attributes }, valid_session
139+
post :create, { protip: valid_attributes }
139140
expect(response).to redirect_to badge_path(username: current_user.username, anchor: 'add-skill')
140141
end
141142
end
@@ -149,18 +150,18 @@ def valid_session
149150
# receives the :update_attributes message with whatever params are
150151
# submitted in the request.
151152
expect_any_instance_of(Protip).to receive(:update_attributes).with('body' => 'params')
152-
put :update, { id: protip.to_param, protip: { 'body' => 'params' } }, valid_session
153+
put :update, { id: protip.to_param, protip: { 'body' => 'params' } }
153154
end
154155

155156
it 'assigns the requested protip as @protip' do
156157
protip = Protip.create! valid_attributes
157-
put :update, { id: protip.to_param, protip: valid_attributes }, valid_session
158+
put :update, { id: protip.to_param, protip: valid_attributes }
158159
expect(assigns(:protip)).to eq(protip)
159160
end
160161

161162
it 'redirects to the protip' do
162163
protip = Protip.create! valid_attributes
163-
put :update, { id: protip.to_param, protip: valid_attributes }, valid_session
164+
put :update, { id: protip.to_param, protip: valid_attributes }
164165
expect(response).to redirect_to(protip)
165166
end
166167
end
@@ -170,7 +171,7 @@ def valid_session
170171
protip = Protip.create! valid_attributes
171172
# Trigger the behavior that occurs when invalid params are submitted
172173
allow_any_instance_of(Protip).to receive(:save).and_return(false)
173-
put :update, { id: protip.to_param, protip: {} }, valid_session
174+
put :update, { id: protip.to_param, protip: {} }
174175
expect(assigns(:protip)).to eq(protip)
175176
end
176177

@@ -180,7 +181,7 @@ def valid_session
180181
# Trigger the behavior that occurs when invalid params are submitted
181182
allow_any_instance_of(Protip).to receive(:save).and_return(false)
182183

183-
put :update, { id: protip.to_param, protip: {} }, valid_session
184+
put :update, { id: protip.to_param, protip: {} }
184185
expect(response).to render_template('edit')
185186
end
186187
end
@@ -191,20 +192,20 @@ def valid_session
191192
attributes = valid_attributes
192193
attributes[:user_id] = Fabricate(:user).id
193194
protip = Protip.create! attributes
194-
delete :destroy, { id: protip.to_param }, valid_session
195+
delete :destroy, { id: protip.to_param }
195196
expect { protip.reload }.not_to raise_error
196197
end
197198

198199
it 'destroys the requested protip' do
199200
protip = Protip.create! valid_attributes
200201
expect {
201-
delete :destroy, { id: protip.to_param }, valid_session
202+
delete :destroy, { id: protip.to_param }
202203
}.to change(Protip, :count).by(-1)
203204
end
204205

205206
it 'redirects to the protips list' do
206207
protip = Protip.create!(valid_attributes)
207-
delete :destroy, { id: protip.to_param }, valid_session
208+
delete :destroy, { id: protip.to_param }
208209
expect(response).to redirect_to(protips_url)
209210
end
210211
end

spec/routing/protips_routing_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
RSpec.describe ProtipsController, type: :routing do
2+
describe 'routing' do
3+
it 'GET p/:id/:slug routes to #show' do
4+
expect(get('/p/1234/abcd')).to route_to(controller: 'protips', action: 'show', id: '1234', slug: 'abcd')
5+
end
6+
7+
it 'POST p/:id/upvote routes to #upvote' do
8+
expect(post('/p/abcd/upvote')).to route_to(controller: 'protips', action: 'upvote', id: 'abcd')
9+
end
10+
end
11+
end

0 commit comments

Comments
 (0)