require 'rails_helper' module Logistics module Core RSpec.describe CommonDocumentsController, type: :controller do routes { Logistics::Core::Engine.routes } let(:valid_attributes) { { document_type_id: create(:document_type).id, original: 1, copy: 1, certified: 1, is_mandatory: true, is_endorsed: true } } let(:invalid_attributes) { { document_type_id: nil, original: 1, copy: 1, certified: 1, is_mandatory: true, is_endorsed: true } } describe "GET #index" do it "assigns all common documents as @common_documents" do cd = create(:common_document) get :index, format: :json result = JSON(response.body) expect(result['data'][0]['document_type_id']).to eq cd.document_type.id expect(result['data'][0]['original']).to eq cd.original expect(result['data'][0]['copy']).to eq cd.copy expect(result['data'][0]['certified']).to eq cd.certified end end describe "POST #create" do context "with valid params" do it "creates a new common document" do expect { post :create, params: { :common_document => valid_attributes }, format: :json }.to change(CommonDocument, :count).by(1) end it "should return a success message" do post :create, params: { :common_document => valid_attributes }, format: :json result = JSON(response.body) expect(result['message']).to eq('Common document saved successfully!') end end context "with invalid params" do it 'should return an error message' do post :create, params: { :common_document => invalid_attributes }, format: :json result = JSON(response.body) expect(result['errors']).to include("Common document Document type can't be blank") end end end describe "PUT #update" do context "with valid params" do let(:new_attributes) { { original: 5 } } it "updates the requested common document" do common_document = create(:common_document) old_data = common_document.original put :update, params: { :id => common_document.to_param, :common_document => new_attributes }, format: :json common_document.reload expect(common_document.original).not_to eq old_data expect(common_document.original).to eq 5 end it "should return a success message" do common_document = create(:common_document) put :update, params: { :id => common_document.to_param, :common_document => valid_attributes }, format: :json result = JSON(response.body) expect(result['message']).to eq('Common document updated successfully!') end end context "with invalid params" do it "should return an error message" do common_document = create(:common_document) put :update, params: { :id => common_document.to_param, :common_document => invalid_attributes }, format: :json result = JSON(response.body) expect(result['errors']).to include("Common document Document type can't be blank") end end end end end end