require 'rails_helper' module Mks module Rate RSpec.describe ServiceTypesController, type: :controller do routes { Mks::Rate::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.unique.name, name: FFaker::Name.name } end let(:invalid_attributes) do { code: nil, name: FFaker::Name.name } end describe 'GET #index' do it 'returns a success response' do create(:service_type) get :index expect(response).to be_successful end end describe 'GET #show' do it 'returns a success response' do service_type = create(:service_type) get :show, params: { id: service_type.to_param } expect(response).to be_successful end end describe 'POST #create' do context 'with valid params' do it 'creates a new ServiceType' do expect do post :create, params: { service_type: valid_attributes } end.to change(ServiceType, :count).by(1) end it 'renders a JSON response with the new service_type' do post :create, params: { service_type: valid_attributes } expect(response).to have_http_status(:created) expect(response.content_type).to eq('application/json') expect(response.location).to eq(service_type_url(ServiceType.last)) end end context 'with invalid params' do it 'renders a JSON response with errors for the new service_type' do post :create, params: { service_type: 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 service_type' do service_type = create(:service_type) put :update, params: { id: service_type.to_param, service_type: new_attributes } service_type.reload expect(service_type.name).to eq new_attributes[:name] end it 'renders a JSON response with the service_type' do service_type = create(:service_type) put :update, params: { id: service_type.to_param, service_type: 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 service_type' do service_type = create(:service_type) put :update, params: { id: service_type.to_param, service_type: invalid_attributes } expect(response).to have_http_status(:unprocessable_entity) expect(response.content_type).to eq('application/json') end end end end end end