【发布时间】:2020-02-29 21:47:00
【问题描述】:
在 Rspec 中有两个变量,它们都是整数,但在示例中(在“之前”块中),其中一个是适当的自己的值,另一个是 nil。为什么?!从未听说过这种奇怪的 rspec 行为。
尝试将 0 值更改为 1, 尝试更改变量名称, 尝试将“让”更改为“让!”, 但行为没有改变。
代码是:
context 'when input contains incorrect symbols' do
let(:counter) { 1 }
let(:mocked_retry_count) { 5 }
before do
allow(described_class).to receive(:gets) {
byebug
counter += 1
counter > mocked_retry_count ? 'Stop the loop' : ['$', (0..9).to_a.sample, '#', '%', '&'].sample
}
described_class.ask_kingdoms
end
end
在 byebug 的输出中我看到了
62: let(:counter) { 1 }
63: let(:mocked_retry_count) { 5 }
64: before do
65: allow(described_class).to receive(:gets) {
66: byebug
=> 67: counter += 1
68: counter > mocked_retry_count ? 'Stop the loop' : ['$', (0..9).to_a.sample, '#', '%', '&'].sample
69: }
70: described_class.ask_kingdoms
71: end
(byebug) counter
nil
(byebug) mocked_retry_count
5
'counter' 和 'mocked_retry_count' 之间的主要区别是什么?在示例中如何获取我的计数器?
【问题讨论】:
-
这里很不清楚这个规范实际上试图测试什么行为,但如果你想测试一个方法被多次调用,你可以使用接收计数
expect(described_class).to receive(:gets).exactly(n).times。 relishapp.com/rspec/rspec-mocks/v/2-8/docs/message-expectations/…
标签: ruby rspec factory-bot