Move ‘dummy provider’ logic into a shared context, to allow for use by multiple specs

This commit is contained in:
David Taylor 2017-07-10 18:19:59 +01:00
parent 52b8e3f3a6
commit ba484dc9a1
2 changed files with 41 additions and 30 deletions

38
spec/dummy_provider.rb Normal file
View File

@ -0,0 +1,38 @@
RSpec.shared_context "dummy provider" do
before(:each) do
if defined? ::DiscourseChat::Provider::DummyProvider
::DiscourseChat::Provider.send(:remove_const, :DummyProvider)
end
module ::DiscourseChat::Provider::DummyProvider
PROVIDER_NAME = "dummy".freeze
PROVIDER_ENABLED_SETTING = :chat_integration_enabled # Tie to main plugin enabled setting
@@sent_messages = []
@@raise_exception = nil
def self.trigger_notification(post, channel)
if @@raise_exception
raise @@raise_exception
end
@@sent_messages.push(post: post.id, channel: channel)
end
def self.sent_messages
@@sent_messages
end
def self.set_raise_exception(bool)
@@raise_exception = bool
end
end
end
let(:provider){::DiscourseChat::Provider::DummyProvider}
end
RSpec.configure do |rspec|
rspec.include_context "dummy provider"
end

View File

@ -1,5 +1,6 @@
require 'rails_helper'
require_dependency 'post_creator'
require_relative '../../dummy_provider'
RSpec.describe DiscourseChat::Manager do
@ -10,40 +11,12 @@ RSpec.describe DiscourseChat::Manager do
let(:second_post) {Fabricate(:post, topic: topic, post_number:2)}
describe '.trigger_notifications' do
include_context "dummy provider"
before do
SiteSetting.chat_integration_enabled = true
end
before(:each) do
module ::DiscourseChat::Provider::DummyProvider
PROVIDER_NAME = "dummy".freeze
PROVIDER_ENABLED_SETTING = :chat_integration_enabled # Tie to main plugin enabled setting
@@sent_messages = []
@@raise_exception = nil
def self.trigger_notification(post, channel)
if @@raise_exception
raise @@raise_exception
end
@@sent_messages.push(post: post.id, channel: channel)
end
def self.sent_messages
@@sent_messages
end
def self.set_raise_exception(bool)
@@raise_exception = bool
end
end
end
after(:each) do
::DiscourseChat::Provider.send(:remove_const, :DummyProvider)
end
let(:provider) {::DiscourseChat::Provider::DummyProvider}
def create_rule(provider, channel, filter, category_id, tags) # Just shorthand for testing purposes
DiscourseChat::Rule.new({provider: provider, channel: channel, filter:filter, category_id:category_id, tags:tags}).save!
end