Ruby on rails 如何使用RSpec测试ActionCable?
这是我的通知频道Ruby on rails 如何使用RSpec测试ActionCable?,ruby-on-rails,rspec,ruby-on-rails-5,actioncable,Ruby On Rails,Rspec,Ruby On Rails 5,Actioncable,这是我的通知频道 class NotificationChannel < ApplicationCable::Channel def subscribed stream_from "notification_user_#{user.id}" end def unsubscribed stop_all_streams end end 当我运行这个规范时,它给出了一个错误 Wrong number of arguments. Expected 2, got
class NotificationChannel < ApplicationCable::Channel
def subscribed
stream_from "notification_user_#{user.id}"
end
def unsubscribed
stop_all_streams
end
end
当我运行这个规范时,它给出了一个错误
Wrong number of arguments. Expected 2, got 1
我使用link为存根和这个文件编写代码
Rails版本-5.0.0.1
Ruby版本-2.3.1
expect(@action_cable).to receive(:broadcast).with("notification_user_#{@user.id}")
仔细观察广播需要两个参数,所以
expect(@action_cable).to receive(:broadcast).with("notification_user_#{@user.id}", data)
我猜不出到底发生了什么,但有一个问题是
let(:data) do
{
"action" => 'action_name',
"category" => "regular",
"region" => "us"
}
end
你需要一个动作来执行动作。
但是,您没有在NotificationsAnnel中定义任何操作
否则您可以尝试
NotificationChannel.broadcast_to("notification_user_#{@user.id}", data )
哪一行有错误?错误在
expect(@action\u cable)
行。我用commentisNotificationChannel.broadcast\u to(“notification\u user\u35;{@user.id}”,data)
更新了我的问题,相当于expect(@action\u cable)。to receive(:broadcast)。with(“notification\u user\u35;{@user.id}”)@channel.perform\u action(data)
或者我们只能在定义了操作的情况下使用此选项,但在我的情况下,只有订阅的和取消订阅的以及取消订阅的我更新了关于错误的答案。Perform_action是指当您从javascript执行以调用通道上的操作时(由框架自动调用)。broadcast_to是向订阅者广播消息,例如:“notification_user_#{user.id}”
NotificationChannel.broadcast_to("notification_user_#{@user.id}", data )