charlessolar / Aggregates.NET

.NET event sourced domain driven design model via NServiceBus and GetEventStore

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Testing - Child Entity - Multipe Events

galenp opened this issue · comments

commented

Hi Charles

I have a child entity that is raising multiple events when processing a command.
I want to test that the event data is correct but values of the properties when the event is retreived through .Raised<> are blank.

  context.UoW
      .Check<Visit>(visitId)
      .Check<VisitPlan>(planId)
      .Raised<ScopeUpdated>(@event =>
      {
          @event.PlanId.Should().Be(planId.GuidId);
          @event.VisitId.Should().Be(visitId.GuidId);
          @event.Version.Should().Be(nextVersion);                
          return true;
      })
      .Raised<SubmittedForApproval>(@event => {
       @event.Version.Should().Be(nextVersion); // the properties are this @event are all empty (i.e. int = 0, string = null etc)
       return true;
      });

I've confirmed in the Event handler in the EntityState that the event properties are populated and correct. It's just when the event is accessed in this scenario.

The first event comes through ok, so it makes me think its some casting issue of the event type for subsequent events. Maybe because all events inherit IStampedEvent or something.

I've only seen this issue when dealing with subsequent events from a child entity if that helps.

Thanks

Curious - if you swapped the Raised calls ie

context.UoW
      .Check<Visit>(visitId)
      .Check<VisitPlan>(planId)
      .Raised<SubmittedForApproval>(@event => {
       @event.Version.Should().Be(nextVersion); // the properties are this @event are all empty (i.e. int = 0, string = null etc)
       return true;
      })
      .Raised<ScopeUpdated>(@event =>
      {
          @event.PlanId.Should().Be(planId.GuidId);
          @event.VisitId.Should().Be(visitId.GuidId);
          @event.Version.Should().Be(nextVersion);                
          return true;
      });

Does the ScopedUpdated check work?

AFAIK - off hand - the raised method checks the event type against the list of raised events, it won't go up the inheritance chain if that's what you are concerned about.

Looking into the double raised issue in any case!

Hi @galenp sorry for the delay and confusion!

So I was writing up unit tests to cover these issues you had and realized what the problem is. Raised is not designed to work like that. Raised takes a factory constructor to create an event object to compare to the list of raised events while processing the message.

The intended way to do this test would be

context.UoW
      .Check<Visit>(visitId)
      .Check<VisitPlan>(planId)
      .Raised<ScopeUpdated>(@event =>
      {
          @event.PlanId = planId.GuidId;
          @event.VisitId = visitId.GuidId;
          @event.Version = nextVersion;  
      })
      .Raised<SubmittedForApproval>(@event => {
       @event.Version = nextVersion; 
      });

The Raised methods are building events not for asserting on their contents. Raised will throw itself if the list of events raised during processing does not contain an exact match to the event you're checking.

See Testable.cs L84

Theres a fuller example of the testing project's capabilities here https://github.com/charlessolar/Aggregates.NET/wiki/Testing#testing-entities