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
# So we can execute code when things happen.
module DiscourseEvent
class DiscourseEvent
# Defaults to a hash where default values are empty sets.
def self.events

View File

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

View File

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

View File

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