Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Evanto/0deba3f4e50dd795ee35c5c91578a4e9 to your computer and use it in GitHub Desktop.
Save Evanto/0deba3f4e50dd795ee35c5c91578a4e9 to your computer and use it in GitHub Desktop.
require 'rails_helper'
RSpec.describe QuestionsController, type: :controller do
sign_in_user
let(:question) { create(:question, user: user) }
let(:user) { create(:user) }
describe 'GET #index' do
let(:questions) { create_list(:question, 2) }
before { get :index }
it 'populates an array of all questions' do
expect(assigns(:questions)).to match_array(questions)
end
it 'renders index view' do
expect(response).to render_template :index
end
end
describe 'GET #show' do
before { get :show, params: { id: question} }
it 'assigns the requested question to @question' do
expect(assigns(:question)).to eq question
end
it 'renders show view' do
expect(response).to render_template :show
end
end
describe 'GET #new' do
before { get :new }
it 'assigns a new Question to @question' do
expect(assigns(:question)).to be_a_new(Question)
end
it 'renders new view' do
expect(response).to render_template :new
end
end
describe 'GET #edit' do
before { get :edit, params: { id: question} }
it 'assign the requested question to @question' do
expect(assigns(:question)).to eq question
end
it 'renders edit view' do
expect(response).to render_template :edit
end
end
describe 'POST #create' do
context '1) with valid attributes' do
it 'saves the new question to the db' do
expect { post :create, params: { user_id: user, question: attributes_for(:question) } }.to change(Question, :count).by(1)
end
it 'redirects to show view' do
post :create, params: { question: attributes_for(:question) }
expect(response).to redirect_to (assigns(:question)) #
end
end
end
context '2) with invalid attributes' do
it 'does not save the question' do
expect { post :create, params: { question: attributes_for(:invalid_question) } }.to_not change(Question, :count)
end
it 're-renders new view' do
post :create, params: { question: attributes_for(:invalid_question) }
expect(response).to render_template :new
end
end
end
describe 'DELETE #destroy' do
before { question }
context '1) autheticated user deletes his question' do
let(:question) { create(:question, user: @user) }
it 'deletes question' do
expect { delete :destroy, params: { id: question } }.to change(Question, :count).by(-1)
end
it 'redirects to index view' do
delete :destroy, params: { id: question }
expect(response).to redirect_to questions_path
end
end
context '2) authenticated user tries to delete some other users question' do
let(:question) { create(:question) }
it 'tries to delete question' do
expect { delete :destroy, params: { id: question } }.to_not change(Question, :count)
end
end
end
require 'rails_helper'
RSpec.describe AnswersController, type: :controller do
sign_in_user
let(:question) { create(:question) }
let(:answer) { create(:answer) }
describe 'DELETE #destroy' do
before { answer }
context "1) user deletes his answer" do
it 'deletes answer' do
expect { delete :destroy, params: { question_id: question, id: answer } }
.to change(Answer, :count).by(-1)
end
it 'redirects to question' do
delete :destroy, params: { question_id: question, id: answer }
expect(response).to be_successful
end
end
end
context '2) tries to delete answer which he is not the author of' do
let(:answer) { create(:answer, question: question) }
it 'does not delete the answer' do
expect { delete :destroy, params: { question_id: question, id: answer } }
.to_not change(Answer, :count)
end
end
describe 'POST #create' do
context '1) with valid attributes' do
it 'saves new users answer to the db' do
expect { post :create, params: { question_id: question, answer: attributes_for(:answer) } }.to change(question.answers, :count).by(1)
end
it 'creates and saves new answer to db for a logged in user' do
expect { post :create, params: { question_id: question.id, answer: attributes_for(:answer) } }.to change(@user.answers, :count).by(1)
end
it 'redirects to show view of a question' do
post :create, params: { question_id: question,
answer: attributes_for(:answer) }
expect(response).to redirect_to question_path(question)
end
end
context '2) with invalid attributes' do
it 'does not save new anwser to db' do
expect { post :create, params: { question_id: question,
answer: attributes_for(:invalid_answer) } }.to_not change(Answer, :count)
end
it 're-renders new view' do
post :create, params: { question_id: question,
answer: attributes_for(:invalid_answer) }
expect(response).to render_template 'questions/show'
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment