在我的Rails控制器中,我正在创建同一模型类的多个实例。我想添加一些RSpec预期,以便我可以测试它是否使用正确的参数创建正确的数字。所以,这就是我的规范中的内容:
Bandmate.should_receive(:create).with(:band_id => @band.id, :user_id => @user.id, :position_id => 1, :is_leader => true) Bandmate.should_receive(:create).with(:band_id => @band.id, :user_id => "2222", :position_id => 2) Bandmate.should_receive(:create).with(:band_id => @band.id, :user_id => "3333", :position_id => 3) Bandmate.should_receive(:create).with(:band_id => @band.id, :user_id => "4444", :position_id => 4)
这导致了问题,因为看起来Bandmate类只能设置1个“should_receive”期望值。因此,当我运行该示例时,我收到以下错误:
Spec::Mocks::MockExpectationError in 'BandsController should create all the bandmates when created' Mock 'Class' expected :create with ({:band_id=>1014, :user_id=>999, :position_id=>1, :is_leader=>true}) but received it with ({:band_id=>1014, :user_id=>"2222", :position_id=>"2"})
这是第二次创建调用的正确参数,但RSpec正在针对错误的参数进行测试。
有谁知道如何设置我的should_receive期望以允许多个不同的电话?
答案 0 :(得分:36)
多重期望根本不是问题。你遇到的问题是订购问题,考虑到你对无序期望的具体论证。有关订购期望的详细信息,请查看this page。
简短的说法是,您应该在每个期望结束时添加.ordered
。
答案 1 :(得分:-2)
my_mock.should_receive(:符号)。一旦
my_mock.should_receive(:符号).twice
my_mock.should_receive(:符号).exactly(n)的.times
my_mock.should_receive(:符号).at_least(1次)
my_mock.should_receive(:符号).at_least(:两次)
my_mock.should_receive(:符号).at_least(n)的.times
my_mock.should_receive(:符号).at_most(1次)
my_mock.should_receive(:符号).at_most(:两次)
my_mock.should_receive(:符号).at_most(n)的.times
my_mock.should_receive(:sym).any_number_of_times
也适用于rspec 2.5。