Sha256: 5cce0717bbfce9adb71f452fb179625fcd0637bc0c5904d2e135cd92af73cee5

Contents?: true

Size: 1.34 KB

Versions: 4

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

require 'rspec/expectations'

RSpec::Matchers.define :respond_with_slack_messages do |expected|
  include SlackRubyBot::SpecHelpers

  match do |actual|
    raise ArgumentError, 'respond_with_slack_messages expects an array of ordered responses' if expected && !expected.respond_to?(:each)

    client = respond_to?(:client) ? send(:client) : SlackRubyBot::Client.new

    message_command = SlackRubyBot::Hooks::Message.new
    channel, user, message, attachments = parse(actual)

    @messages ||= []
    allow(client).to receive(:message) do |options|
      @messages.push options
    end

    message_command.call(client, Hashie::Mash.new(text: message, channel: channel, user: user, attachments: attachments))

    @responses = []

    if expected&.any?
      expected.each do |exp|
        @responses.push(expect(client).to(have_received(:message).with(hash_including(channel: channel, text: exp)).once))
      end
    else
      expect(@messages.size).to be > 1
    end

    true
  end

  failure_message do |_actual|
    if expected&.any?
      expected.map do |exp|
        "Expected text: #{exp}, got #{@messages[expected.index(exp)] || 'none'}" unless @responses[expected.index(exp)]
      end.compact.join("\n")
    else
      "Expected to receive multiple messages, got #{@messages.any? ? @messages.size : 'none'}"
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
slackrb-0.17.1 lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb
slackrb-0.17.0 lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb
slack-ruby-bot-0.16.1 lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb
slack-ruby-bot-0.16.0 lib/slack-ruby-bot/rspec/support/slack-ruby-bot/respond_with_slack_messages.rb