require 'rails_helper' RSpec.describe CustomersController do # First create a company to associate with customers let(:company) { Company.create!(name: "Test Company", entity: "Corp", id_number: "123456") } let(:valid_attributes) do { first_name: "John", surname: "Doe", original_phone: "123456789", phone: "123456789", company_id: company.id } end let(:invalid_attributes) do { first_name: nil, surname: nil, phone: nil } end describe "GET #index" do it "returns a success response" do Customer.create! valid_attributes get :index, params: { company_id: company.id } expect(response).to be_successful end end describe "GET #new" do it "returns a success response" do get :new, params: { company_id: company.id } expect(response).to be_successful end end describe "POST #create" do context "with valid params" do it "creates a new Customer" do expect do post :create, params: { customer: valid_attributes, company_id: company.id } end.to change(Customer, :count).by(1) # Verify the customer data was saved correctly customer = Customer.last expect(customer.first_name).to eq("John") expect(customer.surname).to eq("Doe") expect(customer.phone).to eq("123456789") expect(customer.original_phone).to eq("123456789") expect(customer.company_id).to eq(company.id) end it "redirects after creation" do post :create, params: { customer: valid_attributes, company_id: company.id } expect(response).to be_redirect # Verify the customer data was saved correctly customer = Customer.last expect(customer.first_name).to eq("John") expect(customer.surname).to eq("Doe") expect(customer.phone).to eq("123456789") expect(customer.company_id).to eq(company.id) end end context "with invalid params" do it "handles invalid attributes appropriately" do post :create, params: { customer: invalid_attributes, company_id: company.id } expect(response.status).to be_in([200, 302, 422]) end end end end