public async Task UserEmailExistsRequestHandler_Handle__Returns_EmailNotExists() { UserContext.OpenInMemoryConnection(); try { UserEmailExistsResponse response; var request = new UserEmailExistsRequest { EmailAddress = "*****@*****.**" }; using (var context = UserContext.GetInMemoryContext()) { context.Users.Add(new User { EmailAddress = "*****@*****.**" }); context.SaveChanges(); } using (var context = UserContext.GetInMemoryContext()) { var handler = new UserEmailExistsRequestHandler(context); response = await handler.Handle(request); } Assert.IsFalse(response.EmailExists); } finally { UserContext.CloseInMemoryConnection(); } }
public async Task GetUserByKeyRequestHandler_Handle_Returns_NotExists() { UserContext.OpenInMemoryConnection(); try { GetUserByKeyResponse response; var request = new GetUserByKeyRequest(); using (var context = UserContext.GetInMemoryContext()) { context.Users.Add(new User { UserKey = Guid.NewGuid(), FirstName = "john", LastName = "doe" }); context.SaveChanges(); } using (var context = UserContext.GetInMemoryContext()) { var handler = new GetUserByKeyRequestHandler(context, AutoMapperHelper.BuildMapper(new MappingProfile())); response = await handler.Handle(request); } Assert.IsFalse(response.UserExists); Assert.IsNull(response.User); } finally { UserContext.CloseInMemoryConnection(); } }
public async Task UpdateUserRequestHandler_Handle_Returns_UserEmailExists() { UserContext.OpenInMemoryConnection(); try { var busMock = new Mock <IBus>(); busMock.Setup(m => m.PublishAsync(It.IsAny <UserUpdatedEvent>())).Returns(Task.FromResult(true)); UpdateUserResponse response; var request = new UpdateUserRequest() { UserKey = Guid.NewGuid(), FirstName = "Ellen", LastName = "Doe", EmailAddress = "*****@*****.**" }; using (var context = UserContext.GetInMemoryContext()) { context.Users.Add(new User { UserKey = request.UserKey, FirstName = "John", LastName = request.LastName, EmailAddress = "*****@*****.**" }); context.Users.Add(new User { UserKey = Guid.NewGuid(), EmailAddress = "*****@*****.**" }); context.SaveChanges(); } using (var context = UserContext.GetInMemoryContext()) { var handler = new UpdateUserRequestHandler(context, AutoMapperHelper.BuildMapper(new MappingProfile()), busMock.Object); response = await handler.Handle(request); } using (var context = UserContext.GetInMemoryContext()) { Assert.AreEqual(2, context.Users.Count()); Assert.AreNotEqual(request.EmailAddress, context.Users.Single(u => u.UserKey == request.UserKey).EmailAddress); Assert.AreEqual(ErrorType.EmailExists, response.ErrorType); Assert.IsFalse(response.IsSuccess); } busMock.Verify(m => m.PublishAsync(It.IsAny <UserUpdatedEvent>()), Times.Never()); } finally { UserContext.CloseInMemoryConnection(); } }
public async Task UpdateUserRequestHandler_Handle_Returns_User() { UserContext.OpenInMemoryConnection(); try { var busMock = new Mock <IBus>(); busMock.Setup(m => m.PublishAsync(It.IsAny <UserUpdatedEvent>())).Returns(Task.FromResult(true)); UpdateUserResponse response; var request = new UpdateUserRequest() { UserKey = Guid.NewGuid(), FirstName = "Ellen", LastName = "Doe" }; using (var context = UserContext.GetInMemoryContext()) { context.Users.Add(new User { UserKey = request.UserKey, FirstName = "John", LastName = request.LastName }); context.SaveChanges(); } using (var context = UserContext.GetInMemoryContext()) { var handler = new UpdateUserRequestHandler(context, AutoMapperHelper.BuildMapper(new MappingProfile()), busMock.Object); response = await handler.Handle(request); } using (var context = UserContext.GetInMemoryContext()) { Assert.AreEqual(1, context.Users.Count()); Assert.AreEqual(request.UserKey, context.Users.Single().UserKey); Assert.AreEqual(request.FirstName, context.Users.Single().FirstName); Assert.AreEqual(request.LastName, context.Users.Single().LastName); Assert.AreEqual(request.AddressLine1, context.Users.Single().AddressLine1); Assert.IsTrue(response.IsSuccess); } busMock.Verify(m => m.PublishAsync(It.Is <UserUpdatedEvent>(e => e.UserKey == request.UserKey && e.FirstName == request.FirstName)), Times.Once); } finally { UserContext.CloseInMemoryConnection(); } }
public async Task GetUserByEmailRequestHandler_Handle_Returns_User() { UserContext.OpenInMemoryConnection(); try { GetUserByEmailResponse response; var request = new GetUserByEmailRequest() { EmailAddress = "*****@*****.**" }; using (var context = UserContext.GetInMemoryContext()) { context.Users.Add(new User { EmailAddress = "*****@*****.**", FirstName = "john", LastName = "doe" }); context.SaveChanges(); } using (var context = UserContext.GetInMemoryContext()) { var handler = new GetUserByEmailRequestHandler(context, AutoMapperHelper.BuildMapper(new MappingProfile())); response = await handler.Handle(request); } using (var context = UserContext.GetInMemoryContext()) { Assert.IsTrue(response.UserExists); Assert.AreEqual(context.Users.Single().FirstName, response.User.FirstName); Assert.AreEqual(context.Users.Single().LastName, response.User.LastName); } } finally { UserContext.CloseInMemoryConnection(); } }
public async Task CreateUserRequestHandler_Handle_Returns_KeyEmpty() { UserContext.OpenInMemoryConnection(); try { var busMock = new Mock <IBus>(); busMock.Setup(m => m.PublishAsync(It.IsAny <UserCreatedEvent>())).Returns(Task.FromResult(true)); CreateUserResponse response; var request = new CreateUserRequest { FirstName = "John", LastName = "Doe" }; using (var context = UserContext.GetInMemoryContext()) { var handler = new CreateUserRequestHandler(context, AutoMapperHelper.BuildMapper(new MappingProfile()), busMock.Object); response = await handler.Handle(request); } using (var context = UserContext.GetInMemoryContext()) { Assert.AreEqual(0, context.Users.Count()); Assert.AreEqual(ErrorType.InvalidKey, response.ErrorType); Assert.IsFalse(response.IsSuccess); } busMock.Verify(m => m.PublishAsync(It.IsAny <UserCreatedEvent>()), Times.Never()); } finally { UserContext.CloseInMemoryConnection(); } }