我正在设置一个测试,应该期望调用两个“订阅者”实例:
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)
,它会失败并显示该错误消息。如果我删除该行,测试运行顺利(没有错误的参数数量问题)。我究竟做错了什么?