public void Allows_Retrieval_Of_Configured_Rules()
        {
            var doerOfThings = new Mock <IDoThings>();
            var rules        = new ISynchronousRule[] { new SynchronousTrueRule(), new SynchronousFalseRule1() };
            var command      = new SynchronousCommandStub(doerOfThings.Object, rules);

            command.GetRules().ShouldBe(rules);
        }
        public void Allows_Execution_Of_Configured_Rules()
        {
            var doerOfThings = new Mock <IDoThings>();
            var rules        = new ISynchronousRule[] { new SynchronousTrueRule(), new SynchronousFalseRule1() };
            var command      = new SynchronousCommandStub(doerOfThings.Object, rules);

            var errors = command.Validate().Errors.ToArray();

            errors.Count().ShouldBe(1);
            errors.First().ErrorMessage.ShouldBe("FalseRule1 failed validation");
        }
        public void Operation_Cannot_Complete_If_Any_Rules_Fail_Validation()
        {
            var doerOfThings = new Mock <IDoThings>();
            var rules        = new ISynchronousRule[] { new SynchronousTrueRule(), new SynchronousFalseRule1() };
            var command      = new SynchronousCommandStub(doerOfThings.Object, rules);

            var result = command.Validate();

            result.CanContinue.ShouldBe(false);
            result.Errors.Count().ShouldBe(1);
            result.Errors.First().ErrorMessage.ShouldBe("FalseRule1 failed validation");
        }
        public void Operation_Can_Complete_If_Rules_Pass_Validation_And_Complete_Validation_With_Successful_Validation_Results()
        {
            var doerOfThings = new Mock <IDoThings>();

            doerOfThings.Setup(d => d.GetValue()).Returns("You shall pass");
            var rules   = new ISynchronousRule[] { new SynchronousTrueRule(), new SynchronousTrueRule() };
            var command = new SynchronousCommandStub(doerOfThings.Object, rules);

            var validationResult = command.Validate();

            validationResult.CanContinue.ShouldBeTrue();
            validationResult.Errors.Count().ShouldBe(0);

            var executionResult = validationResult.CompleteCommandExecution();

            executionResult.Success.ShouldBeTrue();
            executionResult.Value.ShouldBe("You shall pass");
        }
        public void Completion_Properly_Handles_Caught_Peasy_Exception()
        {
            var doerOfThings = new Mock <IDoThings>();

            doerOfThings.Setup(d => d.GetValue()).Throws(new PeasyException("You shall not pass"));
            var rules   = new ISynchronousRule[] { new SynchronousTrueRule(), new SynchronousTrueRule() };
            var command = new SynchronousCommandStub(doerOfThings.Object, rules);

            var validationResult = command.Validate();

            validationResult.CanContinue.ShouldBeTrue();
            validationResult.Errors.Count().ShouldBe(0);

            var executionResult = validationResult.CompleteCommandExecution();

            executionResult.Success.ShouldBeFalse();
            executionResult.Errors.Count().ShouldBe(1);
            executionResult.Errors.First().ErrorMessage.ShouldBe("You shall not pass");
        }
        public void Fails_Execution_With_Expected_ExecutionResult_And_Method_Invocations_When_Any_Validation_Results_Exist()
        {
            var doerOfThings     = new Mock <IDoThings>();
            var validationResult = new ValidationResult("You shall not pass");
            var command          = new SynchronousCommandStub(doerOfThings.Object, new [] { validationResult });

            var result = command.Execute();

            result.Success.ShouldBeFalse();
            result.Errors.Count().ShouldBe(1);
            result.Errors.First().ErrorMessage.ShouldBe("You shall not pass");
            result.Value.ShouldBeNull();

            doerOfThings.Verify(d => d.Log("OnInitialization"), Times.Once);
            doerOfThings.Verify(d => d.Log("OnValidate"), Times.Once);
            doerOfThings.Verify(d => d.Log("OnGetRules"), Times.Never);
            doerOfThings.Verify(d => d.Log("OnComplete"), Times.Never);
            doerOfThings.Verify(d => d.Log("OnExecute"), Times.Never);
            doerOfThings.Verify(d => d.Log("OnFailedExecution"), Times.Once);
            doerOfThings.Verify(d => d.Log("OnPeasyExceptionHandled"), Times.Never);
            doerOfThings.Verify(d => d.Log("OnSuccessfulExecution"), Times.Never);
        }
        public void Successful_Execution_With_Expected_ExecutionResult_And_Method_Invocations_When_All_Rules_Pass()
        {
            var doerOfThings = new Mock <IDoThings>();

            doerOfThings.Setup(d => d.GetValue()).Returns("You shall pass");
            var command = new SynchronousCommandStub(doerOfThings.Object, new ISynchronousRule[] { new SynchronousTrueRule(), new SynchronousTrueRule() });

            var result = command.Execute();

            result.Success.ShouldBeTrue();
            result.Errors.ShouldBeNull();
            result.Value.ShouldBe("You shall pass");

            doerOfThings.Verify(d => d.Log("OnInitialization"), Times.Once);
            doerOfThings.Verify(d => d.Log("OnValidate"), Times.Once);
            doerOfThings.Verify(d => d.Log("OnGetRules"), Times.Once);
            doerOfThings.Verify(d => d.Log("OnComplete"), Times.Once);
            doerOfThings.Verify(d => d.Log("OnExecute"), Times.Once);
            doerOfThings.Verify(d => d.Log("OnFailedExecution"), Times.Never);
            doerOfThings.Verify(d => d.Log("OnPeasyExceptionHandled"), Times.Never);
            doerOfThings.Verify(d => d.Log("OnSuccessfulExecution"), Times.Once);
        }