Module: BoltRb::Testing::RSpecHelpers

Defined in:
lib/bolt_rb/testing/rspec_helpers.rb

Overview

RSpec helper methods for testing Bolt handlers

Include this module in your RSpec configuration to get convenient methods for building contexts and mocking Slack clients.

Examples:

Including in RSpec

RSpec.configure do |config|
  config.include BoltRb::Testing::RSpecHelpers
end

Using in specs

describe MyHandler do
  it 'handles messages' do
    ctx = build_context(payload.message(text: 'hello'))
    MyHandler.call(ctx)
    expect(ctx.acked?).to be true
  end
end

Instance Method Summary collapse

Instance Method Details

#build_context(payload, client: nil, ack: nil) ⇒ BoltRb::Context

Builds a Context object from a payload for testing



30
31
32
33
34
35
36
# File 'lib/bolt_rb/testing/rspec_helpers.rb', line 30

def build_context(payload, client: nil, ack: nil)
  BoltRb::Context.new(
    payload: payload,
    client: client || mock_slack_client,
    ack: ack || ->(_) {}
  )
end

#mock_slack_clientRSpec::Mocks::Double

Creates a mock Slack Web API client with common methods stubbed



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/bolt_rb/testing/rspec_helpers.rb', line 41

def mock_slack_client
  client = instance_double(Slack::Web::Client)
  allow(client).to receive(:chat_postMessage).and_return({ 'ok' => true, 'ts' => '123.456' })
  allow(client).to receive(:chat_update).and_return({ 'ok' => true })
  allow(client).to receive(:views_open).and_return({ 'ok' => true })
  allow(client).to receive(:views_update).and_return({ 'ok' => true })
  allow(client).to receive(:assistant_threads_setStatus).and_return({ 'ok' => true })
  allow(client).to receive(:assistant_threads_setTitle).and_return({ 'ok' => true })
  allow(client).to receive(:assistant_threads_setSuggestedPrompts).and_return({ 'ok' => true })
  client
end

#payloadClass

Convenience accessor for the PayloadFactory

Examples:

payload.message(text: 'hello')
payload.command(command: '/deploy')


60
61
62
# File 'lib/bolt_rb/testing/rspec_helpers.rb', line 60

def payload
  BoltRb::Testing::PayloadFactory
end