ruby - 如何为 RSpec 中的块生成对象的方法编写单元测试
问题描述
我有一个类,它实现了一个.call
产生块对象的方法,我想学习如何为此编写单元测试。这就是我所拥有的。
module A
class B < Service
def call(object_id:)
@object = Something.find(object_id)
@object.update(status: 'done')
yield @object
end
def set_to_in_progress
@object.update(status: 'in_progress')
end
end
end
class Service
def self.call(*args); new.call(*args); end
end
然后我像这样使用它:
A::B.call(obj) do |object|
object.set_to_in_progress if some_other_condition?
end
我需要能够为call
测试状态是否已更改为已完成或正在进行的方法编写单元测试。这是我所拥有的:
RSpec.describe A::B, :unit do
let(:object) { create(:something, id: 1, status: 'in_progress') }
it 'updates the status to done' do
described_class.call(object.id) do |???|
???
end
expect(object.status).to equal('done')
end
it 'updates the status to in progress' do
described_class.call(object.id) do |???|
???
end
expect(object.status).to equal('in_progress')
end
end
解决方案
您可以在以下位置返回对象#call
:
def call(object_id:)
@object = Something.find(object_id)
@object.update(status: 'done')
yield @object
@object
end
然后在测试中你可以这样做:
object = described_class.call(object.id)
expect(object.status).to eq('in_progress')
推荐阅读
- java - NoClassDefFoundError:Java SDK 中的 AmazonSNSClient
- excel - 有没有办法创建一个基于 Excel 表中的行生成问题的动态表单?
- ionic-framework - Adonis.js、Ionic 和 Cors
- makefile - 如何在隐藏文件夹中自动创建 Makefile 依赖文件?
- glibc - 编写自定义 nss 主机模块
- excel - 如何从最后一行取消隐藏 VBA 中的所有行?
- sql-server - 从 .ss 文件恢复 SQL Server 数据库
- ldap - LDAP 更改 uID 和 gID 的基数
- docker - HAProxy/Docker: 运行 Flask/React 应用程序的 Docker 容器时出现 502 Bad Gateway
- go - 使用 GORM 同时保存到事务