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

107 lines
3 KiB
Ruby
Raw Permalink Normal View History

2016-09-24 03:00:46 +00:00
# frozen_string_literal: true
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 create(:user)
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
post :create, format: :json, params: {
topic: valid_attributes
}
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
post :create, format: :json, params: {
topic: valid_attributes
}
expect(comparable(Topic.last)).to eq comparable(topic)
end
it 'returns 201 CREATED' do
post :create, format: :json, params: {
topic: valid_attributes
}
2016-02-09 05:42:24 +00:00
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
post :create, format: :json, params: {
topic: invalid_attributes
}
expect(Topic.count).to eq 0
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
put :update, format: :json, params: {
id: topic.to_param, topic: new_attributes
}
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
put :update, format: :json, params: {
id: topic.to_param, topic: valid_attributes
}
expect(Topic.last).to eq(topic)
end
it 'returns status of no content' do
put :update, format: :json, params: {
id: topic.to_param, topic: valid_attributes
}
2016-02-09 05:42:24 +00:00
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
put :update, format: :json, params: {
id: topic.to_param, topic: invalid_attributes
}
expect(Topic.last).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
delete :destroy, format: :json, params: {
id: owned_topic.to_param
}
2015-12-16 14:19:58 +00:00
end.to change(Topic, :count).by(-1)
2016-03-14 03:46:45 +00:00
expect(response.body).to eq ''
2016-02-09 05:42:24 +00:00
expect(response.status).to eq 204
end
end
end