require 'rails_helper' module Logistics module Core RSpec.describe AgencyPermitDocumentsController, type: :controller do routes { Logistics::Core::Engine.routes } let(:document_type_category) { create(:document_type_category) } let(:document_type_origin) { create(:document_type_origin) } let(:document_type) { create(:document_type) } let(:agency_category) { create(:agency_category) } let(:agency){ create(:agency) } describe "GET #index" do it "assigns all agency_permit_documents as @agency_permit_documents" do agency_permit_document = create(:agency_permit_document, agency: agency, document_type: document_type) get :index, params: { :agency_id => agency.to_param }, format: :json result = JSON(response.body) expect(result['data'][0]['code']).to eq agency_permit_document.document_type.code expect(result['data'][0]['name']).to eq agency_permit_document.document_type.name expect(result['data'][0]['document_type_category_id']).to eq agency_permit_document.document_type.document_type_category_id expect(result['data'][0]['document_type_origin_id']).to eq agency_permit_document.document_type.document_type_origin_id end end describe "POST #create" do context "with valid params" do it "creates a new AgencyPermitDocument" do data = {:document_type_id => document_type.id, :agency_id => agency.id} expect { post :create, params: { :agency_permit_document => data }, format: :json }.to change(AgencyPermitDocument, :count).by(1) end it "returns a success message" do data = {:document_type_id => document_type.id, :agency_id => agency.id} post :create, params: { :agency_permit_document => data }, format: :json result = JSON(response.body) expect(result['message']).to eq "Agency Permit Document successfully saved!" end end context "with invalid params" do it "returns an error message" do data = {:document_type_id => nil, :agency_id => agency.id} post :create, params: { :agency_permit_document => data }, format: :json result = JSON(response.body) expect(result['errors']).to include("Agency Permit Document Document type can't be blank") end end end describe "PUT #update" do context "with valid params" do it "updates the requested agency_permit_document" do new_document = create(:document_type) data = {:document_type_id => new_document.id, :agency_id => agency.id} agency_permit_document = AgencyPermitDocument.create(document_type: document_type, agency: agency) put :update, params: { :id => agency_permit_document.to_param, :agency_permit_document => data }, format: :json agency_permit_document.reload expect(agency_permit_document.document_type_id).not_to eq document_type.id end it "returns a success message" do new_document = create(:document_type) data = {:document_type_id => new_document.id, :agency_id => agency.id} agency_permit_document = AgencyPermitDocument.create(document_type: document_type, agency: agency) put :update, params: { :id => agency_permit_document.to_param, :agency_permit_document => data }, format: :json result = JSON(response.body) expect(result['message']).to eq "Agency Permit Document successfully updated!" end end context "with invalid params" do it "returns an error message" do data = {:document_type_id => nil, :agency_id => agency.id} agency_permit_document = AgencyPermitDocument.create(document_type: document_type, agency: agency) put :update, params: { :id => agency_permit_document.to_param, :agency_permit_document => data }, format: :json result = JSON(response.body) expect(result['errors']).to include("Agency Permit Document Document type can't be blank") end end end end end end