Rspec:允许double在多个测试中接收消息

时间:2018-02-06 03:40:09

标签: ruby-on-rails rspec

我刚刚对我的代码进行了更新,打破了以下测试以及50个其他错误:

#<Double AnonymousEvent> received unexpected message :provider with (no args)

我可以通过在allow(anon_event).to receive(:provider)块的正文中添加it来修复一个(请参阅下面的代码),但这显然无法解决所有这些问题。

是否允许anon_event double在定义:provider时接收RSpec.describe Something::Parser::AnonymousParser do subject { described_class } let(:anon_event) { double(AnonymousEvent, provider_data: provider_data) } let(:provider_data) { { 'description' => agenda } } it { is_expected.to be } describe '.applicable?' do subject { described_class.applicable?(anon_event) } context 'invalid' do let(:agenda) { 'Not valid' } it 'returns nil' do allow(anon_event).to receive(:provider) # FIX HERE expect(subject).not_to be end end end end 。或者添加一个before_action类的东西?

众多测试之一

JavaScript

1 个答案:

答案 0 :(得分:1)

是的,您可以在describe '.applicable?' do

之前的前一个块中定义它
before do
  allow(anon_event).to receive(:provider)  # FIX HERE
end