【发布时间】:2020-07-13 11:28:48
【问题描述】:
当我运行 rails c 时,我可以调用以下类并且该方法有效:
test = SlackService::BoardGameNotifier
test.create_alert("test")
>>method works
我正在尝试像这样在 rspec 中进行设置:
require 'spec_helper'
require 'slack-notifier'
RSpec.describe SlackService::BoardGameNotifier do
describe '#notify' do
@notifier = SlackService::BoardGameNotifier
it 'pings Slack' do
error = nil
message = "test"
expect(notifier).to receive(:ping).with(message)
notifier.send_message()
end
end
end
但我不断收到错误:
NameError:
uninitialized constant SlackService
这与我如何设置模块有关吗?
我目前的设置:
slack_service/board_game_notifier.rb
module SlackService
class BoardGameNotifier < BaseNotifier
WEBHOOK_URL = Rails.configuration.x.slack.url
DEFAULT_OPTIONS = {
channel: "board-games-channel",
text: "board games alert",
username: "bot",
}
def create_alert(message)
message #testing
end
end
end
slack_service/base_notifier.rb
module SlackService
class BaseNotifier
include Singleton
def initialize
webhook_url = self.class::WEBHOOK_URL
options = self.class::DEFAULT_OPTIONS
@notifier = Slack::Notifier.new(webhook_url, options)
end
def self.send_message
message = instance.create_alert("test")
instance.notify(message)
end
def notify(message)
@notifier.post blocks: message
end
end
end
【问题讨论】: