require 'rails_helper' module Mks module Edm RSpec.describe EquipmentCategoriesController, type: :controller do routes { Mks::Edm::Engine.routes } before(:each) do u = create(:user) token = Mks::Auth::TokenAuth.issue(name: u.full_name, email: u.email, id: u.id) request.headers['Authorization'] = "Bearer #{token}" end let(:valid_attributes) do { code: FFaker::Name.name, name: FFaker::Name.name, description: FFaker::Lorem.paragraph, equipment_type_id: create(:equipment_type).id } end let(:invalid_attributes) do { code: nil, name: FFaker::Name.name, description: FFaker::Lorem.paragraph, equipment_type_id: create(:equipment_type).id } end describe 'GET #index' do it 'returns a success response' do et = create(:equipment_type) 3.times { create(:equipment_category, equipment_type: et) } 3.times { create(:equipment_category) } get :index, params: { id: et.id } expect(response).to be_successful result = JSON(response.body) expect(result['data'].count).to eq 3 end end describe 'GET #show' do it 'returns a success response' do equipment_category = create(:equipment_category) get :show, params: { id: equipment_category.to_param } expect(response).to be_successful end end describe 'POST #create' do context 'with valid params' do it 'creates a new EquipmentCategory' do expect do post :create, params: { equipment_category: valid_attributes } end.to change(EquipmentCategory, :count).by(1) end it 'renders a JSON response with the new equipment_category' do post :create, params: { equipment_category: valid_attributes } expect(response).to have_http_status(:created) expect(response.content_type).to eq('application/json') expect(response.location).to eq(equipment_category_url(EquipmentCategory.last)) end end context 'with invalid params' do it 'renders a JSON response with errors for the new equipment_category' do post :create, params: { equipment_category: invalid_attributes } expect(response).to have_http_status(:unprocessable_entity) expect(response.content_type).to eq('application/json') end end end describe 'PUT #update' do context 'with valid params' do let(:new_attributes) do { name: FFaker::Name.name } end it 'updates the requested equipment_category' do equipment_category = create(:equipment_category) put :update, params: { id: equipment_category.to_param, equipment_category: new_attributes } equipment_category.reload expect(equipment_category.name).to eq new_attributes[:name] end it 'renders a JSON response with the equipment_category' do equipment_category = create(:equipment_category) put :update, params: { id: equipment_category.to_param, equipment_category: valid_attributes } expect(response).to have_http_status(:ok) expect(response.content_type).to eq('application/json') end end context 'with invalid params' do it 'renders a JSON response with errors for the equipment_category' do equipment_category = create(:equipment_category) put :update, params: { id: equipment_category.to_param, equipment_category: invalid_attributes } expect(response).to have_http_status(:unprocessable_entity) expect(response.content_type).to eq('application/json') end end end end end end