require 'rails_helper' module Mks module Edm RSpec.describe LocationTypesController, 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) { { code: FFaker::Name.unique.name, name: FFaker::Name.name, description: FFaker::Name.name } } let(:invalid_attributes) { { code: nil, name: nil, description: nil } } describe 'GET #index' do it 'returns all location types' do 3.times { create(:location_type) } get :index result = JSON(response.body) expect(result['data'].count).to eq 3 end end describe 'POST #create' do context 'with valid params' do it 'creates a new LocationType' do expect { post :create, params: { location_type: valid_attributes }, format: :json }.to change(LocationType, :count).by(1) end it 'returns a success message' do post :create, params: { location_type: valid_attributes }, format: :json result = JSON(response.body) expect(result['message']).to eq('Location type saved successfully !') end end context 'with invalid params' do it 'returns error message(s)' do post :create, params: { location_type: invalid_attributes }, format: :json result = JSON(response.body) expect(result['code'].count).to be > 0 end end end describe 'PUT #update' do context 'with valid params' do it 'updates the requested location type' do lt = create(:location_type) put :update, params: { id: lt.id, location_type: { code: 'LT1' } }, format: :json lt.reload expect(lt.code).to eq('LT1') end it 'returns a success message' do lt = create(:location_type) put :update, params: { id: lt.id, location_type: { code: 'LT1' } }, format: :json result = JSON(response.body) expect(result['message']).to eq('Location type updated successfully !') end end context 'with invalid params' do it 'returns error message(s)' do lt = create(:location_type) put :update, params: { id: lt.id, location_type: { code: nil } }, format: :json result = JSON(response.body) expect(result['code'].count).to be > 0 end end end end end end