# New helper to test \`DiscourseEvents\` triggers in RSpec

**URL:** https://meta.discourse.org/t/new-helper-to-test-discourseevents-triggers-in-rspec/63729
**Category:** Development
**Created:** [2017年六月1日 12:50 UTC](https://meta.discourse.org/t/new-helper-to-test-discourseevents-triggers-in-rspec/63729 "2017-06-01T12:50:51Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)
#### Post date: [2017年六月1日 12:50 UTC](https://meta.discourse.org/t/new-helper-to-test-discourseevents-triggers-in-rspec/63729/1 "2017-06-01T12:50:51Z")

</div>

Previously, we used to test that a `DiscourseEvent` has been triggered by mocking the method call using Mocha which looks something like:

`DiscourseEvent.expects(:trigger).with(:user_created)`

However, these mocks make test cases super brittle because the test case would fail when another `DiscourseEvent` that is not “expected” is triggered. As a result, the developer would have to mock every single `DiscourseEvent` trigger that would trigger even if he is only concerned about testing a single trigger.

To work around this problem, I’ve introduced a new [helper](https://github.com/discourse/discourse/blob/76510d695ffb21be0a7b8f43524d69ea98375127/spec/support/discourse_event_helper.rb), inspired by our [MessageBus diagnostic helper](https://github.com/discourse/discourse/blob/76510d695ffb21be0a7b8f43524d69ea98375127/spec/support/diagnostics_helper.rb), that would track all the events that were triggered when the given block of code is executed.

Examples of how to use the new helper can be found in

[https://github.com/discourse/discourse/commit/bd486100c04c13e78cfa776e23dfdab52687aaeb](https://github.com/discourse/discourse/commit/bd486100c04c13e78cfa776e23dfdab52687aaeb)
