Files
old-zsterminator/spec/models/customer_spec.rb
2025-03-03 19:38:57 +01:00

128 lines
3.3 KiB
Ruby

require 'rails_helper'
RSpec.describe Customer do
let(:company) { Company.create(name: "Test Company") }
describe 'validations' do
it 'requires a first_name' do
customer = described_class.new(
surname: "Doe",
original_phone: "123456789",
phone: "123456789",
company: company
)
expect(customer).not_to be_valid
expect(customer.errors[:first_name]).to include("can't be blank")
end
it 'requires a surname' do
customer = described_class.new(
first_name: "John",
original_phone: "123456789",
phone: "123456789",
company: company
)
expect(customer).not_to be_valid
expect(customer.errors[:surname]).to include("can't be blank")
end
it 'requires a phone number' do
customer = described_class.new(
first_name: "John",
surname: "Doe",
company: company
)
expect(customer).not_to be_valid
expect(customer.errors[:phone]).to include("can't be blank")
end
it 'requires a phone' do
customer = described_class.new(
first_name: "John",
surname: "Doe",
original_phone: "123456789",
company: company
)
expect(customer).not_to be_valid
expect(customer.errors[:phone]).to include("can't be blank")
end
it 'requires a company' do
customer = described_class.new(
first_name: "John",
surname: "Doe",
original_phone: "123456789",
phone: "123456789"
)
expect(customer).not_to be_valid
expect(customer.errors[:company_id]).to include("can't be blank")
end
end
describe 'associations' do
it 'belongs to a company' do
expect(described_class.reflect_on_association(:company).macro).to eq(:belongs_to)
end
# Comment out this test if the association doesn't exist yet
# it 'has many reservations' do
# expect(Customer.reflect_on_association(:reservations).macro).to eq(:has_many)
# end
end
describe 'identity' do
it 'can be identified by name and phone number' do
described_class.create(
first_name: "John",
surname: "Doe",
original_phone: "123456789",
phone: "123456789",
company: company
)
found = described_class.find_by(
first_name: "John",
surname: "Doe",
phone: "123456789"
)
expect(found).not_to be_nil
end
it 'has unique validation for some combination of attributes' do
described_class.create(
first_name: "John",
surname: "Doe",
original_phone: "123456789",
phone: "123456789",
company: company
)
duplicate = described_class.new(
first_name: "John",
surname: "Doe",
original_phone: "123456789",
phone: "123456789",
company: company
)
duplicate.valid?
expect(duplicate.errors).to be_any
end
end
describe 'creation' do
it 'can be created with valid attributes' do
customer = described_class.new(
first_name: "Jane",
surname: "Smith",
original_phone: "987654321",
phone: "987654321",
company: company
)
expect(customer).to be_valid
expect { customer.save }.to change(described_class, :count).by(1)
end
end
end