我正在使用shoulda-callback-matchers
gem测试回调。 Rails版本是5.2.2,RSpec版本是3.8.0。
任何人都知道如何使用after_create_commit
gem测试shoulda-callback-matchers
回调吗?
它适用于after_create
和after_update
。
型号代码
after_create :admission_created_notification
after_update :admission_status_change_notification
示例示例
context "callbacks" do
let(:admission) { create(:admission) }
it { expect(admission).to callback(:admission_created_notification).after(:create) }
it { expect(admission).to callback(:admission_status_change_notification).after(:update) }
end
答案 0 :(得分:0)
类似这样的东西:
RSpec.describe YourModel, type: :model do
context 'callbacks check' do
it { is_expected.to callback(:admission_created_notification).after(:create) }
it { is_expected.to callback(:admission_status_change_notification).after(:update) }
end
end
答案 1 :(得分:0)
我没有使用上述宝石。我使用Rspec的默认方式为这些回调编写rspec。 模型回调代码
class Admission < ApplicationRecord
after_create_commit :admission_created_notification
after_update_commit :admission_status_change_notification
end
型号规格代码
context 'create and update' do
let(:student_detail) { FactoryBot.create(:student_detail) }
let(:institute_program) { FactoryBot.create(:institute_program) }
let(:admission) { FactoryBot.create(:admission, student_detail: student_detail, institute_program: institute_program) }
it 'When create' do
admission.student_detail.notifications.count(1)
admission.institute_program.institute_detail.notifications.count(1)
end
it 'When applied' do
admission.update(status: 'applied')
admission.student_detail.notifications.count(2)
admission.institute_program.institute_detail.notifications.count(2)
end
it 'When accepted' do
admission.update(status: 'accepted')
admission.student_detail.notifications.count(3)
admission.institute_program.institute_detail.notifications.count(3)
end
it 'When rejected' do
admission.update(status: 'rejected')
admission.student_detail.notifications.count(3)
end
it 'When approved' do
admission.update(status: 'approved')
admission.student_detail.notifications.count(4)
admission.institute_program.institute_detail.notifications.count(4)
admission.notifications.count(9)
end
end