Skip to content

Instantly share code, notes, and snippets.

@jramiresbrito
Created November 28, 2020 09:53
Show Gist options
  • Save jramiresbrito/12d96df14b503f8eb560d83be0158925 to your computer and use it in GitHub Desktop.
Save jramiresbrito/12d96df14b503f8eb560d83be0158925 to your computer and use it in GitHub Desktop.
Request test for Category
module Admin::V1
class ApiController < ApplicationController
include Authenticable
def render_error(message: nil, fields: nil, status: :unprocessable_entity)
errors = {}
errors['fields'] = fields if fields.present?
errors['message'] = message if message.present?
render json: { errors: errors }, status: status
end
end
end
module Admin::V1
class CategoriesController < ApiController
def index
@categories = Category.all
end
def create
@category = Category.new(category_params)
save_category!
end
private
class ApiError < StandardError; end
def category_params
return {} unless params.key?(:category)
params.require(:category).permit(:id, :name)
end
def save_category!
@category.save!
render :show
rescue ApiError
render_error(fields: @category.errors.messages)
end
end
end
require 'rails_helper'
RSpec.describe "Admin V1 Categories", type: :request do
let(:user) { create(:user) }
context "GET /categories" do
let(:url) { "/admin/v1/categories" }
let!(:categories) { create_list(:category, 5) }
before { get url, headers: auth_header(user) }
it "returns all Categories" do
expect(json_body['categories']).to contain_exactly(*categories.as_json(only: %i[id name]))
end
it "should return success status" do
expect(response).to have_http_status(:ok)
end
end
context 'POST /categories' do
let(:url) { "/admin/v1/categories" }
context "valid params" do
let(:category_params) { { category: attributes_for(:category) }.to_json }
it "should add a new Category" do
expect do
post url, headers: auth_header(user), params: category_params
end.to change(Category, :count).by(1)
end
it "should return the last added Category" do
post url, headers: auth_header(user), params: category_params
expect_category = Category.last.as_json(only: %i[id name])
expect(json_body['category']).to eq expect_category
end
it "should return success status" do
post url, headers: auth_header(user), params: category_params
expect(response).to have_http_status(:ok)
end
end
context "invalid params" do
let(:category_invalid_params) do
{ category: attributes_for(:category, name: nil) }.to_json
end
it "shouldn't add a new Category" do
expect do
post url, headers: auth_header(user), params: category_invalid_params
end.to_not change(Category, :count)
end
it "should return error messages" do
post url, headers: auth_header(user), params: category_invalid_params
expect(json_body['errors']['fields']).to have_key('name')
end
it "should return unprocessable_entity - status code 422" do
post url, headers: auth_header(user), params: category_invalid_params
expect(body_json['errors']['fields']).to have_key('name')
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment