1
0
зеркало из https://github.com/glebtv/telegram-bot.git synced 2026-09-07 19:35:52 +03:00
Этот коммит содержится в:
Max Melentiev
2016-03-04 15:27:41 +06:00
родитель 2a511abe16
Коммит 32b6794924
4 изменённых файлов: 100 добавлений и 4 удалений

Просмотреть файл

@@ -166,7 +166,7 @@ class Telegram::WebhookController < Telegram::Bot::UpdatesController
end end
def read def read
session[:text] reply_with :message, text: session[:text]
end end
private private
@@ -215,13 +215,35 @@ call `.dispatch(bot, update)` on controller.
### Development & Debugging ### Development & Debugging
Use `rake telegram:bot:poller BOT=chat` to run poller. It'll automatically load Use `rake telegram:bot:poller` to run poller. It'll automatically load
changes without restart in development env. This task will not if you don't use changes without restart in development env. Optionally specify bot to run poller for
`telegram_webhooks`. with `BOT` envvar (`BOT=chat`).
This task will not work if you don't use `telegram_webhooks`.
You can run poller manually with You can run poller manually with
`Telegram::Bot::UpdatesPoller.start(bot, controller_class)`. `Telegram::Bot::UpdatesPoller.start(bot, controller_class)`.
### Testing
There is `Telegram::Bot::ClientStub` class to stub client for tests.
Instead of performing API requests it stores them in `requests` hash.
To stub all possible clients use `Telegram::Bot::ClientStub.stub_all!` before
initializing clients. Most likely you'll want something like this:
```ruby
RSpec.configure do |config|
# ...
Telegram.reset_bots
Telegram::Bot::ClientStub.stub_all!
config.after { Telegram.bot.reset }
# ...
end
```
There are also some helpers for controller tests.
Check out `telegram/bot/updates_controller/rspec_helpers`.
### Deploying ### Deploying
Use `rake telegram:bot:set_webhook` to update webhook url for all configured bots. Use `rake telegram:bot:set_webhook` to update webhook url for all configured bots.

Просмотреть файл

@@ -4,6 +4,35 @@ module Telegram
class ClientStub < Client class ClientStub < Client
attr_reader :requests attr_reader :requests
module StubbedConstructor
def new(*args)
if self == ClientStub || !ClientStub.stub_all?
super
else
ClientStub.new(args[1])
end
end
end
class << self
# Makes all
def stub_all!(enabled = true)
Client.extend(StubbedConstructor) unless Client < StubbedConstructor
return @_stub_all = enabled unless block_given?
begin
old = @_stub_all
stub_all!(enabled)
yield
ensure
stub_all!(old)
end
end
def stub_all?
@_stub_all
end
end
def initialize(username = nil) def initialize(username = nil)
@username = username @username = username
reset reset

Просмотреть файл

@@ -41,6 +41,13 @@ module Telegram
config[:default] = default if default config[:default] = default if default
end end
end end
# Resets all cached bots and their configs.
def reset_bots
@bots = nil
@bot = nil
@bots_config = nil
end
end end
end end
end end

38
spec/telegram/bot/client_stub_spec.rb Обычный файл
Просмотреть файл

@@ -0,0 +1,38 @@
RSpec.describe Telegram::Bot::ClientStub do
describe '#stub_all!' do
let(:client) { Telegram::Bot::Client.new('token', 'bot_name') }
let(:clients) { Telegram::Bot::Client.wrap(['token', token: 'token2']) }
shared_examples 'constructors' do |expected_class|
it 'makes Client.new return ClientStub' do
expect(client).to be_instance_of expected_class
expect(client.username).to eq 'bot_name'
end
it 'makes Client.wrap raturn ClientStub' do
expect(clients).to contain_exactly instance_of(expected_class),
instance_of(expected_class)
end
end
context 'when not used' do
include_examples 'constructors', Telegram::Bot::Client
end
context 'when enabled' do
around { |ex| described_class.stub_all! { ex.run } }
include_examples 'constructors', Telegram::Bot::ClientStub
end
context 'when redisabled' do
around do |ex|
described_class.stub_all! do
described_class.stub_all!(false) do
ex.run
end
end
end
include_examples 'constructors', Telegram::Bot::Client
end
end
end