topics controller spec
This commit is contained in:
parent
7f50db1ae9
commit
0c81c8d9de
1 changed files with 30 additions and 21 deletions
|
@ -2,15 +2,15 @@ require 'rails_helper'
|
||||||
|
|
||||||
RSpec.describe TopicsController, type: :controller do
|
RSpec.describe TopicsController, type: :controller do
|
||||||
let(:topic) { create(:topic) }
|
let(:topic) { create(:topic) }
|
||||||
let(:valid_attributes) { topic.attributes.except(:id) }
|
let(:valid_attributes) { topic.attributes.except('id') }
|
||||||
let(:invalid_attributes) { { permission: :commons } }
|
let(:invalid_attributes) { { permission: :invalid_lol } }
|
||||||
before :each do
|
before :each do
|
||||||
sign_in
|
sign_in
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'GET #show' do
|
describe 'GET #show' do
|
||||||
it 'assigns the requested topic as @topic' do
|
it 'assigns the requested topic as @topic' do
|
||||||
get :show, { id: topic.to_param }
|
get :show, { id: topic.to_param, format: :json }
|
||||||
expect(assigns(:topic)).to eq(topic)
|
expect(assigns(:topic)).to eq(topic)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -18,26 +18,27 @@ RSpec.describe TopicsController, type: :controller do
|
||||||
describe 'POST #create' do
|
describe 'POST #create' do
|
||||||
context 'with valid params' do
|
context 'with valid params' do
|
||||||
it 'creates a new Topic' do
|
it 'creates a new Topic' do
|
||||||
|
topic.reload # ensure it's created
|
||||||
expect do
|
expect do
|
||||||
post :create, { topic: valid_attributes }
|
post :create, { topic: valid_attributes, format: :json }
|
||||||
end.to change(Topic, :count).by(1)
|
end.to change(Topic, :count).by(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'assigns a newly created topic as @topic' do
|
it 'assigns a newly created topic as @topic' do
|
||||||
post :create, { topic: valid_attributes }
|
post :create, { topic: valid_attributes, format: :json }
|
||||||
expect(assigns(:topic)).to be_a(Topic)
|
expect(assigns(:topic)).to be_a(Topic)
|
||||||
expect(assigns(:topic)).to be_persisted
|
expect(assigns(:topic)).to be_persisted
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'redirects to the created topic' do
|
it 'returns 201 CREATED' do
|
||||||
post :create, { topic: valid_attributes }
|
post :create, { topic: valid_attributes, format: :json }
|
||||||
expect(response).to redirect_to(Topic.last)
|
expect(response.status).to eq 201
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'with invalid params' do
|
context 'with invalid params' do
|
||||||
it 'assigns a newly created but unsaved topic as @topic' do
|
it 'assigns a newly created but unsaved topic as @topic' do
|
||||||
post :create, { topic: invalid_attributes }
|
post :create, { topic: invalid_attributes, format: :json }
|
||||||
expect(assigns(:topic)).to be_a_new(Topic)
|
expect(assigns(:topic)).to be_a_new(Topic)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -46,48 +47,56 @@ RSpec.describe TopicsController, type: :controller do
|
||||||
describe 'PUT #update' do
|
describe 'PUT #update' do
|
||||||
context 'with valid params' do
|
context 'with valid params' do
|
||||||
let(:new_attributes) do
|
let(:new_attributes) do
|
||||||
skip('Add a hash of attributes valid for your model')
|
{ name: 'Cool Topic with no number',
|
||||||
|
desc: 'This is a cool topic.',
|
||||||
|
link: 'https://cool-topics.com/4',
|
||||||
|
permission: :public }
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'updates the requested topic' do
|
it 'updates the requested topic' do
|
||||||
put :update,
|
put :update,
|
||||||
{ id: topic.to_param, topic: new_attributes }
|
{ id: topic.to_param, topic: new_attributes, format: :json }
|
||||||
topic.reload
|
topic.reload
|
||||||
skip('Add assertions for updated state')
|
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
|
end
|
||||||
|
|
||||||
it 'assigns the requested topic as @topic' do
|
it 'assigns the requested topic as @topic' do
|
||||||
put :update,
|
put :update,
|
||||||
{ id: topic.to_param, topic: valid_attributes }
|
{ id: topic.to_param, topic: valid_attributes, format: :json }
|
||||||
expect(assigns(:topic)).to eq(topic)
|
expect(assigns(:topic)).to eq(topic)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'redirects to the topic' do
|
it 'returns status of no content' do
|
||||||
put :update,
|
put :update,
|
||||||
{ id: topic.to_param, topic: valid_attributes }
|
{ id: topic.to_param, topic: valid_attributes, format: :json }
|
||||||
expect(response).to redirect_to(topic)
|
expect(response.status).to eq 204
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'with invalid params' do
|
context 'with invalid params' do
|
||||||
it 'assigns the topic as @topic' do
|
it 'assigns the topic as @topic' do
|
||||||
put :update,
|
put :update,
|
||||||
{ id: topic.to_param, topic: invalid_attributes }
|
{ id: topic.to_param, topic: invalid_attributes, format: :json }
|
||||||
expect(assigns(:topic)).to eq(topic)
|
expect(assigns(:topic)).to eq(topic)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'DELETE #destroy' do
|
describe 'DELETE #destroy' do
|
||||||
|
let(:owned_topic) { create(:topic, user: controller.current_user) }
|
||||||
it 'destroys the requested topic' do
|
it 'destroys the requested topic' do
|
||||||
|
owned_topic.reload # ensure it's there
|
||||||
expect do
|
expect do
|
||||||
delete :destroy, { id: topic.to_param }
|
delete :destroy, { id: owned_topic.to_param, format: :json }
|
||||||
end.to change(Topic, :count).by(-1)
|
end.to change(Topic, :count).by(-1)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'redirects to the topics list' do
|
it 'return 204 NO CONTENT' do
|
||||||
delete :destroy, { id: topic.to_param }
|
delete :destroy, { id: topic.to_param, format: :json }
|
||||||
expect(response).to redirect_to(topics_url)
|
expect(response.status).to eq 204
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue