ruby-on-rails - AASM + Rspec - 如何在测试中忽略/禁用/跳过转换回调?
问题描述
我有这样的课
class Job
include AASM
aasm do
state :created, initial: true
state :processing, :notifying, :finished, :error
event :process do
before do
# do some undesired stuffs for test
end
transitions from: :created, to: :processing
end
event :notify do
transitions from: :processing, to: :notifying
end
event :finish do
before do
# do more undesired stuffs for test
end
transitions from: [:processing, :notifying], to: :finished
end
event :error do
transitions to: :error
end
end
def notify?
Date.today.saturday?
end
def run!
process!
notify! if notify?
finish!
rescue
error!
end
end
我想创建一个测试来验证是否run!
按预期遵循工作流程,但是在我的转换中,我有一些我不想在此测试中触发的回调。
before do
allow(job).to receive(:notify?).and_return(false)
# do some magick to disable the callbacks
end
it do
job.run!
expect(job).to have_received(:process!)
expect(job).to have_received(:finish!)
expect(job).not_to have_received(:notify!)
expect(job).not_to have_received(:error!)
end
是否有某种方法可以在 rspec 测试中禁用 AASM 回调,或者唯一的选择是模拟回调中的所有内容?
PS
我的实际课程比那些例子复杂得多。
解决方案
您可以将回调逻辑包装在方法中并存根这些方法调用。例如。
event :process do
before do
wrapped_method_one
wrapped_method_two
end
transitions from: :created, to: :processing
end
规格:
before { allow(job).to receive(:wrapped_method_one) }
before { allow(job).to receive(:wrapped_method_two) }
推荐阅读
- c# - 如何使用 Selenium 从网站收集信息到 c# 数组中?
- ssl - 当我在运行 Mosquitto 的服务器上使用 SSL 时收到错误消息?
- java - 如何设置 JFace Dialog 的初始大小?
- javascript - 使用 javascript 对象作为“函数存储”并动态调用它们?
- json - 三个 js JSON 属性中的“索引”是什么
- python - 使用属性从 H5 文件中过滤 HDF 数据集
- r - R中的自定义.Primitive函数
- nsis - 如何使用 NSIS 更改目录目标文件夹文本?
- android - Android Studio APK 未更新 buildVersion
- python - 如何引发我的异常而不是内置异常?