如何使用rspec测试电报bot

问题描述 投票:0回答:1

我正在构建一个简单的机器人,我需要测试以下方法,但是无论如何我都会遇到一个看起来像是无限循环的事物。

我要测试的方法

def main_method(token)
@id_array = []
Telegram::Bot::Client.run(token) do |bot|
  bot.listen do |message|
    @id_array.push(message.chat.id) unless @id_array.include?(message.chat.id)
    case message.text
    when '/quote'
      quote, author = RandomQuote.new.get_quote!
      bot.api.send_message(chat_id: message.chat.id, text: "#{quote}\n\t- Author: #{author}")
    when '/joke'
      bot.api.send_message(chat_id: message.chat.id, text: "What's your name?")
      bot.listen do |name|
        if name
          @joke = Jokes.new.random_joke(name.text)
          break
        end
      end
      bot.api.send_message(chat_id: message.chat.id, text: @joke.to_s)
    when '/help'
      bot.api.send_message(chat_id: message.chat.id, text: 'Type /joke to receive a custom joke with your name')
      bot.api.send_message(chat_id: message.chat.id, text: 'Type /quote to receive an inspirational quote')
    end
  end
end

结束

我的机器人正在工作并完成任务,我只需要任何测试用例即可证明其功能正常。

ruby rspec telegram-bot
1个回答
0
投票

嗯,这不是一种“无限循环”,它是事件监听器(bot.listen),它是一种轮询某些端口以获取某些消息,然后在此事件上触发响应的方法。

因此,您需要使用OOP逻辑将该方法拆分为小的块,因为一种特定方法的情况很多,然后将每个块作为模拟使用的专用类进行测试。

要处理初始化,您可以使用VCR卡带将实际请求存根到API中,并将令牌替换为测试令牌。

例如,我们可以模拟main_method以确保:调用了listener,如下所示:

class MyRubyBot
  def main_method(token)
    ...
  end
end


around do |example|
  VCR.use_cassette('telegram_bot') do
    example.run
  end
end

it 'fires run on telegram bot' do
  expect(telegram_bot).to receive(:run).with("YOU_TEST_TOKEN")
  MyRubyBot.new.main_method("YOU_TEST_TOKEN")
end

it 'fires listener' do
  expect_any_instance_of(Telegram::Bot::Client).to receive(:listen)
  MyRubyBot.new.main_method("YOU_TEST_TOKEN")
end

然后您需要将每个when中的逻辑放入专用的类中,例如:

class QuoteMessage
  attr_accessor :bot, :message

  def initialize(bot:, message:)
    self.bot = bot
    self.message = message
  end

  def send_response
    quote, author = RandomQuote.new.get_quote!
    bot.api.send_message(chat_id: message.chat.id, text: "#{quote}\n\t- Author: #{author}")
  end
end
class QuoteMessage
  attr_accessor :bot, :message, :joke

  def initialize(bot:, message:)
    self.bot = bot
    self.message = message
  end

  def send_response
    bot.api.send_message(chat_id: message.chat.id, text: "What's your name?")
    bot.listen do |name|
      if name
        @joke = Jokes.new.random_joke(name.text)
        break
      end
    end
    bot.api.send_message(chat_id: message.chat.id, text: @joke.to_s)
  end
end
class HelpMessage
  attr_accessor :bot, :message

  def initialize(bot:, message:)
    self.bot = bot
    self.message = message
  end

  def send_response
    bot.api.send_message(chat_id: message.chat.id, text: 'Type /joke to receive a custom joke with your name')
    bot.api.send_message(chat_id: message.chat.id, text: 'Type /quote to receive an inspirational quote')
  end
end

太好了,现在我们可以分别对模拟机器人类进行任何测试,举个简单的例子:

describe QuoteMessage do
  let(:bot) { double }
  let(:api) { double }
  let(:message) { double }

  it 'fires send_message' do
    expect(bot).to receive(:api).and_return(api)
    expect(api).to receive(:send_message).with(...)
    described_class.new(bot: bot, message: message).send_response
  end
end
# ... same logic for other classes

快到了!现在我们可以使用新的MessageHandler类来重构main_method,如下所示:

def main_method(token)
  @id_array = []
  Telegram::Bot::Client.run(token) do |bot|
    bot.listen do |message|
      @id_array.push(message.chat.id) unless @id_array.include?(message.chat.id)
      MessageHandler.new(bot: bot, message: message).handle_message
    end
  end
end

class MessageHandler
  attr_accessor :bot, :message

  def initialize(bot:, message:)
    self.bot = bot
    self.message = message
  end

  def handle_message
    case message.text
    when '/quote'
      QuoteMessage.new(bot: bot, message: message).send_response
    when '/joke'
      joke_message = JokeMessage.new(bot: bot, message: message)
      joke_message.send_response
      @joke = joke_message.joke
    when '/help'
      HelpMessage.new(bot: bot, message: message).send_response
    end
  end
end

因此,现在我们可以通过MessageHandler方法测试and_yield的火灾:


it 'fires MessageHandler' do
  message = 'any_message'
  expect(bot).to receive(:listen).and_yield(MessageHandler.new(bot: bot, message: message))
  MyRubyBot.new.main_method("YOU_TEST_TOKEN")
end

至少,我们需要测试MessageHandler,它现在应该非常容易,例如:

describe MessageHandler do
  subject { described_class.new(bot: bot, message: message) }
  let(:bot) { double }
  let(:api) { double }

  context 'when quote' do
    let(:message) { '/quote' }
    let(:quote_message) { instance_double QuoteMessage }

    it 'fires QuoteMessage instance' do
      expect(QuoteMessage).to receive(:new).with(bot: bot, message: message).and_return quote_message 
      expect(quote_message).to receive(:send_response) 
      subject.handle_message
    end
  end
end

当然,我可能在某些细节上是错误的,但总体而言,我想提出这种方法。

[JFYI:尝试问一些更具体的问题,因为看起来您在这里有一个任务,并且您决定在此处以相同的形式放置它,所以,这不是最好的方法,为什么许多人会忽略它。我希望它能对您有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.