这是我的NotificationChannel
class NotificationChannel < ApplicationCable::Channel
def subscribed
stream_from "notification_user_#{user.id}"
end
def unsubscribed
stop_all_streams
end
end
这是我的Rspec
require 'rails_helper'
require_relative 'stubs/test_connection'
RSpec.describe NotificationChannel, type: :channel do
before do
@user = create(:user)
@connection = TestConnection.new(@user)
@channel = NotificationChannel.new @connection, {}
@action_cable = ActionCable.server
end
let(:data) do
{
"category" => "regular",
"region" => "us"
}
end
it 'notify user' do
#error is in below line
expect(@action_cable).to receive(:broadcast).with("notification_user_#{@user.id}")
@channel.perform_action(data)
end
end
当我运行此规范时,它会给出错误
Wrong number of arguments. Expected 2, got 1
我使用this链接来编写存根和此文件的代码。
Rails版本 - 5.0.0.1 Ruby版本 - 2.3.1
答案 0 :(得分: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
您需要针对perform_action执行操作。 但是,您没有在NotificationsChannel中定义任何操作。
否则你可以尝试
NotificationChannel.broadcast_to("notification_user_#{@user.id}", data )