Code Monkey home page Code Monkey logo

Comments (5)

dblock avatar dblock commented on June 21, 2024

I think this is what you're looking for?

from slack-ruby-bot.

CeeBeeUK avatar CeeBeeUK commented on June 21, 2024

I ended up changing the pattern before investigating... closing for now but will investigate and add example if I get it working! 😄

from slack-ruby-bot.

CeeBeeUK avatar CeeBeeUK commented on June 21, 2024

I've finally found the time to come back to this and am having some issues and would really appreciate a steer.
I have a command

module MySlackBot
  module Commands
    class Test < SlackRubyBot::Commands::Base
      command 'test' do |client, data, match|
        client.typing(channel: data.channel)
        sleep(3)
        client.say(channel: data.channel, text: 'Oh! Hi!!')
      end
    end
  end
end

Adding the test you suggested...

require 'spec_helper'

describe MySlackBot::Commands::Test do
  let(:app) { SlackRubyBot::Server.new }
  let(:client) { app.send(:client) }
  let(:message_hook) { SlackRubyBot::Hooks::Message.new }
  it 'receives typing' do
    expect(client).to receive(:typing)
    message_hook.call(client, Hashie::Mash.new(text: "#{SlackRubyBot.config.user} test", channel: 'channel'))
  end
end

(Note: I added I added the SlackRubyBot:: to Server.new as otherwise I had a uninitialized constant Server NameError)
Results in

Slack::RealTime::Client::ClientNotStartedError:
       Slack::RealTime::Client::ClientNotStartedError

How do I go about stubbing out the server successfully?

If you can provide some pointers I would gladly open a PR to update the README :)

from slack-ruby-bot.

dblock avatar dblock commented on June 21, 2024

I think you don't need that server/app at all anymore if you assign a client a team, then the whole server trying to start won't matter. I am looking at https://github.com/slack-ruby/slack-shellbot/blob/3afcaa82c0030d90984ef83d3795bdb82a193e23/spec/commands/cd_spec.rb#L4 that does this:

let(:team) { Fabricate(:team) } # I think a stub or a Team.new will work here too
let(:client) { SlackShellbot::Web::Client.new(token: 'token', team: team) }
let(:message_hook) { SlackShellbot::Commands::Base }

from slack-ruby-bot.

CeeBeeUK avatar CeeBeeUK commented on June 21, 2024

For FutureCeeBee/anyone who searches for this keyword...
I now have tests passing on a pure slack-ruby-bot client using

require 'spec_helper'

describe MyBot::Commands::LongTest do
  let!(:client) { SlackRubyBot::App.new.send(:client) }
  let(:message_hook) { SlackRubyBot::Hooks::Message.new }
  let(:params) do
    Hashie::Mash.new(
      text: "#{SlackRubyBot.config.user} run long test",
      channel: 'channel',
      user: 'user'
    )
  end

  it 'responds with a warning message' do
    expect(client).to receive(:typing)
    expect(client).to receive(:say).with({ channel: 'channel', text: 'User created' })
    message_hook.call(client, params)
  end
end

calling

module MyBot
  module Commands
    class LongTest < SlackRubyBot::Commands::Base
      def self.user_create_script(_match)
        sleep 5
      end

      command(/run long test/) do |client, data, match|
        client.typing(channel: data.channel)
        user_create_script(match)
        client.say(channel: data.channel, text: 'User created')
      end
    end
  end
end

from slack-ruby-bot.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.