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