Module: SlackMessage::RSpec

Extended by:
RSpec::Matchers::DSL
Defined in:
lib/slack_message/rspec.rb

Overview

TODO: test helpers for scheduled messages, editing and deleting, and notification text. And realistically, overhaul all this.

Defined Under Namespace

Classes: FauxResponse, PostTo

Constant Summary collapse

@@listeners =
[]
@@custom_response =
{}
@@response_code =
'200'

Class Method Summary collapse

Class Method Details

.included(_) ⇒ Object



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
# File 'lib/slack_message/rspec.rb', line 42

def self.included(_)
  [:post_message, :update_message, :delete_message].each do |method|
    SlackMessage::Api.undef_method(method)
    SlackMessage::Api.define_singleton_method(method) do |profile, params|
      @@listeners.each do |listener|
        listener.record_call(params.merge(profile: profile))
      end

      response = {
       "ok" => true,
       "channel" => "C12345678",
       "ts" => "1635863996.002300",
       "message" => { "type"=>"message", "subtype"=>"bot_message",
                     "text"=>"foo",
                     "ts"=>"1635863996.002300",
                     "username"=>"SlackMessage",
                     "icons"=>{"emoji"=>":successkid:"},
                     "bot_id"=>"B1234567890",
                     "blocks"=> [{"type"=>"section",
                                  "block_id"=>"hAh7",
                                  "text"=>{"type"=>"mrkdwn", "text"=>"foo", "verbatim"=>false}}
                     ]
       }
      }.merge(@@custom_response).to_json

      return FauxResponse.new(@@response_code, response)
    end
  end

  SlackMessage::Api.undef_method(:look_up_user_by_email)
  SlackMessage::Api.define_singleton_method(:look_up_user_by_email) do |email, profile|
    response = {"ok"=>true, "user"=>{"id"=>"U5432CBA"}}
    return FauxResponse.new('200', response.to_json)
  end
end

.register_expectation_listener(expectation_instance) ⇒ Object



27
28
29
# File 'lib/slack_message/rspec.rb', line 27

def self.register_expectation_listener(expectation_instance)
  @@listeners << expectation_instance
end

.reset_custom_responsesObject



35
36
37
38
# File 'lib/slack_message/rspec.rb', line 35

def self.reset_custom_responses
  @@custom_response = {}
  @@response_code = '200'
end

.reset_mock_responseObject



85
86
87
88
# File 'lib/slack_message/rspec.rb', line 85

def self.reset_mock_response
  @@custom_response = {}
  @@response_code = '200'
end

.respond_with(response = {}, code: '200') ⇒ Object

Raises:

  • (ArgumentError)


78
79
80
81
82
83
# File 'lib/slack_message/rspec.rb', line 78

def self.respond_with(response = {}, code: '200')
  raise ArgumentError, "custom response must be a hash" unless response.is_a? Hash

  @@custom_response = response
  @@response_code = code
end

.unregister_expectation_listener(expectation_instance) ⇒ Object



31
32
33
# File 'lib/slack_message/rspec.rb', line 31

def self.unregister_expectation_listener(expectation_instance)
  @@listeners.delete(expectation_instance)
end