Пример #1
0
        public void IsInvalid_WhenUserNotFound_ByPrincipal()
        {
            string userName  = FakeData.String();
            var    principal = new Mock <IPrincipal>(MockBehavior.Strict);
            var    identity  = new Mock <IIdentity>(MockBehavior.Strict);

            identity.SetupGet(x => x.IsAuthenticated).Returns(true);
            identity.SetupGet(x => x.Name).Returns(userName);
            principal.SetupGet(x => x.Identity).Returns(identity.Object);
            var queries = new Mock <IProcessQueries>(MockBehavior.Strict);
            var command = new FakeMustFindUserByPrincipalCommand {
                Principal = principal.Object,
            };
            Expression <Func <UserBy, bool> > expectedQuery = x => x.Principal == principal.Object;

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

            var result = validator.Validate(command);

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

            result.Errors.Count(nameError).ShouldEqual(1);
            result.Errors.Single(nameError).ErrorMessage.ShouldEqual(Resources.Validation_DoesNotExist
                                                                     .Replace("{PropertyName}", User.Constraints.Label)
                                                                     .Replace("{PropertyValue}", userName)
                                                                     );
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Once);
            validator.ShouldHaveValidationErrorFor(x => x.Principal, command.Principal);
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Exactly(2));
        }
Пример #2
0
        public void IsInvalid_WhenPrincipal_IsNull()
        {
            var queries = new Mock <IProcessQueries>(MockBehavior.Strict);
            var command = new FakeMustFindUserByPrincipalCommand();

            queries.Setup(x => x.Execute(It.IsAny <UserBy>())).Returns(Task.FromResult(new User()));
            var validator = new FakeMustFindUserByPrincipalValidator(queries.Object);

            var result = validator.Validate(command);

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

            result.Errors.Count(nameError).ShouldEqual(1);
            result.Errors.Single(nameError).ErrorMessage.ShouldEqual(Resources.Validation_DoesNotExist
                                                                     .Replace("{PropertyName}", User.Constraints.Label)
                                                                     .Replace("{PropertyValue}", "")
                                                                     );
            queries.Verify(x => x.Execute(It.IsAny <UserBy>()), Times.Never);
            validator.ShouldHaveValidationErrorFor(x => x.Principal, command.Principal);
            queries.Verify(x => x.Execute(It.IsAny <UserBy>()), Times.Never);
        }