-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add stubbing test helpers for the event bus #4214
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sad that we can't use once
or similar, but this is a great starting point. Thanks @waiting-for-dev!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very cool. Might be nice to have it print out the difference in arguments when they're provided, but this is a great initial implementation.
Thanks, @adammathys. Yeah, I agree. Unfortunately, there's no straightforward way to reuse the logic in RSpec's |
@waiting-for-dev this looks so convenient! I was thinking that we could also use rspec metadata so we don't need to explicitly write it 'fires foo event', :stub_events do
Spree::Event.fire 'foo'
expect('foo').to have_been_fired
end what do you think? |
@spaghetticode, I think it's a great idea to use it that way! However, I'm not particularly eager to modify RSpec configuration from a library. I think that's something that we could address from the docs point of view. How do you feel about that? |
I think I get what you mean, but I'm not sure I would consider that addition something that intrusive, the possible clash with other metadata names is unlikely, but if this is a concern then the name could be changed to |
This commit adds a `Spree::TestingSupport::EventHelpers` module which adds stubbing helpers for `Spree::Event` when included in an RSpec file. It adds a RSpec matcher, `have_been_fired`, which can be used after the event bus has been stubbed through `stub_spree_events`. Internally, it leans on the `have_received` built-in `rspec-mocks` matcher. Unfortunately, RSpec's matcher lacks good extensibility, so we can't provide OOTB with all its bells and whistles (like `once`, `exactly(2).times`), and the effort is not worth it at this point. However, we do support a `with` modifier for `have_been_fired` to match the published payload. Example: ```ruby require 'rails_helper' require 'spree/testing_support/event_helpers' RSpec.describe MyClass do include Spree::TestingSupport::EventHelpers it 'does stuff with events' do stub_spree_events described_class.new.do_stuff_with_events expect('foo').to have_been_fired expect('bar').to have_been_fired.with(a_hash_containing(foo: :bar)) end end ```
698cf4b
to
e2a0ac9
Compare
I think you're right, and it makes it consistent with the approach used elsewhere in the codebase. I just renamed the method to
Thanks for bringing up those points. That's entirely a valid reason! I think my hesitance grounds in two points:
Considering that the whole stubbing approach is not my favorite one (I'm more in favor of using #4204), I wouldn't like to make it too easy to use if you get my point. For instance, if someone adds On top of that, it'll be a one-liner to add it the user application. Otherwise, Now, I understand that those are all opinable reasons. But, if you don't have strong opinions about it, I'd prefer to leave it as it is for now. It'll always be easier to add it than to remove it 🙂 |
Description
This commit adds a
Spree::TestingSupport::EventHelpers
module whichadds stubbing helpers for
Spree::Event
when included in an RSpec file.It adds a RSpec matcher,
have_been_fired
, which can be used after theevent bus has been stubbed through
stub_events
.Internally, it leans on the
have_received
built-inrspec-mocks
matcher. Unfortunately, RSpec's matcher lacks good extensibility, so we
can't provide OOTB with all its bells and whistles (like
once
,exactly(2).times
), and the effort is not worth it at this point.However, we do support a
with
modifier forhave_been_fired
to matchthe published payload.
Example:
Checklist: