/// <summary>
 /// Runs the specified test specification.
 /// </summary>
 /// <param name="specification">The test specification to run.</param>
 /// <returns>
 /// The result of running the test specification.
 /// </returns>
 /// <exception cref="ArgumentNullException">Thrown when <paramref name="specification"/> is <c>null</c>.</exception>
 public ExceptionCentricAggregateQueryTestResult Run(ExceptionCentricAggregateQueryTestSpecification specification)
 {
     if (specification == null) throw new ArgumentNullException("specification");
     var sut = specification.SutFactory();
     sut.Initialize(specification.Givens);
     object queryResult = null;
     var result = Catch.Exception(() => queryResult = specification.When(sut));
     if (!result.HasValue)
     {
         if (sut.HasChanges())
         {
             return specification.Fail(sut.GetChanges().ToArray());
         }
         return specification.Fail(queryResult);
     }
     var actualException = result.Value;
     if (_comparer.Compare(actualException, specification.Throws).Any())
     {
         return specification.Fail(actualException);
     }
     if (sut.HasChanges())
     {
         return specification.Fail(sut.GetChanges().ToArray());
     }
     return specification.Pass();
 }
Exemplo n.º 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ExceptionCentricTestResult"/> class.
 /// </summary>
 /// <param name="specification">The specification.</param>
 /// <param name="state">The state.</param>
 /// <param name="actualException">The actual exception.</param>
 /// <param name="actualEvents">The actual events.</param>
 /// <param name="actualResult">The actual result.</param>
 internal ExceptionCentricAggregateQueryTestResult(
     ExceptionCentricAggregateQueryTestSpecification specification, TestResultState state,
     Optional <Exception> actualException,
     Optional <object[]> actualEvents,
     Optional <object> actualResult)
 {
     _specification   = specification;
     _state           = state;
     _actualException = actualException;
     _actualEvents    = actualEvents;
     _actualResult    = actualResult;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ExceptionCentricTestResult"/> class.
 /// </summary>
 /// <param name="specification">The specification.</param>
 /// <param name="state">The state.</param>
 /// <param name="actualException">The actual exception.</param>
 /// <param name="actualEvents">The actual events.</param>
 /// <param name="actualResult">The actual result.</param>
 internal ExceptionCentricAggregateQueryTestResult(
     ExceptionCentricAggregateQueryTestSpecification specification, TestResultState state, 
     Optional<Exception> actualException, 
     Optional<object[]> actualEvents, 
     Optional<object> actualResult)
 {
     _specification = specification;
     _state = state;
     _actualException = actualException;
     _actualEvents = actualEvents;
     _actualResult = actualResult;
 }
        public void SetUp()
        {
            Func <IAggregateRootEntity> sutQuery = () => (IAggregateRootEntity)null;
            var givens = new[] { new object(), new object() };
            Func <IAggregateRootEntity, object> when = _ => null;
            var throws = new Exception();

            _sut = new ExceptionCentricAggregateQueryTestSpecification(
                sutQuery,
                givens,
                when,
                throws);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Runs the specified test specification.
        /// </summary>
        /// <param name="specification">The test specification to run.</param>
        /// <returns>
        /// The result of running the test specification.
        /// </returns>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="specification"/> is <c>null</c>.</exception>
        public ExceptionCentricAggregateQueryTestResult Run(ExceptionCentricAggregateQueryTestSpecification specification)
        {
            if (specification == null)
            {
                throw new ArgumentNullException(nameof(specification));
            }
            var sut = specification.SutFactory();

            sut.Initialize(specification.Givens);
            object queryResult = null;
            var    result      = Catch.Exception(() => queryResult = specification.When(sut));

            if (!result.HasValue)
            {
                if (sut.HasChanges())
                {
#if NET20
                    return(specification.Fail(new List <object>(sut.GetChanges()).ToArray()));
#else
                    return(specification.Fail(sut.GetChanges().ToArray()));
#endif
                }
                return(specification.Fail(queryResult));
            }
            var actualException = result.Value;
#if NET20
            using (var enumerator = _comparer.Compare(actualException, specification.Throws).GetEnumerator())
            {
                if (enumerator.MoveNext())
                {
                    return(specification.Fail(actualException));
                }
            }
#else
            if (_comparer.Compare(actualException, specification.Throws).Any())
            {
                return(specification.Fail(actualException));
            }
#endif
            if (sut.HasChanges())
            {
#if NET20
                return(specification.Fail(new List <object>(sut.GetChanges())));
#else
                return(specification.Fail(sut.GetChanges().ToArray()));
#endif
            }
            return(specification.Pass());
        }
        public void RunReturnsExpectedResultWhenPassed()
        {
            var specification = new ExceptionCentricAggregateQueryTestSpecification(
                () => new PassCase(),
                new object[0],
                _ => ((PassCase)_).Pass(),
                PassCase.TheException);

            var result = _sut.Run(specification);
            Assert.That(result.Passed, Is.True);
            Assert.That(result.Failed, Is.False);
            Assert.That(result.ButEvents, Is.EqualTo(Optional<object[]>.Empty));
            Assert.That(result.ButException, Is.EqualTo(Optional<Exception>.Empty));
            Assert.That(result.ButResult, Is.EqualTo(Optional<object>.Empty));
        }
        public void RunReturnsExpectedResultWhenFailedBecauseOfDifferentException()
        {
            var specification = new ExceptionCentricAggregateQueryTestSpecification(
                () => new FailExceptionCase(),
                new object[0],
                _ => ((FailExceptionCase)_).Fail(),
                FailExceptionCase.TheExpectedException);

            var result = _sut.Run(specification);
            Assert.That(result.Passed, Is.False);
            Assert.That(result.Failed, Is.True);
            Assert.That(result.ButEvents, Is.EqualTo(Optional<object[]>.Empty));
            Assert.That(result.ButException, Is.EqualTo(new Optional<Exception>(FailExceptionCase.TheActualException)));
            Assert.That(result.ButResult, Is.EqualTo(Optional<object>.Empty));
        }
Exemplo n.º 8
0
        public void RunReturnsExpectedResultWhenFailedBecauseNoExceptionOccurred()
        {
            var specification = new ExceptionCentricAggregateQueryTestSpecification(
                () => new FailNoExceptionCase(),
                new object[0],
                _ => ((FailNoExceptionCase)_).Fail(),
                FailNoExceptionCase.TheExpectedException);

            var result = _sut.Run(specification);

            Assert.That(result.Passed, Is.False);
            Assert.That(result.Failed, Is.True);
            Assert.That(result.ButEvents, Is.EqualTo(Optional <object[]> .Empty));
            Assert.That(result.ButException, Is.EqualTo(Optional <Exception> .Empty));
            Assert.That(result.ButResult, Is.EqualTo(new Optional <object>(FailNoExceptionCase.TheResult)));
        }
        public void UsingDefaultCtorReturnsInstanceWithExpectedProperties()
        {
            Func <IAggregateRootEntity> sutQuery = () => (IAggregateRootEntity)null;
            var givens = new[] { new object(), new object() };
            Func <IAggregateRootEntity, object> when = _ => null;
            var throws = new Exception();

            var sut = new ExceptionCentricAggregateQueryTestSpecification(
                sutQuery,
                givens,
                when,
                throws);

            Assert.That(sut.SutFactory, Is.SameAs(sutQuery));
            Assert.That(sut.Givens, Is.EquivalentTo(givens));
            Assert.That(sut.When, Is.SameAs(when));
            Assert.That(sut.Throws, Is.SameAs(throws));
        }
        /// <summary>
        /// Runs the specified test specification.
        /// </summary>
        /// <param name="specification">The test specification to run.</param>
        /// <returns>
        /// The result of running the test specification.
        /// </returns>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="specification"/> is <c>null</c>.</exception>
        public ExceptionCentricAggregateQueryTestResult Run(ExceptionCentricAggregateQueryTestSpecification specification)
        {
            if (specification == null) throw new ArgumentNullException("specification");
            var sut = specification.SutFactory();
            sut.Initialize(specification.Givens);
            object queryResult = null;
            var result = Catch.Exception(() => queryResult = specification.When(sut));
            if (!result.HasValue)
            {
                if (sut.HasChanges())
                {
#if NET20
                    return specification.Fail(new List<object>(sut.GetChanges()).ToArray());
#else
                    return specification.Fail(sut.GetChanges().ToArray());
#endif
                }
                return specification.Fail(queryResult);
            }
            var actualException = result.Value;
#if NET20
            using (var enumerator = _comparer.Compare(actualException, specification.Throws).GetEnumerator())
            {
                if (enumerator.MoveNext())
                {
                    return specification.Fail(actualException);
                }
            }
#else
            if (_comparer.Compare(actualException, specification.Throws).Any())
            {
                return specification.Fail(actualException);
            }
#endif
            if (sut.HasChanges())
            {
#if NET20
                return specification.Fail(new List<object>(sut.GetChanges()));
#else
                return specification.Fail(sut.GetChanges().ToArray());
#endif
            }return specification.Pass();
        }
        public void UsingDefaultCtorReturnsInstanceWithExpectedProperties()
        {
            Func<IAggregateRootEntity> sutQuery = () => (IAggregateRootEntity)null;
            var givens = new[] { new object(), new object() };
            Func<IAggregateRootEntity, object> when = _ => null;
            var throws = new Exception();

            var sut = new ExceptionCentricAggregateQueryTestSpecification(
                sutQuery,
                givens,
                when,
                throws);

            Assert.That(sut.SutFactory, Is.SameAs(sutQuery));
            Assert.That(sut.Givens, Is.EquivalentTo(givens));
            Assert.That(sut.When, Is.SameAs(when));
            Assert.That(sut.Throws, Is.SameAs(throws));
        }
        public void SetUp()
        {
            Func<IAggregateRootEntity> sutQuery = () => (IAggregateRootEntity)null;
            var givens = new[] { new object(), new object() };
            Func<IAggregateRootEntity, object> when = _ => null;
            var throws = new Exception();

            _sut = new ExceptionCentricAggregateQueryTestSpecification(
                sutQuery,
                givens,
                when,
                throws);
        }