1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53require 'rails_helper'
RSpec.describe Category, type: :model do
describe 'associations' do
it { should have_many(:subscriptions).dependent(:destroy) }
it { should have_many(:customers).through(:subscriptions) }
end
describe 'CRUD operations' do
it 'can be created with valid attributes' do
category = build(:category)
expect(category.save).to be true
end
it 'cannot be created with invalid attributes' do
category = Category.new # Empty attributes
expect(category.save).to be false
expect(category.errors.full_messages).to include(
"Name can't be blank"
)
end
it 'cannot be created with duplicate name' do
create(:category, name: 'Technology')
new_category = build(:category, name: 'Technology')
expect(new_category.save).to be false
expect(new_category.errors.full_messages).to include(
'Name has already been taken'
)
end
it 'can be updated with valid attributes' do
category = create(:category)
expect(category.update(name: 'New Category Name')).to be true
expect(category.reload.name).to eq('New Category Name')
end
it 'cannot be updated with invalid attributes' do
category = create(:category)
expect(category.update(name: '')).to be false
expect(category.errors.full_messages).to include(
"Name can't be blank"
)
end
it 'can be soft deleted' do
category = create(:category)
category.soft_delete
expect(category.deleted_at).to be_present
end
end
end