在我的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期望允许多个不同的电话?
多重期望根本不是问题.你遇到的问题是订购问题,考虑到你对无序期望的具体论证.查看此页面了解订购期望的详细信息.
简短的故事是,你应该添加.ordered
到每个期望的结尾.