metamaps--metamaps/spec/controllers/topics_controller_spec.rb

103 lines
3.1 KiB
Ruby
Raw Normal View History

require 'rails_helper'
2015-12-16 14:19:58 +00:00
RSpec.describe TopicsController, type: :controller do
2016-02-08 03:56:46 +00:00
let(:topic) { create(:topic) }
2016-02-09 05:42:24 +00:00
let(:valid_attributes) { topic.attributes.except('id') }
let(:invalid_attributes) { { permission: :invalid_lol } }
2016-02-08 03:56:46 +00:00
before :each do
sign_in
2015-12-16 14:19:58 +00:00
end
2015-12-16 14:19:58 +00:00
describe 'GET #show' do
it 'assigns the requested topic as @topic' do
2016-02-09 05:42:24 +00:00
get :show, { id: topic.to_param, format: :json }
expect(assigns(:topic)).to eq(topic)
end
end
2015-12-16 14:19:58 +00:00
describe 'POST #create' do
context 'with valid params' do
it 'creates a new Topic' do
2016-02-09 05:42:24 +00:00
topic.reload # ensure it's created
2015-12-16 14:19:58 +00:00
expect do
2016-02-09 05:42:24 +00:00
post :create, { topic: valid_attributes, format: :json }
2015-12-16 14:19:58 +00:00
end.to change(Topic, :count).by(1)
end
2015-12-16 14:19:58 +00:00
it 'assigns a newly created topic as @topic' do
2016-02-09 05:42:24 +00:00
post :create, { topic: valid_attributes, format: :json }
expect(assigns(:topic)).to be_a(Topic)
expect(assigns(:topic)).to be_persisted
end
2016-02-09 05:42:24 +00:00
it 'returns 201 CREATED' do
post :create, { topic: valid_attributes, format: :json }
expect(response.status).to eq 201
end
end
2015-12-16 14:19:58 +00:00
context 'with invalid params' do
it 'assigns a newly created but unsaved topic as @topic' do
2016-02-09 05:42:24 +00:00
post :create, { topic: invalid_attributes, format: :json }
expect(assigns(:topic)).to be_a_new(Topic)
end
end
end
2015-12-16 14:19:58 +00:00
describe 'PUT #update' do
context 'with valid params' do
let(:new_attributes) do
2016-02-09 05:42:24 +00:00
{ name: 'Cool Topic with no number',
desc: 'This is a cool topic.',
link: 'https://cool-topics.com/4',
permission: :public }
2015-12-16 14:19:58 +00:00
end
2015-12-16 14:19:58 +00:00
it 'updates the requested topic' do
2015-12-16 14:33:29 +00:00
put :update,
2016-02-09 05:42:24 +00:00
{ id: topic.to_param, topic: new_attributes, format: :json }
topic.reload
2016-02-09 05:42:24 +00:00
expect(topic.name).to eq 'Cool Topic with no number'
expect(topic.desc).to eq 'This is a cool topic.'
expect(topic.link).to eq 'https://cool-topics.com/4'
expect(topic.permission).to eq 'public'
end
2015-12-16 14:19:58 +00:00
it 'assigns the requested topic as @topic' do
2015-12-16 14:33:29 +00:00
put :update,
2016-02-09 05:42:24 +00:00
{ id: topic.to_param, topic: valid_attributes, format: :json }
expect(assigns(:topic)).to eq(topic)
end
2016-02-09 05:42:24 +00:00
it 'returns status of no content' do
2015-12-16 14:33:29 +00:00
put :update,
2016-02-09 05:42:24 +00:00
{ id: topic.to_param, topic: valid_attributes, format: :json }
expect(response.status).to eq 204
end
end
2015-12-16 14:19:58 +00:00
context 'with invalid params' do
it 'assigns the topic as @topic' do
2015-12-16 14:33:29 +00:00
put :update,
2016-02-09 05:42:24 +00:00
{ id: topic.to_param, topic: invalid_attributes, format: :json }
expect(assigns(:topic)).to eq(topic)
end
end
end
2015-12-16 14:19:58 +00:00
describe 'DELETE #destroy' do
2016-02-09 05:42:24 +00:00
let(:owned_topic) { create(:topic, user: controller.current_user) }
2015-12-16 14:19:58 +00:00
it 'destroys the requested topic' do
2016-02-09 05:42:24 +00:00
owned_topic.reload # ensure it's there
2015-12-16 14:19:58 +00:00
expect do
2016-02-09 05:42:24 +00:00
delete :destroy, { id: owned_topic.to_param, format: :json }
2015-12-16 14:19:58 +00:00
end.to change(Topic, :count).by(-1)
end
2016-02-09 05:42:24 +00:00
it 'return 204 NO CONTENT' do
delete :destroy, { id: topic.to_param, format: :json }
expect(response.status).to eq 204
end
end
end