require 'rails_helper' module Logistics module Core RSpec.describe UnitTypesController, type: :controller do routes { Logistics::Core::Engine.routes } let(:valid_attributes) { { code: FFaker::Name.name, name: FFaker::Name.name } } let(:invalid_attributes) { { code: '', name: FFaker::Name.name } } describe "GET #index" do it "assigns all unit types as @unit_types" do ut = create(:unit_type) get :index, format: :json result = JSON(response.body) expect(result['data'][0]['code']).to eq ut.code expect(result['data'][0]['name']).to eq ut.name end end describe "POST #create" do context "with valid params" do it "creates a new unit type" do expect { post :create, params: { :unit_type => valid_attributes }, format: :json }.to change(UnitType, :count).by(1) end it "should return a success message" do post :create, params: { :unit_type => valid_attributes }, format: :json result = JSON(response.body) expect(result['message']).to eq('Unit type saved successfully!') end end context "with invalid params" do it 'should return an error message' do post :create, params: { :unit_type => invalid_attributes }, format: :json result = JSON(response.body) expect(result['errors']).to include("Unit type Code can't be blank") end end end describe "PUT #update" do context "with valid params" do let(:new_attributes) { { name: FFaker::Name.name } } it "updates the requested unit type" do unit_type = create(:unit_type) old_data = unit_type.name put :update, params: { :id => unit_type.to_param, :unit_type => new_attributes }, format: :json unit_type.reload expect(unit_type.name).not_to eq old_data end it "should return a success message" do unit_type = create(:unit_type) put :update, params: { :id => unit_type.to_param, :unit_type => valid_attributes }, format: :json result = JSON(response.body) expect(result['message']).to eq('Unit type updated successfully!') end end context "with invalid params" do it "should return an error message" do unit_type = create(:unit_type) put :update, params: { :id => unit_type.to_param, :unit_type => invalid_attributes }, format: :json result = JSON(response.body) expect(result['errors']).to include("Unit type Code can't be blank") end end end end end end