require 'rails_helper' module Mks module Edm RSpec.describe EquipmentValuationsController, 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 { equipment_item_id: create(:equipment_item).id, initial_cost: 1.5, residual_value: 1.5, work_hrs_per_yr: 1.5, life_year: 1.5, insurance_premium: 1.5, fuel_cost_per_hr: 1.5, oil_rate: 1.5, repair_rate: 1.5, rate_of_return: 1.5, dep_method: FFaker::Name.name } end let(:invalid_attributes) do { equipment_item_id: create(:equipment_item).id, initial_cost: nil, residual_value: 1.5, work_hrs_per_yr: 1.5, life_year: 1.5, insurance_premium: 1.5, fuel_cost_per_hr: 1.5, oil_rate: 1.5, repair_rate: 1.5, rate_of_return: 1.5, dep_method: FFaker::Name.name } end describe 'GET #index' do it 'returns a success response' do ei = create(:equipment_item) 3.times { create(:equipment_valuation, equipment_item: ei) } 3.times { create(:equipment_valuation) } get :index, params: { id: ei.id } expect(response).to be_successful result = JSON(response.body) expect(result['data'].count).to eq 3 end end describe 'GET #valuation' do it 'returns a success response' do ei = create(:equipment_item) 2.times { create(:equipment_valuation, equipment_item: ei, is_current: false) } create(:equipment_valuation, equipment_item: ei, is_current: true) get :valuation, params: { id: ei.id } expect(response).to be_successful result = JSON(response.body) expect(result['data'][0]['is_current']).to be_truthy end end describe 'GET #show' do it 'returns a success response' do ei = create(:equipment_valuation) get :show, params: { id: ei.to_param } expect(response).to be_successful end end describe 'POST #create' do context 'with valid params' do it 'creates a new EquipmentValuation' do expect do post :create, params: { equipment_valuation: valid_attributes } end.to change(EquipmentValuation, :count).by(1) end it 'renders a JSON response with the new equipment_valuation' do post :create, params: { equipment_valuation: valid_attributes } expect(response).to have_http_status(:created) expect(response.content_type).to eq('application/json') expect(response.location).to eq(equipment_valuation_url(EquipmentValuation.last)) end end context 'with invalid params' do it 'renders a JSON response with errors for the new equipment_valuation' do post :create, params: { equipment_valuation: 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 { initial_cost: 2000 } end it 'updates the requested equipment_valuation' do ei = EquipmentValuation.create! valid_attributes put :update, params: { id: ei.to_param, equipment_valuation: new_attributes } ei.reload expect(ei.initial_cost).to eq new_attributes[:initial_cost] end it 'renders a JSON response with the equipment_valuation' do ei = create(:equipment_valuation) put :update, params: { id: ei.to_param, equipment_valuation: 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_valuation' do ei = create(:equipment_valuation) put :update, params: { id: ei.to_param, equipment_valuation: invalid_attributes } expect(response).to have_http_status(:unprocessable_entity) expect(response.content_type).to eq('application/json') end end end end end end