slack_message 1.8.0 → 1.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -1
- data/Gemfile.lock +1 -1
- data/lib/slack_message/rspec.rb +84 -24
- data/slack_message.gemspec +1 -1
- data/spec/slack_message_spec.rb +26 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 94e7433c5374c318cd955ad43f6a02705b62876498253c4e7587a7ba5ae5277f
|
4
|
+
data.tar.gz: 0ca96c1266c9dc55af30c6df7f07e2ea86a64bc5fad99258a10efe10e4127e9b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2f4c15c91c6a7741c1f387b820cc36689cd7530233e9582fb3db73407d9f39eb69656a071b65789cd33ec359d5fa1478efe212336a3834202c30f96269885895
|
7
|
+
data.tar.gz: ed86a645c481c0c91510acedc5b0abd2bf3923f9ea33e9a23671db0e107005c07f64a1dfc52f4f5f47f826630712756bd5ce43ddfd06622a10ed24afa7731465
|
data/CHANGELOG.md
CHANGED
@@ -2,8 +2,11 @@
|
|
2
2
|
|
3
3
|
## [Unreleased]
|
4
4
|
|
5
|
+
## [1.8.1] - 2021-10-08
|
6
|
+
- Cleaned that rspec code a bit, added more matchers for real world use.
|
7
|
+
|
5
8
|
## [1.8.0] - 2021-10-07
|
6
|
-
- Added the ability to test in RSpec
|
9
|
+
- Added the ability to test in RSpec.
|
7
10
|
|
8
11
|
## [1.7.1] - 2021-10-06
|
9
12
|
- Fixed literally a syntax issue.
|
data/Gemfile.lock
CHANGED
data/lib/slack_message/rspec.rb
CHANGED
@@ -4,6 +4,15 @@ require 'rspec/mocks'
|
|
4
4
|
# Honestly, this code is what happens when you do not understand the RSpec
|
5
5
|
# custom expectation API really at all, but you really want to create your
|
6
6
|
# matcher. This code is soo baaad.
|
7
|
+
#
|
8
|
+
# We override API calls by entirely replacing the low-level API method. Then we
|
9
|
+
# use our overridden version to capture and record calls. When someone creates
|
10
|
+
# a new expectation, an object is created, so we allow that object to register
|
11
|
+
# itself to receive notification when a slack message _would have_ been posted.
|
12
|
+
#
|
13
|
+
# Then once the expectation is fulfilled, that class unregisters itself so that
|
14
|
+
# it can be cleaned up properly.
|
15
|
+
#
|
7
16
|
|
8
17
|
module SlackMessage::RSpec
|
9
18
|
extend RSpec::Matchers::DSL
|
@@ -31,6 +40,7 @@ module SlackMessage::RSpec
|
|
31
40
|
end
|
32
41
|
end
|
33
42
|
|
43
|
+
# w/ channel
|
34
44
|
matcher :post_slack_message_to do |expected|
|
35
45
|
match do |actual|
|
36
46
|
@instance ||= PostTo.new
|
@@ -40,9 +50,50 @@ module SlackMessage::RSpec
|
|
40
50
|
@instance.enforce_expectations
|
41
51
|
end
|
42
52
|
|
43
|
-
chain :with_content_matching do |
|
53
|
+
chain :with_content_matching do |content|
|
44
54
|
@instance ||= PostTo.new
|
45
|
-
@instance.with_content_matching(
|
55
|
+
@instance.with_content_matching(content)
|
56
|
+
end
|
57
|
+
|
58
|
+
failure_message { @instance.failure_message }
|
59
|
+
failure_message_when_negated { @instance.failure_message_when_negated }
|
60
|
+
|
61
|
+
supports_block_expectations
|
62
|
+
end
|
63
|
+
|
64
|
+
# no channel
|
65
|
+
matcher :post_to_slack do |expected|
|
66
|
+
match do |actual|
|
67
|
+
@instance ||= PostTo.new
|
68
|
+
|
69
|
+
actual.call
|
70
|
+
@instance.enforce_expectations
|
71
|
+
end
|
72
|
+
|
73
|
+
chain :with_content_matching do |content|
|
74
|
+
@instance ||= PostTo.new
|
75
|
+
@instance.with_content_matching(content)
|
76
|
+
end
|
77
|
+
|
78
|
+
failure_message { @instance.failure_message }
|
79
|
+
failure_message_when_negated { @instance.failure_message_when_negated }
|
80
|
+
|
81
|
+
supports_block_expectations
|
82
|
+
end
|
83
|
+
|
84
|
+
# name / profile matcher
|
85
|
+
matcher :post_slack_message_as do |expected|
|
86
|
+
match do |actual|
|
87
|
+
@instance ||= PostTo.new
|
88
|
+
@instance.with_profile(expected)
|
89
|
+
|
90
|
+
actual.call
|
91
|
+
@instance.enforce_expectations
|
92
|
+
end
|
93
|
+
|
94
|
+
chain :with_content_matching do |content|
|
95
|
+
@instance ||= PostTo.new
|
96
|
+
@instance.with_content_matching(content)
|
46
97
|
end
|
47
98
|
|
48
99
|
failure_message { @instance.failure_message }
|
@@ -54,8 +105,9 @@ module SlackMessage::RSpec
|
|
54
105
|
class PostTo
|
55
106
|
def initialize
|
56
107
|
@captured_calls = []
|
57
|
-
@
|
108
|
+
@content = nil
|
58
109
|
@channel = nil
|
110
|
+
@profile = nil
|
59
111
|
|
60
112
|
SlackMessage::RSpec.register_expectation_listener(self)
|
61
113
|
end
|
@@ -68,41 +120,49 @@ module SlackMessage::RSpec
|
|
68
120
|
@channel = channel
|
69
121
|
end
|
70
122
|
|
71
|
-
def with_content_matching(
|
72
|
-
raise ArgumentError unless
|
73
|
-
@
|
123
|
+
def with_content_matching(content)
|
124
|
+
raise ArgumentError unless content.is_a? Regexp
|
125
|
+
@content = content
|
126
|
+
end
|
127
|
+
|
128
|
+
def with_profile(profile)
|
129
|
+
@profile = profile
|
74
130
|
end
|
75
131
|
|
76
132
|
def enforce_expectations
|
77
133
|
SlackMessage::RSpec.unregister_expectation_listener(self)
|
78
|
-
matching_messages.any? { |msg| body_matches_expectation?(msg.fetch(:blocks)) }
|
79
|
-
end
|
80
134
|
|
81
|
-
|
82
|
-
|
135
|
+
@captured_calls
|
136
|
+
.filter { |call| !@channel || call[:channel] == @channel }
|
137
|
+
.filter { |call| !@profile || [call[:profile], call[:username]].include?(@profile) }
|
138
|
+
.filter { |call| !@content || call.fetch(:blocks).to_s =~ @content }
|
139
|
+
.any?
|
83
140
|
end
|
84
141
|
|
85
|
-
def
|
86
|
-
|
142
|
+
def failure_message
|
143
|
+
"expected block to #{failure_expression}"
|
144
|
+
end
|
87
145
|
|
88
|
-
|
146
|
+
def failure_message_when_negated
|
147
|
+
"expected block not to #{failure_expression}"
|
89
148
|
end
|
90
149
|
|
91
|
-
def
|
92
|
-
|
93
|
-
|
150
|
+
def failure_expression
|
151
|
+
concat = []
|
152
|
+
|
153
|
+
if @channel
|
154
|
+
concat << "post a slack message to '#{@channel}'"
|
155
|
+
elsif @profile
|
156
|
+
concat << "post a slack message as '#{@profile}'"
|
94
157
|
else
|
95
|
-
|
158
|
+
concat << "post a slack message"
|
96
159
|
end
|
97
|
-
end
|
98
160
|
|
99
|
-
|
100
|
-
|
101
|
-
if @content_expectation
|
102
|
-
"expected block not to post slack message to '#{@channel}' with content matching #{@content_expectation.inspect}"
|
103
|
-
else
|
104
|
-
"expected block not to post slack message to '#{@channel}'"
|
161
|
+
if @content
|
162
|
+
concat << "with content matching #{@content.inspect}"
|
105
163
|
end
|
164
|
+
|
165
|
+
concat.join " "
|
106
166
|
end
|
107
167
|
end
|
108
168
|
end
|
data/slack_message.gemspec
CHANGED
data/spec/slack_message_spec.rb
CHANGED
@@ -46,7 +46,7 @@ RSpec.describe SlackMessage do
|
|
46
46
|
|
47
47
|
it "raises errors for missing configuration" do
|
48
48
|
SlackMessage.configure do |config|
|
49
|
-
|
49
|
+
config.api_token = nil
|
50
50
|
end
|
51
51
|
|
52
52
|
expect {
|
@@ -87,12 +87,36 @@ RSpec.describe SlackMessage do
|
|
87
87
|
}.to post_slack_message_to('#general').with_content_matching(/foo/)
|
88
88
|
end
|
89
89
|
|
90
|
-
it "
|
90
|
+
it "resets state properly" do
|
91
91
|
expect {
|
92
92
|
SlackMessage.post_to('#general') { text "foo" }
|
93
93
|
}.to post_slack_message_to('#general')
|
94
94
|
|
95
95
|
expect { }.not_to post_slack_message_to('#general')
|
96
96
|
end
|
97
|
+
|
98
|
+
it "lets you assert by profile name" do
|
99
|
+
SlackMessage.configure do |config|
|
100
|
+
config.add_profile(:schmoebot, name: 'Schmoe', url: 'http://hooks.slack.com/1234/', default_channel: '#schmoes')
|
101
|
+
end
|
102
|
+
|
103
|
+
expect {
|
104
|
+
SlackMessage.post_as(:schmoebot) { text "foo" }
|
105
|
+
}.to post_slack_message_to('#schmoes')
|
106
|
+
|
107
|
+
expect {
|
108
|
+
SlackMessage.post_as(:schmoebot) { text "foo" }
|
109
|
+
}.to post_slack_message_as(:schmoebot)
|
110
|
+
|
111
|
+
expect {
|
112
|
+
SlackMessage.post_as(:schmoebot) { text "foo" }
|
113
|
+
}.to post_slack_message_as('Schmoe').with_content_matching(/foo/)
|
114
|
+
end
|
115
|
+
|
116
|
+
it "can assert more generally too tbh" do
|
117
|
+
expect {
|
118
|
+
SlackMessage.post_to('#general') { text "foo" }
|
119
|
+
}.to post_to_slack.with_content_matching(/foo/)
|
120
|
+
end
|
97
121
|
end
|
98
122
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slack_message
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.8.
|
4
|
+
version: 1.8.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joe Mastey
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-10-
|
11
|
+
date: 2021-10-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|