Skip to content

Instantly share code, notes, and snippets.

@mrjonesbot
Created December 11, 2018 22:03
Show Gist options
  • Save mrjonesbot/11f9ccaa9c565f172797b353030bdf36 to your computer and use it in GitHub Desktop.
Save mrjonesbot/11f9ccaa9c565f172797b353030bdf36 to your computer and use it in GitHub Desktop.
require 'rails_helper'
RSpec.describe 'Answer', type: :request do
let!(:student) { create(:student) }
let!(:answer) { create(:correct_option_answer) }
let!(:question) { answer.question }
let!(:section_attempt) { answer.section_attempt }
let(:answer_id) { answer.id }
describe 'GET /answers/:id' do
before do
get_request("/answers/#{answer_id}", user: student)
end
context 'when answer exists' do
it 'returns status code 200' do
expect(response).to have_http_status(200)
end
it 'returns the answer' do
expect(json['data']['id'].to_i).to eq(answer_id)
end
end
context 'when answer does not exist' do
let(:answer_id) { 0 }
it 'returns status code 404' do
expect(response).to have_http_status(404)
end
it 'returns a not found message' do
expect(response.body).to match(/Couldn't find Answer/)
end
end
end
describe 'POST /answers' do
let(:section_attempt_id) { section_attempt.id }
let(:question_id) { question.id }
let(:free_text) { 'The pen is mightier than the sword!'}
let(:valid_params) {
{ answer: { section_attempt_id: section_attempt_id, question_id: question_id, free_text: free_text} }
}
let(:invalid_params) {
{ answer: { free_text: free_text }}
}
let!(:validation_error) {
"Validation failed: Section attempt must exist, Question must exist"
}
context 'when request attributes are valid' do
let(:request) {
post_request('/answers',
user: student, params: valid_params)
}
it 'returns status code 200' do
request
expect(response).to have_http_status(200)
end
it 'creates a new answer' do
expect{ request }.to change{ Answer.count }.by 1
end
it 'returns the created answer' do
request
expect(json['data']['type']).to eq('answer')
end
end
context 'when request attributes are invalid' do
let(:request) {
post_request('/answers',
user: student, params: invalid_params)
}
it 'returns status code 422' do
request
expect(response).to have_http_status(422)
end
it 'returns a failure message' do
request
expect(json['errors']).to eq(validation_error)
end
end
end
describe 'PATCH /answers/:id' do
let(:new_answer) { 'Every memory has a piece of its artist.' }
before do
params = { answer: { free_text: new_answer }}
patch_request("/answers/#{answer_id}",
user: student, params: params)
end
context 'when item exists' do
it 'returns status code 200' do
expect(response).to have_http_status(200)
end
it 'updates the answer' do
updated_answer = Answer.find(answer_id)
expect(updated_answer.free_text).to eq(new_answer)
end
end
context 'when the answer does not exist' do
let(:answer_id) { 0 }
it 'returns status code 404' do
expect(response).to have_http_status(404)
end
it 'returns a not found message' do
expect(response.body).to match(/Couldn't find Answer/)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment