[英]wrong number of arguments (1 for 0)
我正在設置一個測試,該測試應該期望在兩個“訂戶”實例上進行調用:
it "sends out sms to all the subscribers" do
@user.subscribers.create!
@user.subscribers.create!
Subscriber.any_instance.should_receive(:send_message).with(@message).times(2)
post :create, {:broadcast => valid_attributes}
end
實際的代碼是:
def create
@broadcast = Broadcast.new(params[:broadcast])
current_user.subscribers.each do |subscriber|
subscriber.send_message(@broadcast.message)
end
...
錯誤:
Failure/Error: post :create, {:broadcast => valid_attributes}
ArgumentError:
wrong number of arguments (1 for 0)
# ./app/controllers/broadcasts_controller.rb:41:in `block in create'
# ./app/controllers/broadcasts_controller.rb:40:in `create'
# ./spec/controllers/broadcasts_controller_spec.rb:73:in `block (4 levels) in <top (required)>'
由於某些原因,如果我添加以下行: Subscriber.any_instance.should_receive(:send_message).with(@message).times(2)
,則它將失敗並顯示該錯誤消息。 如果我刪除該行,則測試運行平穩(沒有錯誤數量的參數問題)。 我究竟做錯了什么?
您得到的錯誤是因為“時間”方法應該鏈接到其他“接收計數”期望值之一上。 您可以使用以下任何一種:
should_receive(:send_message).with(@message).exactly(2).times
should_receive(:send_message).with(@message).at_most(2).times
您還可以使用不需要'times'方法的其他替代方法之一:
should_receive(:send_message).with(@message).twice
should_receive(:send_message).with(@message).at_most(:twice)
有關更多信息,請參見rspec-mocks文檔。
您可能需要在創建訂戶之前設置期望值:
it "sends out sms to all the subscribers" do
Subscriber.any_instance.should_receive(:send_message).with(@message).twice
@user.subscribers.create!
@user.subscribers.create!
post :create, {:broadcast => valid_attributes}
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.