ruby - Minitest/Mocha:测试一个值是否多次变化
问题描述
假设我有一个这样的服务类:
class FooService
def self.execute(foo_id)
Foo.find(foo_id).tap do |foo|
foo.update_attribute :status, :working
do_work(foo)
foo.update_attribute :status, :done
end
end
end
在 Minitest with Mocha 中对此方法的简单测试:
test 'executing the service' do
@foo = Foo.first
FooService.expects(:do_work).with(@foo)
FooService.execute(@foo.id)
assert_equal :done, @foo.reload.status
end
测试该status
属性是否设置为的最佳方法是:working
什么?
我尝试过使用Foo.any_instance.expects(:update_attribute).with(:status, :working)
,但由于无法在 Mocha 中调用原始实现,因此会产生不好的副作用。
解决方案
一种解决方案是do_work
引发错误。那应该在例程结束之前停止该过程并foo
保持状态working
FooService.expects(:do_work).raises(Exception, 'foo')
assert_equal :working, @foo.reload.status
推荐阅读
- java - 手机翻转时如何更改魔术8球答案?
- javascript - 如何使变量不开始新行
- python - 有没有办法在运行其他代码的同时每小时执行一次代码?
- smalltalk - “元类类”真的只是元类吗?
- swift - 无法使用 iOS 13 中的 diffable 数据源使用 tableview 滑动删除
- mysql - 如何检查 RDS Aurora 中表的状态 - MySQL 5.6 只读副本
- azure - 带有 OpenId Connect 的 Azure 流量管理器
- c++ - 我正在使用 VS 代码,但 cout 无法正常工作
- javascript - JQuery 函数似乎更新了选择器,但没有显示任何项目
- tensorflow - 在 TensorFlow 中挤压除一个轴以外的所有轴