在我的Rails控制器中,我創建了同一模型類的多個實例。我想添加一些RSpec期望值,以便我可以測試它是否使用正確的參數創建了正確的數字。所以,在這裏就是我在我的規格:如何在使用RSpec的對象上添加多個should_receive期望?
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期望以允許多個不同的調用?
這應該工作。你使用的是什麼版本的RSpec?嘗試從規範(Bandmate.create(...:user_id => @ user.id ...); Bandmate.create(...:user_id =>「2222」...)中依次調用存根方法; ...),看看它是否工作。 – 2008-10-28 20:56:15