Skip to content
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

NoMethodError when using RubyEventStore::RSpec::Publish matcher with InMemoryRepository and preceding events #1670

Closed
silvabox opened this issue Aug 29, 2023 · 3 comments · Fixed by #1672
Assignees

Comments

@silvabox
Copy link

silvabox commented Aug 29, 2023

When using the Publish matcher in the following manner:

before do
  # some code that publishes an event to `other_event_stream`
end

# ...

expect { subject }.to publish(SomeEvent)
  .in(event_store)
  .in_stream(stream_name)

...using the InMemoryRepository, we get the error NoMethodError: undefined method '+' for nil:NilClass.

This happens as a result of the call in Publish:

      def matches?(event_proc)
        fetch_events.from_last
        event_proc.call
        stream_names.all? do |stream_name|
          fetch_events.stream(stream_name)

The initial call to fetch_events.from_last will set the spec.start to an event_id from an arbitrary stream published to in the preamble (in this example a before block. A let! block has the same effect).
The subsequent fetch_events.stream(stream_name) then causes an error in InMemoryRepository:153:

      serialized_records = serialized_records.drop(index_of(serialized_records, spec.start) + 1) if spec.start

since index_of(serialized_records, spec.start) is nil (i.e. nil + 1)

I would propose that if the event specified in spec.start is not present in serialized_records, then it should simply iterate from the beginning of the collection.

@mostlyobvious mostlyobvious moved this to Todo in Backlog Aug 29, 2023
@pjurewicz pjurewicz self-assigned this Aug 30, 2023
@pjurewicz
Copy link
Contributor

Hello @silvabox! Thank you very much for sharing this problem.
I've just managed to reproduce your case with this simple test:

      specify do
        event_store.publish(FooEvent.new, stream_name: "Foo$1")
        expect do
          event_store.publish(FooEvent.new, stream_name: "Foo$2")
        end.to publish(FooEvent)
                 .in(event_store)
                 .in_stream("Foo$2")
      end

We are working on a fix.

@mostlyobvious mostlyobvious moved this from Todo to In Progress in Backlog Aug 30, 2023
@silvabox
Copy link
Author

That's fantastic, thank you @pjurewicz for responding so quickly 😌

@pjurewicz
Copy link
Contributor

Hello @silvabox
This issue is resolved in ruby_event_store-rspec version 2.12.0.
Thank you again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants