Remove stubs on DiscourseEvent in tests.

This commit is contained in:
Guo Xiang Tan 2017-06-01 16:19:43 +09:00
parent 9c40b6c1ec
commit bd486100c0
5 changed files with 51 additions and 19 deletions

View File

@ -1,6 +1,6 @@
# This is meant to be used by plugins to trigger and listen to events # This is meant to be used by plugins to trigger and listen to events
# So we can execute code when things happen. # So we can execute code when things happen.
module DiscourseEvent class DiscourseEvent
# Defaults to a hash where default values are empty sets. # Defaults to a hash where default values are empty sets.
def self.events def self.events

View File

@ -77,16 +77,19 @@ describe PostCreator do
end end
it "triggers extensibility events" do it "triggers extensibility events" do
DiscourseEvent.expects(:trigger).with(:before_create_post, anything).once events = DiscourseEvent.track_events { creator.create }
DiscourseEvent.expects(:trigger).with(:validate_post, anything).once
DiscourseEvent.expects(:trigger).with(:topic_created, anything, anything, user).once expect(events.map { |event| event[:event_name] }).to include(
DiscourseEvent.expects(:trigger).with(:post_created, anything, anything, user).once :before_create_post,
DiscourseEvent.expects(:trigger).with(:after_validate_topic, anything, anything).once :validate_post,
DiscourseEvent.expects(:trigger).with(:before_create_topic, anything, anything).once :topic_created,
DiscourseEvent.expects(:trigger).with(:after_trigger_post_process, anything).once :post_created,
DiscourseEvent.expects(:trigger).with(:markdown_context, anything).at_least_once :after_validate_topic,
DiscourseEvent.expects(:trigger).with(:topic_notification_level_changed, anything, anything, anything).at_least_once :before_create_topic,
creator.create :after_trigger_post_process,
:markdown_context,
:topic_notification_level_changed,
)
end end
it "does not notify on system messages" do it "does not notify on system messages" do

View File

@ -103,8 +103,12 @@ describe UserBadgesController do
it 'will trigger :user_badge_granted' do it 'will trigger :user_badge_granted' do
log_in :admin log_in :admin
user user
DiscourseEvent.expects(:trigger).with(:user_badge_granted, anything, anything).once
event = DiscourseEvent.track_events do
xhr :post, :create, badge_id: badge.id, username: user.username xhr :post, :create, badge_id: badge.id, username: user.username
end.first
expect(event[:event_name]).to eq(:user_badge_granted)
end end
end end
@ -127,8 +131,11 @@ describe UserBadgesController do
it 'will trigger :user_badge_removed' do it 'will trigger :user_badge_removed' do
log_in :admin log_in :admin
DiscourseEvent.expects(:trigger).with(:user_badge_removed, anything, anything).once event = DiscourseEvent.track_events do
xhr :delete, :destroy, id: user_badge.id xhr :delete, :destroy, id: user_badge.id
end.first
expect(event[:event_name]).to eq(:user_badge_removed)
end end
end end
end end

View File

@ -81,8 +81,10 @@ describe User do
it 'triggers a extensibility event' do it 'triggers a extensibility event' do
user && admin # bypass the user_created event user && admin # bypass the user_created event
DiscourseEvent.expects(:trigger).with(:user_approved, user).once event = DiscourseEvent.track_events { user.approve(admin) }.first
user.approve(admin)
expect(event[:event_name]).to eq(:user_approved)
expect(event[:params].first).to eq(user)
end end
context 'after approval' do context 'after approval' do
@ -181,8 +183,10 @@ describe User do
end end
it 'triggers an extensibility event' do it 'triggers an extensibility event' do
DiscourseEvent.expects(:trigger).with(:user_created, subject).once event = DiscourseEvent.track_events { subject.save! }.first
subject.save!
expect(event[:event_name]).to eq(:user_created)
expect(event[:params].first).to eq(subject)
end end
context 'after_save' do context 'after_save' do

View File

@ -0,0 +1,18 @@
module DiscourseEvent::TestHelper
def trigger(event_name, *params)
super(event_name, *params)
if @events_trigger
@events_trigger << { event_name: event_name, params: params }
end
end
def track_events
@events_trigger = events_trigger = []
yield
@events_trigger = nil
events_trigger
end
end
DiscourseEvent.singleton_class.prepend DiscourseEvent::TestHelper