public void SetsCommandPrincipalProperty() { const string principalIdentityName = "*****@*****.**"; var scenarioOptions = new ScenarioOptions { PrincipalIdentityName = principalIdentityName, }; var model = new UpdateNameForm { DisplayName = "test", IsDisplayNameDerived = false, FirstName = "first", LastName = "last", }; var controller = CreateController(scenarioOptions); Expression <Func <UpdateMyNameCommand, bool> > commandDerivedFromModel = command => command.DisplayName == model.DisplayName && command.IsDisplayNameDerived == model.IsDisplayNameDerived && command.Salutation == model.Salutation && command.FirstName == model.FirstName && command.MiddleName == model.MiddleName && command.LastName == model.LastName && command.Suffix == model.Suffix ; UpdateMyNameCommand executedCommand = null; scenarioOptions.MockCommandHandler.Setup(m => m.Handle(It.Is(commandDerivedFromModel))) .Callback((UpdateMyNameCommand command) => executedCommand = command); controller.Put(model); executedCommand.Principal.Identity.Name.ShouldEqual(scenarioOptions.PrincipalIdentityName); executedCommand.Principal.Identity.Name.ShouldEqual(principalIdentityName); }
public void UpdatesPersonName_WhenFieldsHaveChanged() { const string principalIdentityName = "*****@*****.**"; var principal = principalIdentityName.AsPrincipal(); var user = new User { Name = principalIdentityName, Person = new Person(), }; var command = new UpdateMyNameCommand { Principal = principal, DisplayName = "Display Name", FirstName = "Display", LastName = "Name", }; var entities = new Mock<ICommandEntities>(MockBehavior.Strict).Initialize(); entities.Setup(m => m.Get<User>()).Returns(new[] { user }.AsQueryable); entities.Setup(m => m.Update(It.Is(PersonBasedOn(command)))); var handler = new UpdateMyNameHandler(entities.Object); handler.Handle(command); entities.Verify(m => m.Update(It.Is(PersonBasedOn(command))), Times.Once()); }
public void IsFalse_WhenChangeCount_IsZero() { var command = new UpdateMyNameCommand { ChangeCount = 0, }; command.ChangedState.ShouldBeFalse(); }
public void IsTrue_WhenChangeCount_IsGreaterThanZero() { var command = new UpdateMyNameCommand { ChangeCount = 1, }; command.ChangedState.ShouldBeTrue(); }
public void IsValidWhen_DisplayName_IsNotEmpty() { var command = new UpdateMyNameCommand { DisplayName = "Adam West" }; var validator = new UpdateMyNameValidator(null); var results = validator.Validate(command); var error = results.Errors.SingleOrDefault(e => e.PropertyName == "DisplayName"); error.ShouldBeNull(); }
public void IsInvalidWhen_DisplayName_IsWhiteSpace() { var command = new UpdateMyNameCommand { DisplayName = "\t" }; var validator = new UpdateMyNameValidator(null); var results = validator.Validate(command); results.IsValid.ShouldBeFalse(); results.Errors.Count.ShouldBeInRange(1, int.MaxValue); var error = results.Errors.SingleOrDefault(e => e.PropertyName == "DisplayName"); error.ShouldNotBeNull(); // ReSharper disable PossibleNullReferenceException error.ErrorMessage.ShouldEqual(ValidatePerson.FailedBecauseDisplayNameWasEmpty); // ReSharper restore PossibleNullReferenceException }
public void ExecutesQuery_ToGetUserPerson_FromPrincipalIdentityName() { const string principalIdentityName = "*****@*****.**"; var principal = principalIdentityName.AsPrincipal(); var command = new UpdateMyNameCommand { Principal = principal, }; var user = new User { Name = principalIdentityName, Person = new Person(), }; var entities = new Mock<ICommandEntities>(MockBehavior.Strict).Initialize(); entities.Setup(m => m.Get<User>()).Returns(new[] { user }.AsQueryable); var handler = new UpdateMyNameHandler(entities.Object); handler.Handle(command); entities.Verify(m => m.Get<User>(), Times.Once()); }
public void IsInvalidWhen_Principal_IsNull() { var command = new UpdateMyNameCommand { Principal = null, }; var validator = new UpdateMyNameValidator(null); var results = validator.Validate(command); results.IsValid.ShouldBeFalse(); results.Errors.Count.ShouldBeInRange(1, int.MaxValue); var error = results.Errors.SingleOrDefault(e => e.PropertyName == "Principal"); error.ShouldNotBeNull(); // ReSharper disable PossibleNullReferenceException error.ErrorMessage.ShouldEqual(ValidatePrincipal.FailedBecausePrincipalWasNull); // ReSharper restore PossibleNullReferenceException }
public void IsValidWhen_PrincipalIdentityName_MatchesUser() { const string principalIdentityName = "*****@*****.**"; var principal = principalIdentityName.AsPrincipal(); var command = new UpdateMyNameCommand { Principal = principal, }; var user = new User { Name = principal.Identity.Name, }; var entities = new Mock<ICommandEntities>(MockBehavior.Strict).Initialize(); entities.Setup(m => m.Query<User>()).Returns(new[] { user }.AsQueryable); var validator = new UpdateMyNameValidator(entities.Object); var results = validator.Validate(command); var error = results.Errors.SingleOrDefault(e => e.PropertyName == "Principal"); error.ShouldBeNull(); }
public void IsInvalidWhen_PrincipalIdentityName_DoesNotMatchUser() { const string principalIdentityName = "*****@*****.**"; var principal = principalIdentityName.AsPrincipal(); var command = new UpdateMyNameCommand { Principal = principal, }; var entities = new Mock<ICommandEntities>(MockBehavior.Strict).Initialize(); entities.Setup(m => m.Query<User>()).Returns(new User[] { }.AsQueryable); var validator = new UpdateMyNameValidator(entities.Object); var results = validator.Validate(command); results.IsValid.ShouldBeFalse(); results.Errors.Count.ShouldBeInRange(1, int.MaxValue); var error = results.Errors.SingleOrDefault(e => e.PropertyName == "Principal"); error.ShouldNotBeNull(); // ReSharper disable PossibleNullReferenceException error.ErrorMessage.ShouldEqual(string.Format( ValidatePrincipal.FailedBecauseIdentityNameMatchedNoUser, command.Principal.Identity.Name)); // ReSharper restore PossibleNullReferenceException }
private static Expression<Func<Person, bool>> PersonBasedOn(UpdateMyNameCommand command) { Expression<Func<Person, bool>> personBasedOnCommand = p => p.IsDisplayNameDerived == command.IsDisplayNameDerived && p.DisplayName == command.DisplayName && p.Salutation == command.Salutation && p.FirstName == command.FirstName && p.MiddleName == command.MiddleName && p.LastName == command.LastName && p.Suffix == command.Suffix; return personBasedOnCommand; }
public void IncrementsChangeCount_WhenSuffix_IsDifferent() { Person outPerson = null; const string principalIdentityName = "*****@*****.**"; var principal = principalIdentityName.AsPrincipal(); var user = new User { Name = principalIdentityName, Person = new Person { Suffix = "Jr", }, }; var command = new UpdateMyNameCommand { Principal = principal, Suffix = "Jr.", }; var entities = new Mock<ICommandEntities>(MockBehavior.Strict).Initialize(); entities.Setup(m => m.Get<User>()).Returns(new[] { user }.AsQueryable); entities.Setup(m => m.Update(It.Is(PersonBasedOn(command)))) .Callback((Entity entity) => outPerson = (Person)entity); var handler = new UpdateMyNameHandler(entities.Object); handler.Handle(command); command.ChangeCount.ShouldEqual(1); outPerson.Suffix.ShouldEqual(command.Suffix); }