Beispiel #1
0
        public void IsValid_WhenUserByName_AlreadyExists_AndHasUserId_DifferentFromProvided()
        {
            const int userId   = 797;
            var       userName = Guid.NewGuid().ToString();
            var       queries  = new Mock <IProcessQueries>(MockBehavior.Strict);
            var       command  = new FakeMustNotFindUserByNameCommand
            {
                UserName = userName,
                UserId   = userId + 1,
            };
            var entity = new ProxiedUser(userId)
            {
                Name = command.UserName,
            };
            Expression <Func <UserBy, bool> > expectedQuery = x => x.Name == command.UserName;

            queries.Setup(x => x.Execute(It.Is(expectedQuery))).Returns(Task.FromResult(entity as User));
            var validator = new FakeMustNotFindUserByNameValidator(queries.Object);

            var result = validator.Validate(command);

            result.IsValid.ShouldBeFalse();
            Func <ValidationFailure, bool> nameError = x => x.PropertyName == command.PropertyName(y => y.UserName);

            result.Errors.Count(nameError).ShouldEqual(1);
            result.Errors.Single(nameError).ErrorMessage.ShouldEqual(Resources.Validation_AlreadyExists
                                                                     .Replace("{PropertyName}", User.Constraints.NameLabel)
                                                                     .Replace("{PropertyValue}", command.UserName)
                                                                     );
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Once);
            validator.ShouldHaveValidationErrorFor(x => x.UserName, command);
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Exactly(2));
        }
Beispiel #2
0
        public void IsValid_WhenUserByName_AlreadyExists_ButHasUserId_SameAsProvided()
        {
            const int userId   = 797;
            var       userName = Guid.NewGuid().ToString();
            var       queries  = new Mock <IProcessQueries>(MockBehavior.Strict);
            var       command  = new FakeMustNotFindUserByNameCommand
            {
                UserName = userName,
                UserId   = userId,
            };
            var entity = new ProxiedUser(userId)
            {
                Name = command.UserName,
            };
            Expression <Func <UserBy, bool> > expectedQuery = x => x.Name == command.UserName;

            queries.Setup(x => x.Execute(It.Is(expectedQuery))).Returns(Task.FromResult(entity as User));
            var validator = new FakeMustNotFindUserByNameValidator(queries.Object);

            var result = validator.Validate(command);

            result.IsValid.ShouldBeTrue();
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Once);
            validator.ShouldNotHaveValidationErrorFor(x => x.UserName, command);
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Exactly(2));
        }
Beispiel #3
0
        public void IsValid_WhenUserByName_DoesNotExist()
        {
            var userName = Guid.NewGuid().ToString();
            var queries  = new Mock <IProcessQueries>(MockBehavior.Strict);
            var command  = new FakeMustNotFindUserByNameCommand {
                UserName = userName
            };
            Expression <Func <UserBy, bool> > expectedQuery = x => x.Name == command.UserName;

            queries.Setup(x => x.Execute(It.Is(expectedQuery))).Returns(Task.FromResult(null as User));
            var validator = new FakeMustNotFindUserByNameValidator(queries.Object);

            var result = validator.Validate(command);

            result.IsValid.ShouldBeTrue();
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Once);
            validator.ShouldNotHaveValidationErrorFor(x => x.UserName, command.UserName);
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Exactly(2));
        }