public void ApplyingPreviouslyGeneratedEvents_GivenNewAggregateObject_ShouldBringAggregateBackToPreviousState() { // Arrange var user = new User(); user.Register(); var events = new object[] {new UserRegistered(user.Id), new UserChangedPassword("newpassword"), new UserChangedPassword("newerpassword") }; // Act user.ApplyAll(events); // Assert Assert.Throws<InvalidOperationException>(() => user.ChangePassword("newerpassword"), "Expected exception stating the new password must be different the the previous one, indicating that previous events have replayed successfully."); }
public void GivenAggregateWithUncommittedEvents_WhenSaving_ThenUncommittedEventsShouldBeAccepted() { // Arrange var user = new User(); user.Register(); var repository = new EventSourcingRepository<User>(new InMemoryEventStore(), new Mock<IConcurrencyMonitor>().Object); // Act repository.Save(user); // Assert CollectionAssert.IsEmpty(user.GetUncommittedEvents()); }
public void GivenEmptyEventStore_WhenAddingEventsInBulk_ThenStoreShouldContainThoseEvents() { // Arrange IEventStore store = new InMemoryEventStore(); var user = new User(); user.Register(); user.ChangePassword("newpassword"); // Act store.Store(user.Id, user.GetUncommittedEvents()); // Assert CollectionAssert.IsNotEmpty(((InMemoryEventStore)store).Events); CollectionAssert.AreEqual(user.GetUncommittedEvents(), ((InMemoryEventStore)store).Events); }
public void GivenAggregateWithUncommittedEvents_WhenSaved_ThenEventStoreShouldContainThoseEvents() { // Arrange var eventStore = new InMemoryEventStore(); IRepository<User> repository = new EventSourcingRepository<User>(eventStore, new Mock<IConcurrencyMonitor>().Object); var user = new User(); user.Register(); var expectedEvents = new object[] { new UserRegistered(user.Id) }; // Act repository.Save(user); // Assert CollectionAssertAreJsonEqual(expectedEvents, eventStore.Events); }
public void AcceptingEvents_GivenAggregateWithUncommittedEvents_ShouldClearUncommittedEvents() { // Arrange var user = new User(); user.Register(); user.ChangePassword("newpassword"); // Act var userRegisteredEvent = new UserRegistered(user.Id); var userChangedPasswordEvent = new UserChangedPassword("newpassword") { ParentVersion = userRegisteredEvent.Version }; IEnumerable<object> expectedBefore = new object[] { userRegisteredEvent, userChangedPasswordEvent }; IEnumerable<object> expectedAfter = new object[0]; IEnumerable<object> before = user.GetUncommittedEvents(); user.AcceptUncommittedEvents(); IEnumerable<object> after = user.GetUncommittedEvents(); // Assert CollectionAssertAreJsonEqual(expectedBefore, before); CollectionAssertAreJsonEqual(expectedAfter, after); }
public void GivenEventStorePopulatedWithEventsForMultipleAggregates_WhenLoadingEventsForAnAggregate_ThenShouldReturnEventsForThatAggregate() { // Arrange IEventStore store = new InMemoryEventStore(); var user1 = new User(); user1.Register(); user1.ChangePassword("user1pwd1"); user1.ChangePassword("user1pwd2"); var user2 = new User(); user2.Register(); user2.ChangePassword("user2pwd1"); user2.ChangePassword("user2pwd2"); store.Store(user1.Id, user1.GetUncommittedEvents()); store.Store(user2.Id, user2.GetUncommittedEvents()); // Act IEnumerable<object> eventsForUser1 = store.Load(user1.Id); // Assert CollectionAssert.AreEqual(user1.GetUncommittedEvents(), eventsForUser1, "Store didn't return user1's events properly."); }
public void GivenAnyAggregateRoot_WhenBehaviourIsInvokedAndEventsRaised_ThenBaseVersionShouldNotChange() { // Arrange var user = new User(); user.Register(); var baseVersion = user.BaseVersion; // Act user.ChangePassword("newpassword"); // Assert Assert.AreEqual(baseVersion, user.BaseVersion, "User's base version is not correct."); }
public void GivenNewAggreateWithNewEvents_WhenSaving_ThenShouldNotBotherCheckingConcurrency() { // Arrange var user = new User(); user.Register(); var concurrencyMonitor = new Mock<IConcurrencyMonitor>(); var repository = new EventSourcingRepository<User>(new InMemoryEventStore(), concurrencyMonitor.Object); // Act repository.Save(user); // Assert concurrencyMonitor.Verify(monitor => monitor.CheckForConflicts(It.IsAny<IEnumerable<object>>(), It.IsAny<IEnumerable<object>>()), Times.Never()); }
public void InvokingBehaviour_GivenSimpleAggregateRoot_ShouldRecordEvents() { // Arrange var user = new User(); user.Register(); // Act user.ChangePassword("newpassword"); IEnumerable<object> actual = user.GetUncommittedEvents(); var userRegisteredEvent = new UserRegistered(user.Id); var userChangedPasswordEvent = new UserChangedPassword("newpassword") { ParentVersion = userRegisteredEvent.Version }; IEnumerable<object> expected = new object[] { userRegisteredEvent, userChangedPasswordEvent }; // Assert CollectionAssertAreJsonEqual(expected, actual); }
public void InvokingBehaviour_GivenAggregateWithInvariantLogic_ShouldFailIfInvariantIsNotSatisfied() { // Arrange var user = new User(); user.Register(); user.ChangePassword("newpassword"); // Act / Assert Assert.Throws<InvalidOperationException>(() => user.ChangePassword("newpassword"), "Expected exception stating the new password must be different the the previous one."); }