require 'rails_helper' module Mks module Edm RSpec.describe EquipmentItemsController, 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, serial_number: FFaker::Name.name, brand: FFaker::Name.name, item_model: FFaker::Name.name, purchase_price: 1000, purchase_date: Date.new, equipment_id: create(:equipment).id, current_location_id: create(:equipment_location).id } end let(:invalid_attributes) do { code: nil, name: FFaker::Name.name, description: FFaker::Lorem.paragraph, serial_number: FFaker::Name.name, brand: FFaker::Name.name, item_model: FFaker::Name.name, purchase_price: 1000, purchase_date: Date.new, equipment_id: create(:equipment).id, current_location_id: create(:equipment_location).id } end describe 'GET #index' do it 'returns a success response' do eq = create(:equipment) create(:equipment_item, equipment: eq) get :index, params: { id: eq.id } expect(response).to be_successful end end describe 'GET #show' do it 'returns a success response' do equipment_item = create(:equipment_item) get :show, params: { id: equipment_item.to_param } expect(response).to be_successful end end describe 'POST #create' do context 'with valid params' do it 'creates a new EquipmentItem' do expect do post :create, params: { equipment_item: valid_attributes } end.to change(EquipmentItem, :count).by(1) end it 'renders a JSON response with the new equipment_item' do post :create, params: { equipment_item: valid_attributes } expect(response).to have_http_status(:created) expect(response.content_type).to eq('application/json') expect(response.location).to eq(equipment_item_url(EquipmentItem.last)) end end context 'with invalid params' do it 'renders a JSON response with errors for the new equipment_item' do post :create, params: { equipment_item: 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_item' do equipment_item = create(:equipment_item) put :update, params: { id: equipment_item.to_param, equipment_item: new_attributes } equipment_item.reload expect(equipment_item.name).to eq new_attributes[:name] end it 'renders a JSON response with the equipment_item' do equipment_item = create(:equipment_item) put :update, params: { id: equipment_item.to_param, equipment_item: 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_item' do equipment_item = create(:equipment_item) put :update, params: { id: equipment_item.to_param, equipment_item: invalid_attributes } expect(response).to have_http_status(:unprocessable_entity) expect(response.content_type).to eq('application/json') end end end end end end