Class Abc
def initialize(target)
@target = target
end
def method_missing(name, *params, &block)
@target.send(name, *params, &block)
end
end我有像上面这样的代码,我想写上面的代码规格,什么是最好的方式来测试这一点。选项1. Abc的测试实例响应@target的方法?
发布于 2018-07-13 22:43:47
你的目标应该是测试行为,而不是实现,所以忘了存根和期待一个特定的方法(除非确实没有其他方法)。
还有--你的例子被从任何上下文中剥离出来。拥有它没有什么意义,除非它能给你的代码库增加一些价值。
但作为一个高层次的回答:理想情况下,您应该对每个可能的目标都有规范。您可以考虑提取这些内容作为共享示例,并像这样做一些事情
RSpec.describe Abc do
subject { described_class.new(target) }
context 'when the target is a string' do
let(:target) { String.new }
it_behaves_like 'a string'
end
context 'when the target is a CustomUser'
let(:target) { CustomUser.new }
it_behaves_like 'custom user behavior'
end
end 正如你所看到的--这很无聊。但是我假设Abc不是一个真实的例子,所以答案也有点抽象。
发布于 2018-07-13 08:26:32
https://stackoverflow.com/questions/51305966
复制相似问题