在 Discourse 中,我试图设置一个用户创建的事件挂钩,但是当我这样做时,它会破坏一些与帖子创建相关的测试。新事件 - 在另一个模型上 - 似乎导致 topic_created 多次触发,同时阻止 post_created 触发。
在 /app/models/user.rb
after_create do
DiscourseEvent.trigger(:user_created, self)
end
相关测试输出
Failures:
1) PostCreator new topic success triggers extensibility events
Failure/Error: creator.create
Mocha::ExpectationError:
unexpected invocation: DiscourseEvent.trigger(:topic_created, #<Topic:0x7fcbdc666ae8>, {:title => 'hello world topic', :raw => 'my name is fred', :archetype_id => 1}, #<User:0x7fcbdd907c10>)
unsatisfied expectations:
- expected exactly once, not yet invoked: DiscourseEvent.trigger(:post_created, anything, anything, #<User:0x7fcbdd907c10>)
- expected exactly once, invoked twice: DiscourseEvent.trigger(:topic_created, anything, anything, #<User:0x7fcbdd907c10>)
satisfied expectations:
- expected at least once, invoked 4 times: DiscourseEvent.trigger(:markdown_context, anything)
- expected exactly once, invoked once: DiscourseEvent.trigger(:after_trigger_post_process, anything)
- expected exactly once, invoked once: DiscourseEvent.trigger(:before_create_topic, anything, anything)
- expected exactly once, invoked once: DiscourseEvent.trigger(:after_validate_topic, anything, anything)
- expected exactly once, invoked once: DiscourseEvent.trigger(:validate_post, anything)
- expected exactly once, invoked once: DiscourseEvent.trigger(:before_create_post, anything)
# ./lib/post_creator.rb:220:in `trigger_after_events'
# ./lib/post_creator.rb:150:in `create'
# ./spec/components/post_creator_spec.rb:82:in `block (4 levels) in <top (required)>'