From bd486100c04c13e78cfa776e23dfdab52687aaeb Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Thu, 1 Jun 2017 16:19:43 +0900 Subject: [PATCH] Remove stubs on DiscourseEvent in tests. --- lib/discourse_event.rb | 2 +- spec/components/post_creator_spec.rb | 23 +++++++++++-------- .../user_badges_controller_spec.rb | 15 ++++++++---- spec/models/user_spec.rb | 12 ++++++---- spec/support/discourse_event_helper.rb | 18 +++++++++++++++ 5 files changed, 51 insertions(+), 19 deletions(-) create mode 100644 spec/support/discourse_event_helper.rb diff --git a/lib/discourse_event.rb b/lib/discourse_event.rb index cc6a1f65d2d..6a45571bf25 100644 --- a/lib/discourse_event.rb +++ b/lib/discourse_event.rb @@ -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 diff --git a/spec/components/post_creator_spec.rb b/spec/components/post_creator_spec.rb index dcfecfe83fb..39fe1443fed 100644 --- a/spec/components/post_creator_spec.rb +++ b/spec/components/post_creator_spec.rb @@ -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 diff --git a/spec/controllers/user_badges_controller_spec.rb b/spec/controllers/user_badges_controller_spec.rb index 7e2cda7f044..3fb4d2ba22c 100644 --- a/spec/controllers/user_badges_controller_spec.rb +++ b/spec/controllers/user_badges_controller_spec.rb @@ -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 diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 8100e927820..cf0bd01d015 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -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 diff --git a/spec/support/discourse_event_helper.rb b/spec/support/discourse_event_helper.rb new file mode 100644 index 00000000000..0309a5c6fab --- /dev/null +++ b/spec/support/discourse_event_helper.rb @@ -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