require 'rails_helper' module Logistics module Core RSpec.describe OperationDocumentsController, type: :controller do routes { Logistics::Core::Engine.routes } let(:valid_attributes) { { operation_id: create(:operation).id, document: FFaker::Name.name, original: 1, copy: 1, certified: 1, is_mandatory: false, is_accepted: false } } let(:invalid_attributes) { { operation_id: create(:operation).id, document: nil, original: 1, copy: 1, certified: 1, is_mandatory: false, is_accepted: false } } describe "GET #index" do it "assigns all operation documents as @operation_documents" do op = create(:operation) od = create(:operation_document, operation: op) get :index, params: { id: op.id }, format: :json result = JSON(response.body) expect(result['data'][0]['document']).to eq od.document expect(result['data'][0]['original']).to eq od.original expect(result['data'][0]['copy']).to eq od.copy expect(result['data'][0]['certified']).to eq od.certified expect(result['data'][0]['is_mandatory']).to eq od.is_mandatory end end describe "GET #accept" do it "accepts an operation document" do od = create(:operation_document) get :accept, params: { id: od.id }, format: :json result = JSON(response.body) od.reload expect(result['success']).to be_truthy expect(od.is_accepted).to be_truthy end end describe "POST #create" do context "with valid params" do it "creates a new operation document" do expect { post :create, params: { :operation_document => valid_attributes }, format: :json }.to change(OperationDocument, :count).by(1) end it "should return a success message" do post :create, params: { :operation_document => valid_attributes }, format: :json result = JSON(response.body) expect(result['message']).to eq('Operation document saved successfully!') end end context "with invalid params" do it 'should return an error message' do post :create, params: { :operation_document => invalid_attributes }, format: :json result = JSON(response.body) expect(result['errors']).to include("Operation document Document can't be blank") end end end describe "PUT #update" do context "with valid params" do let(:new_attributes) { { document: FFaker::Name.name } } it "updates the requested operation document" do operation_document = create(:operation_document) old_data = operation_document.document put :update, params: { :id => operation_document.to_param, :operation_document => new_attributes }, format: :json operation_document.reload expect(operation_document.document).not_to eq old_data end it "should return a success message" do operation_document = create(:operation_document) put :update, params: { :id => operation_document.to_param, :operation_document => valid_attributes }, format: :json result = JSON(response.body) expect(result['message']).to eq('Operation document updated successfully!') end end context "with invalid params" do it "should return an error message" do operation_document = create(:operation_document) put :update, params: { :id => operation_document.to_param, :operation_document => invalid_attributes }, format: :json result = JSON(response.body) expect(result['errors']).to include("Operation document Document can't be blank") end end end describe "DELETE #destroy" do it "deletes an operation document" do od = create(:operation_document) expect { delete :destroy, params: { :id => od.to_param }, format: :json }.to change(OperationDocument, :count).by(-1) end end end end end