File: have_broadcasted_matcher.feature

package info (click to toggle)
ruby-rspec-rails 7.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,796 kB
  • sloc: ruby: 11,068; sh: 198; makefile: 6
file content (151 lines) | stat: -rw-r--r-- 4,442 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
Feature: `have_broadcasted_to` matcher

  The `have_broadcasted_to` (also aliased as `broadcast_to`) matcher is used
  to check if a message has been broadcasted to a given stream.

  Background:
    Given action cable testing is available

  Scenario: Checking stream name
    Given a file named "spec/models/broadcaster_spec.rb" with:
      """ruby
      require "rails_helper"

      RSpec.describe "broadcasting" do
        it "matches with stream name" do
          expect {
            ActionCable.server.broadcast(
              "notifications", { text: "Hello!" }
            )
          }.to have_broadcasted_to("notifications")
        end
      end
      """
    When I run `rspec spec/models/broadcaster_spec.rb`
    Then the examples should all pass

  Scenario: Checking passed message to stream
    Given a file named "spec/models/broadcaster_spec.rb" with:
      """ruby
      require "rails_helper"

      RSpec.describe "broadcasting" do
        it "matches with message" do
          expect {
            ActionCable.server.broadcast(
              "notifications", { text: "Hello!" }
            )
          }.to have_broadcasted_to("notifications").with(text: 'Hello!')
        end
      end
      """
    When I run `rspec spec/models/broadcaster_spec.rb`
    Then the examples should all pass

  Scenario: Checking that message passed to stream matches
    Given a file named "spec/models/broadcaster_spec.rb" with:
      """ruby
      require "rails_helper"

      RSpec.describe "broadcasting" do
        it "matches with message" do
          expect {
            ActionCable.server.broadcast(
              "notifications", { text: 'Hello!', user_id: 12 }
            )
          }.to have_broadcasted_to("notifications").with(a_hash_including(text: 'Hello!'))
        end
      end
      """
    When I run `rspec spec/models/broadcaster_spec.rb`
    Then the examples should all pass

  Scenario: Checking passed message with block
    Given a file named "spec/models/broadcaster_spec.rb" with:
      """ruby
      require "rails_helper"

      RSpec.describe "broadcasting" do
        it "matches with message" do
          expect {
            ActionCable.server.broadcast(
              "notifications", { text: 'Hello!', user_id: 12 }
            )
          }.to have_broadcasted_to("notifications").with { |data|
            expect(data['user_id']).to eq 12
          }
        end
      end
      """
    When I run `rspec spec/models/broadcaster_spec.rb`
    Then the examples should all pass

  Scenario: Using alias method
    Given a file named "spec/models/broadcaster_spec.rb" with:
      """ruby
      require "rails_helper"

      RSpec.describe "broadcasting" do
        it "matches with stream name" do
          expect {
            ActionCable.server.broadcast(
              "notifications", { text: 'Hello!' }
            )
          }.to broadcast_to("notifications")
        end
      end
      """
    When I run `rspec spec/models/broadcaster_spec.rb`
    Then the examples should all pass

  Scenario: Checking broadcast to a record
    Given a file named "spec/channels/chat_channel_spec.rb" with:
      """ruby
      require "rails_helper"

      RSpec.describe ChatChannel, type: :channel do
        it "matches with stream name" do
          user = User.new(42)

          expect {
            ChatChannel.broadcast_to(user, text: 'Hi')
          }.to have_broadcasted_to(user)
        end
      end
      """
    And a file named "app/models/user.rb" with:
      """ruby
      class User < Struct.new(:name)
        def to_gid_param
          name
        end
      end
      """
    When I run `rspec spec/channels/chat_channel_spec.rb`
    Then the example should pass

  Scenario: Checking broadcast to a record in non-channel spec
    Given a file named "spec/models/broadcaster_spec.rb" with:
      """ruby
      require "rails_helper"

      RSpec.describe "broadcasting" do
        it "matches with stream name" do
          user = User.new(42)

          expect {
            ChatChannel.broadcast_to(user, text: 'Hi')
          }.to broadcast_to(ChatChannel.broadcasting_for(user))
        end
      end
      """
    And a file named "app/models/user.rb" with:
      """ruby
      class User < Struct.new(:name)
        def to_gid_param
          name
        end
      end
      """
    When I run `rspec spec/models/broadcaster_spec.rb`
    Then the example should pass