зеркало из
https://github.com/glebtv/telegram-bot.git
synced 2026-09-04 18:25:51 +03:00
Merge pull request #31 from telegram-bot-rb/integration_test
Integration test with rails app and two bots
Этот коммит содержится в:
1
.gitignore
поставляемый
1
.gitignore
поставляемый
@@ -6,4 +6,5 @@
|
|||||||
/doc/
|
/doc/
|
||||||
/pkg/
|
/pkg/
|
||||||
/spec/reports/
|
/spec/reports/
|
||||||
|
/log/
|
||||||
/tmp/
|
/tmp/
|
||||||
|
|||||||
@@ -1,3 +1,7 @@
|
|||||||
|
# Unreleased
|
||||||
|
|
||||||
|
- ClientStub saves and returns token. Fixes testing multiple bots.
|
||||||
|
|
||||||
# 0.11.3
|
# 0.11.3
|
||||||
|
|
||||||
- Release dependencies for Rails 5.1.
|
- Release dependencies for Rails 5.1.
|
||||||
|
|||||||
25
Gemfile
25
Gemfile
@@ -1,16 +1,22 @@
|
|||||||
source 'https://rubygems.org'
|
source 'https://rubygems.org'
|
||||||
gemspec
|
gemspec
|
||||||
|
|
||||||
case ENV['RAILS']
|
|
||||||
when '5_1'
|
|
||||||
gem 'actionpack', '5.1.0'
|
|
||||||
when '5'
|
|
||||||
gem 'actionpack', '5.0.2'
|
|
||||||
when '4'
|
|
||||||
gem 'actionpack', '~> 4.2'
|
|
||||||
end
|
|
||||||
|
|
||||||
group :development do
|
group :development do
|
||||||
|
case ENV['RAILS']
|
||||||
|
when '5_1'
|
||||||
|
gem 'railties', '5.1.0'
|
||||||
|
gem 'actionpack', '5.1.0'
|
||||||
|
when '5'
|
||||||
|
gem 'railties', '5.0.2'
|
||||||
|
gem 'actionpack', '5.0.2'
|
||||||
|
when '4'
|
||||||
|
gem 'railties', '~> 4.2'
|
||||||
|
gem 'actionpack', '~> 4.2'
|
||||||
|
else
|
||||||
|
gem 'railties'
|
||||||
|
gem 'actionpack'
|
||||||
|
end
|
||||||
|
|
||||||
gem 'sdoc', '~> 0.4.1'
|
gem 'sdoc', '~> 0.4.1'
|
||||||
gem 'pry', '~> 0.10.1'
|
gem 'pry', '~> 0.10.1'
|
||||||
gem 'pry-byebug', '~> 3.2.0'
|
gem 'pry-byebug', '~> 3.2.0'
|
||||||
@@ -19,6 +25,7 @@ group :development do
|
|||||||
|
|
||||||
gem 'rspec', '~> 3.5.0'
|
gem 'rspec', '~> 3.5.0'
|
||||||
gem 'rspec-its', '~> 1.1.0'
|
gem 'rspec-its', '~> 1.1.0'
|
||||||
|
gem 'rspec-rails', '~> 3.5.0'
|
||||||
|
|
||||||
gem 'rubocop', '~> 0.37.0'
|
gem 'rubocop', '~> 0.37.0'
|
||||||
|
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ module Telegram
|
|||||||
end
|
end
|
||||||
|
|
||||||
def initialize(token = nil, username = nil, **options)
|
def initialize(token = nil, username = nil, **options)
|
||||||
|
@token = token
|
||||||
@username = username || options[:username] || token
|
@username = username || options[:username] || token
|
||||||
reset
|
reset
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ RSpec.shared_context 'telegram/bot/integration' do
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
let(:clear_session?) { described_class.respond_to?(:session_store) }
|
let(:clear_session?) { described_class.respond_to?(:session_store) }
|
||||||
before { described_class.session_store.clear if clear_session? }
|
before { described_class.session_store.try!(:clear) if clear_session? }
|
||||||
|
|
||||||
include Telegram::Bot::RSpec::ClientMatchers
|
include Telegram::Bot::RSpec::ClientMatchers
|
||||||
|
|
||||||
|
|||||||
8
spec/integration/requests/default_bot_spec.rb
Обычный файл
8
spec/integration/requests/default_bot_spec.rb
Обычный файл
@@ -0,0 +1,8 @@
|
|||||||
|
require 'integration_helper'
|
||||||
|
|
||||||
|
RSpec.describe DefaultBotController, :telegram_bot, type: :request do
|
||||||
|
describe '#start' do
|
||||||
|
subject { -> { dispatch_command :start } }
|
||||||
|
it { should respond_with_message 'from default' }
|
||||||
|
end
|
||||||
|
end
|
||||||
9
spec/integration/requests/other_bot_spec.rb
Обычный файл
9
spec/integration/requests/other_bot_spec.rb
Обычный файл
@@ -0,0 +1,9 @@
|
|||||||
|
require 'integration_helper'
|
||||||
|
|
||||||
|
RSpec.describe OtherBotController, :telegram_bot, type: :request do
|
||||||
|
let(:bot) { Telegram.bots[:other] }
|
||||||
|
describe '#start' do
|
||||||
|
subject { -> { dispatch_command :start } }
|
||||||
|
it { should respond_with_message 'from other' }
|
||||||
|
end
|
||||||
|
end
|
||||||
63
spec/integration_helper.rb
Обычный файл
63
spec/integration_helper.rb
Обычный файл
@@ -0,0 +1,63 @@
|
|||||||
|
require 'telegram/bot/rspec/integration'
|
||||||
|
require 'action_controller'
|
||||||
|
require 'action_dispatch'
|
||||||
|
require 'action_dispatch/testing/integration'
|
||||||
|
|
||||||
|
require 'rails'
|
||||||
|
require 'rspec/rails/adapters'
|
||||||
|
require 'rspec/rails/fixture_support'
|
||||||
|
require 'rspec/rails/example/rails_example_group'
|
||||||
|
require 'rspec/rails/example/request_example_group'
|
||||||
|
|
||||||
|
ENV['RAILS_ENV'] = 'test'
|
||||||
|
class TestApplication < Rails::Application
|
||||||
|
config.eager_load = false
|
||||||
|
config.log_level = :debug
|
||||||
|
secrets[:secret_key_base] = 'test'
|
||||||
|
secrets[:telegram] = {
|
||||||
|
bot: 'default_token',
|
||||||
|
bots: {
|
||||||
|
other: {token: 'other_token'},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
end
|
||||||
|
Rails.application.initialize!
|
||||||
|
|
||||||
|
# # Controllers
|
||||||
|
class DefaultBotController < Telegram::Bot::UpdatesController
|
||||||
|
def start(*)
|
||||||
|
respond_with :message, text: 'from default'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class OtherBotController < Telegram::Bot::UpdatesController
|
||||||
|
def start(*)
|
||||||
|
respond_with :message, text: 'from other'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
RSpec.configure do |config|
|
||||||
|
config.include RSpec::Rails::RequestExampleGroup, type: :request
|
||||||
|
|
||||||
|
config.around type: :request do |ex|
|
||||||
|
begin
|
||||||
|
Telegram.reset_bots
|
||||||
|
Telegram::Bot::ClientStub.stub_all!
|
||||||
|
ex.run
|
||||||
|
ensure
|
||||||
|
Telegram.reset_bots
|
||||||
|
Telegram::Bot::ClientStub.stub_all!(false)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
config.before type: :request do
|
||||||
|
# Redefine routes before every example, so it does not depent on order.
|
||||||
|
Rails.application.routes.draw do
|
||||||
|
require 'telegram/bot/routes_helper'
|
||||||
|
extend Telegram::Bot::RoutesHelper
|
||||||
|
|
||||||
|
telegram_webhooks default: DefaultBotController,
|
||||||
|
other: OtherBotController
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -46,7 +46,7 @@ RSpec.describe Telegram::Bot::ClientStub do
|
|||||||
|
|
||||||
context 'when username and token are given' do
|
context 'when username and token are given' do
|
||||||
let(:args) { %w(token superbot) }
|
let(:args) { %w(token superbot) }
|
||||||
its(:token) { should eq nil }
|
its(:token) { should eq args[0] }
|
||||||
its(:username) { should eq args[1] }
|
its(:username) { should eq args[1] }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -72,6 +72,7 @@ RSpec.describe Telegram::Bot::ConfigMethods do
|
|||||||
|
|
||||||
context 'when not configured' do
|
context 'when not configured' do
|
||||||
let(:registry) { Object.new.tap { |x| x.extend described_class } }
|
let(:registry) { Object.new.tap { |x| x.extend described_class } }
|
||||||
|
before { hide_const('Rails') }
|
||||||
it { should eq({}) }
|
it { should eq({}) }
|
||||||
|
|
||||||
context 'in rails environment' do
|
context 'in rails environment' do
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user