public void Changing_email_from_corporate_to_non_corporate() { // Arrange var db = new Database(ConnectionString); User user = CreateUser( "*****@*****.**", UserType.Employee, db); CreateCompany("mycorp.com", 1, db); var busSpy = new BusSpy(); var messageBus = new MessageBus(busSpy); var loggerMock = new Mock <IDomainLogger>(); var sut = new UserController(db, messageBus, loggerMock.Object); // Act string result = sut.ChangeEmail(user.UserId, "*****@*****.**"); // Assert Assert.Equal("OK", result); object[] userData = db.GetUserById(user.UserId); User userFromDb = UserFactory.Create(userData); Assert.Equal("*****@*****.**", userFromDb.Email); Assert.Equal(UserType.Customer, userFromDb.Type); object[] companyData = db.GetCompany(); Company companyFromDb = CompanyFactory.Create(companyData); Assert.Equal(0, companyFromDb.NumberOfEmployees); busSpy.ShouldSendNumberOfMessages(1) .WithEmailChangedMessage(user.UserId, "*****@*****.**"); loggerMock.Verify( x => x.UserTypeHasChanged( user.UserId, UserType.Employee, UserType.Customer), Times.Once); }
public string ChangeEmail(int userId, string newEmail) { object[] userData = _database.GetUserById(userId); User user = UserFactory.Create(userData); string error = user.CanChangeEmail(); if (error != null) { return(error); } object[] companyData = _database.GetCompany(); Company company = CompanyFactory.Create(companyData); user.ChangeEmail(newEmail, company); _database.SaveCompany(company); _database.SaveUser(user); _eventDispatcher.Dispatch(user.DomainEvents); return("OK"); }