我正在使用rspec 3.0.0.beta1。我必须测试一种产生self
以下结果的方法:
class Test
def initialize
yield self if block_given?
end
end
这是一个成功的测试:
describe Test do
context 'giving a block with one argument' do
it 'yields self'
expect { |b| described_class.new &b }.to yield_with_args described_class
end
end
end
但它仅测试对象类,而不测试对的标识self
。
这是我写的最接近的(失败)测试:
describe Test do
context 'giving a block with one argument' do
it 'yields itself'
instance = nil
expect { |b|
instance = described_class.new &b
}.to yield_with_args instance
end
end
end
实际上,它失败了,因为在评估最后一个实例出现时它是nil
,因此它与块评估中的实例不匹配。
该yield
匹配器不能在你的情况直接使用。最简单的事情是稍后使用不同的匹配器修改第二个代码。
describe Test do
context 'giving a block with one argument' do
it 'yields itself'
yielded_instance = nil
new_instance = described_class.new { |i| yielded_instance = i }
expect(yielded_instance).to be new_instance
end
end
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句