Skip to content

Fix protip controller specs, add routing specs #297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/models/protip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class Protip < ActiveRecord::Base
validates :body, presence: true
validates :kind, presence: true, inclusion: { in: KINDS }
validates :topic_list, length: { minimum: 1 }
validates :slug, presence: true

after_validation :tag_user
before_create :assign_random_id
Expand Down
65 changes: 33 additions & 32 deletions spec/controllers/protips_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RSpec.describe ProtipsController, type: :controller, skip: true do
RSpec.describe ProtipsController, type: :controller do
let(:current_user) { Fabricate(:user) }

before { controller.send :sign_in, current_user }
Expand All @@ -7,23 +7,19 @@ def valid_attributes
{
title: 'hello world',
body: "somethings that's meaningful and nice",
topics: %w(java javascript),
topic_list: "java, javascript",
user_id: current_user.id
}
end

def valid_session
{}
end

describe 'GET user' do
describe 'banned' do
it 'should assign user @protips for page, despite not being in search index' do
current_user.update_attribute(:banned_at, Time.now)
expect(current_user.banned?).to eq(true)
Protip.rebuild_index
protip = Protip.create! valid_attributes
get :user, { username: current_user.username }, valid_session
get :user, { username: current_user.username }
expect(assigns(:protips).first.title).to eq(protip.title)
end
end
Expand All @@ -32,28 +28,33 @@ def valid_session
it 'should assign user @protips for page' do
Protip.rebuild_index
protip = Protip.create! valid_attributes
get :user, { username: current_user.username }, valid_session
get :user, { username: current_user.username }
expect(assigns(:protips).results.first.title).to eq(protip.title)
end

end

end

# describe "GET topic" do
# it "assigns all protips as @protips" do
# Protip.rebuild_index
# protip = Protip.create! valid_attributes
# get :topic, {tags: "java"}, valid_session
# get :topic, {tags: "java"}
# expect(assigns(:protips).results.first.title).to eq(protip.title)
# end
# end

describe 'GET show using public_id' do
it 'redirects to GET show using slug' do
it 'redirects to GET show if slug is empty' do
protip = Protip.create! valid_attributes
protip.save
get :show, { id: protip.to_param }
expect(response).to redirect_to slug_protips_path(protip, protip.friendly_id)
end

it 'redirects to GET show if slug is invalid' do
protip = Protip.create! valid_attributes
protip.save
get :show, { id: protip.to_param }, valid_session
get :show, { id: protip.to_param, slug: "an_invalid_slug" }
expect(response).to redirect_to slug_protips_path(protip, protip.friendly_id)
end
end
Expand All @@ -62,7 +63,7 @@ def valid_session
it 'assigns the requested protip as @protip' do
protip = Protip.create! valid_attributes
protip.save
get :show, { id: protip.public_id, slug: protip.friendly_id }, valid_session
get :show, { id: protip.public_id, slug: protip.friendly_id }
expect(assigns(:protip)).to eq(protip)
end
end
Expand All @@ -71,26 +72,26 @@ def valid_session
before { allow_any_instance_of(User).to receive(:skills).and_return(['skill']) } # User must have a skill to create protips

it 'assigns a new protip as @protip' do
get :new, {}, valid_session
get :new, {}
expect(assigns(:protip)).to be_a_new(Protip)
end

it 'allows viewing the page when you have a skill' do
get :new, {}, valid_session
get :new, {}
expect(response).to render_template('new')
end

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

describe 'GET edit' do
it 'assigns the requested protip as @protip' do
protip = Protip.create! valid_attributes
get :edit, { id: protip.to_param }, valid_session
get :edit, { id: protip.to_param }
expect(assigns(:protip)).to eq(protip)
end
end
Expand All @@ -101,18 +102,18 @@ def valid_session
describe 'with valid params' do
it 'creates a new Protip' do
expect do
post :create, { protip: valid_attributes }, valid_session
post :create, { protip: valid_attributes }
end.to change(Protip, :count).by(1)
end

it 'assigns a newly created protip as @protip' do
post :create, { protip: valid_attributes }, valid_session
post :create, { protip: valid_attributes }
expect(assigns(:protip)).to be_a(Protip)
expect(assigns(:protip)).to be_persisted
end

it 'redirects to the created protip' do
post :create, { protip: valid_attributes }, valid_session
post :create, { protip: valid_attributes }
expect(response).to redirect_to(Protip.last)
end
end
Expand All @@ -121,21 +122,21 @@ def valid_session
it 'assigns a newly created but unsaved protip as @protip' do
# Trigger the behavior that occurs when invalid params are submitted
allow_any_instance_of(Protip).to receive(:save).and_return(false)
post :create, { protip: {} }, valid_session
post :create, { protip: {} }
expect(assigns(:protip)).to be_a_new(Protip)
end

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

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

it 'assigns the requested protip as @protip' do
protip = Protip.create! valid_attributes
put :update, { id: protip.to_param, protip: valid_attributes }, valid_session
put :update, { id: protip.to_param, protip: valid_attributes }
expect(assigns(:protip)).to eq(protip)
end

it 'redirects to the protip' do
protip = Protip.create! valid_attributes
put :update, { id: protip.to_param, protip: valid_attributes }, valid_session
put :update, { id: protip.to_param, protip: valid_attributes }
expect(response).to redirect_to(protip)
end
end
Expand All @@ -170,7 +171,7 @@ def valid_session
protip = Protip.create! valid_attributes
# Trigger the behavior that occurs when invalid params are submitted
allow_any_instance_of(Protip).to receive(:save).and_return(false)
put :update, { id: protip.to_param, protip: {} }, valid_session
put :update, { id: protip.to_param, protip: {} }
expect(assigns(:protip)).to eq(protip)
end

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

put :update, { id: protip.to_param, protip: {} }, valid_session
put :update, { id: protip.to_param, protip: {} }
expect(response).to render_template('edit')
end
end
Expand All @@ -191,20 +192,20 @@ def valid_session
attributes = valid_attributes
attributes[:user_id] = Fabricate(:user).id
protip = Protip.create! attributes
delete :destroy, { id: protip.to_param }, valid_session
delete :destroy, { id: protip.to_param }
expect { protip.reload }.not_to raise_error
end

it 'destroys the requested protip' do
protip = Protip.create! valid_attributes
expect {
delete :destroy, { id: protip.to_param }, valid_session
delete :destroy, { id: protip.to_param }
}.to change(Protip, :count).by(-1)
end

it 'redirects to the protips list' do
protip = Protip.create!(valid_attributes)
delete :destroy, { id: protip.to_param }, valid_session
delete :destroy, { id: protip.to_param }
expect(response).to redirect_to(protips_url)
end
end
Expand Down
11 changes: 11 additions & 0 deletions spec/routing/protips_routing_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
RSpec.describe ProtipsController, type: :routing do
describe 'routing' do
it 'GET p/:id/:slug routes to #show' do
expect(get('/p/1234/abcd')).to route_to(controller: 'protips', action: 'show', id: '1234', slug: 'abcd')
end

it 'POST p/:id/upvote routes to #upvote' do
expect(post('/p/abcd/upvote')).to route_to(controller: 'protips', action: 'upvote', id: 'abcd')
end
end
end