109 lines
3.0 KiB
Ruby
109 lines
3.0 KiB
Ruby
|
|
require 'rails_helper'
|
||
|
|
|
||
|
|
RSpec.describe CompaniesController do
|
||
|
|
let(:valid_attributes) do
|
||
|
|
{ name: "Test Company", entity: "Corp", id_number: "123456" }
|
||
|
|
end
|
||
|
|
|
||
|
|
let(:invalid_attributes) do
|
||
|
|
{ name: "Test Company", id_number: "invalid-format" * 100 } # Extremely long value
|
||
|
|
end
|
||
|
|
|
||
|
|
describe "GET #index" do
|
||
|
|
it "returns a success response" do
|
||
|
|
Company.create! valid_attributes
|
||
|
|
get :index
|
||
|
|
expect(response).to be_successful
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
describe "GET #show" do
|
||
|
|
it "returns a success response" do
|
||
|
|
company = Company.create! valid_attributes
|
||
|
|
get :show, params: { id: company.to_param }
|
||
|
|
expect(response).to be_successful
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
describe "GET #new" do
|
||
|
|
it "returns a success response" do
|
||
|
|
get :new
|
||
|
|
expect(response).to be_successful
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
describe "GET #edit" do
|
||
|
|
it "returns a success response" do
|
||
|
|
company = Company.create! valid_attributes
|
||
|
|
get :edit, params: { id: company.to_param }
|
||
|
|
expect(response).to be_successful
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
describe "POST #create" do
|
||
|
|
context "with valid params" do
|
||
|
|
it "creates a new Company" do
|
||
|
|
expect do
|
||
|
|
post :create, params: { company: valid_attributes }
|
||
|
|
end.to change(Company, :count).by(1)
|
||
|
|
end
|
||
|
|
|
||
|
|
it "redirects to the created company" do
|
||
|
|
post :create, params: { company: valid_attributes }
|
||
|
|
expect(response).to redirect_to(Company.last)
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
context "with invalid params" do
|
||
|
|
it "handles invalid attributes appropriately" do
|
||
|
|
post :create, params: { company: invalid_attributes }
|
||
|
|
expect(response.status).to be_in([200, 302, 422])
|
||
|
|
end
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
describe "PUT #update" do
|
||
|
|
context "with valid params" do
|
||
|
|
let(:new_attributes) do
|
||
|
|
{ name: "Updated Company Name" }
|
||
|
|
end
|
||
|
|
|
||
|
|
it "updates the requested company" do
|
||
|
|
company = Company.create! valid_attributes
|
||
|
|
put :update, params: { id: company.to_param, company: new_attributes }
|
||
|
|
company.reload
|
||
|
|
expect(company.name).to eq("Updated Company Name")
|
||
|
|
end
|
||
|
|
|
||
|
|
it "redirects to the company" do
|
||
|
|
company = Company.create! valid_attributes
|
||
|
|
put :update, params: { id: company.to_param, company: new_attributes }
|
||
|
|
expect(response).to redirect_to(company)
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
context "with invalid params" do
|
||
|
|
it "handles invalid attributes appropriately" do
|
||
|
|
company = Company.create! valid_attributes
|
||
|
|
put :update, params: { id: company.to_param, company: invalid_attributes }
|
||
|
|
expect(response.status).to be_in([200, 302, 422])
|
||
|
|
end
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
describe "DELETE #destroy" do
|
||
|
|
it "destroys the requested company" do
|
||
|
|
company = Company.create! valid_attributes
|
||
|
|
expect do
|
||
|
|
delete :destroy, params: { id: company.to_param }
|
||
|
|
end.to change(Company, :count).by(-1)
|
||
|
|
end
|
||
|
|
|
||
|
|
it "redirects to the companies list" do
|
||
|
|
company = Company.create! valid_attributes
|
||
|
|
delete :destroy, params: { id: company.to_param }
|
||
|
|
expect(response).to redirect_to(companies_url)
|
||
|
|
end
|
||
|
|
end
|
||
|
|
end
|