ruby - 如何在 case 语句中存根\模拟实例?
问题描述
我有一个类方法,它使用另一个类实例方法:
class Foo
def foo
# a lot of code here, which return String instance
end
end
class Bar
class UnknownType < StandardError;end
def initialize(foo)
self.foo = foo
end
attr_reader :foo
def call
# some code which use method foo
foo
end
private
def foo=(attr)
@foo ||= case attr
when Foo then attr.foo
when String then attr
else raise UnknownType, "Unknown type #{attr.class.name}"
end
end
end
而且我的测试不起作用,我尝试子方法: - is_a - kind_of?
let(:foo) { instance_double(Foo, foo: 'some text') }
let(:bar) { Bar.new(foo) }
subject { bar.call }
it 'make some business logic here' do
expect { subject }.to be_truthy
end
但它会引发错误,UnknownType
因为模板是#<InstanceDouble(Foo) (anonymous)>
不是Foo
解决方案
Case 语句===
用于大小写相等的目的,在这种情况下Foo
是接收者而不是参数。例如
case attr
when Foo then attr.foo
end
相比之下attr
,Foo
反之亦然Foo === attr
。
所以你可以改变你的测试
it 'make some business logic here' do
allow(Foo).to receive(:===).with(foo).and_return(true)
expect { subject }.to be_truthy
end
This way when it evaluates your case statement it will follow the when Foo
path because Foo === attr
will be true due to the stubbing.
推荐阅读
- r - 为什么 tmap 的渲染速度比 ggplot2 快 80 倍?[在 R 中使用 ggplot2::geom_sf() 绘制 shapefile,在 macOS 上使用 XQuartz/X11 图形设备]
- angular - Angular 6:计算列表的总和(可观察)
- ms-access - Ms Access,按特定日期分组
- swift - UIImagePicker 无法正常运行
- javascript - 使用 EJS 将 MySQL 查询结果显示为 HTML
- java - 背靠背扫描仪尝试使用给出异常的资源
- r - 从逻辑回归中提取概率和 SE
- rest - 为什么 indy 返回不可读的数据但 chrome 加载它很好?
- excel - 根据单元格值将数据从一个工作表移动到另一个工作表
- javascript - 如何为嵌套对象设置状态