slack-ruby / slack-ruby-bot

The easiest way to write a Slack bot in Ruby.
MIT License
1.12k stars 187 forks source link

Missing documentation on usage with RSpec? #233

Open Alobeast opened 4 years ago

Alobeast commented 4 years ago

Hi,

When trying to rspec test a command such as:

command('support', &->(client, data, match){
        channel_real_name = client.channels.dig(data.channel, :name)
        client.say(channel: data.channel,
                         text: "#{channel_real_name} support url: https://url")
})

with the following spec file:

require_relative '../commands/support'
require 'pry'

describe MyBot::Commands::Support do

  it 'gives support link' do
    expect(message: "#{SlackRubyBot.config.user} support").to respond_with_slack_message(/support url/)
  end
end

and spec_helper:

require 'slack-ruby-bot/rspec'
require 'webmock/rspec'

$LOAD_PATH.unshift(File.dirname(__FILE__))

require 'yaml'
require 'json'
require 'async'

require_relative '../mybot'

When running rspec I get:

1) MyBot::Commands::Support gives support link
     Failure/Error: client.channels.dig(data.channel, :name)

     NoMethodError:
       undefined method `dig' for nil:NilClass

Whereas I'm expecting to get a green passing test.

In order to get over the error I think I need to connect to slack but I can’t find the proper documentation for doing so.

Thanks

dblock commented 4 years ago

The RTM client's .channels is something that comes from a local store. Since we are not actually starting a client we either need to stub that store in tests or default it. I think it's a bug either way. I'm happy to take a PR for either code or docs.