2008-10-28 17 views
26

在我的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期望以允許多個不同的調用?

+0

這應該工作。你使用的是什麼版本的RSpec?嘗試從規範(Bandmate.create(...:user_id => @ user.id ...); Bandmate.create(...:user_id =>「2222」...)中依次調用存根方法; ...),看看它是否工作。 – 2008-10-28 20:56:15

回答

34

多重期望根本不是問題。考慮到您對無序期望的具體指導,您遇到的問題是訂購問題。有關訂購期望的詳細信息,請查詢this page

小故事是,你應該添加.ordered到你的每一個期望結束。

+0

呵呵,它現在工作,甚至沒有。訂購我想我的代碼中有一個錯誤,並將其歸咎於RSpec。 感謝您的提示! – Micah 2008-10-30 17:58:14

+0

鏈接是在github上的404 – xxjjnn 2015-03-18 13:45:27

-1

Mock Receive Counts

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(:一次)
my_m ock.should_receive(:符號).at_most(:兩次)
my_mock.should_receive(:符號).at_most(n)的.times
my_mock.should_receive(:符號).any_number_of_times

Works的RSpec的2.5太。

相關問題