Esempio n. 1
0
        public async Task <ExceptionCentricTestResult> Run(ExceptionCentricTestSpecification specification)
        {
            var methodInfo = _handler.GetType().GetMethods().Single(
                mi => {
                var parameters = mi.GetParameters();
                return(mi.IsPublic &&
                       mi.ReturnType == typeof(ValueTask) &&
                       parameters.Length == 2 &&
                       parameters[0].ParameterType == specification.When.GetType() &&
                       parameters[1].ParameterType == typeof(CancellationToken));
            });

            ValueTask Handler(object o, CancellationToken ct) => (ValueTask)methodInfo.Invoke(_handler, new[] { o, ct }) !;

            _factRecorder.Record(specification.Givens);

            Optional <Exception> optionalException = Optional <Exception> .Empty;

            try {
                await Handler(specification.When, CancellationToken.None);
            } catch (Exception ex) {
                optionalException = ex;
            }

            var then = _factRecorder.GetFacts().Skip(specification.Givens.Length).ToArray();

            return(new ExceptionCentricTestResult(specification,
                                                  optionalException.HasValue &&
                                                  !_comparer.Compare(specification.Throws, optionalException.Value).Any()
                    ? TestResultState.Passed
                    : TestResultState.Failed,
                                                  optionalException, then));
        }
Esempio n. 2
0
        public async ValueTask <Optional <T> > GetOptional(string identifier,
                                                           CancellationToken cancellationToken = default)
        {
            var facts = await _facts.GetFacts().Where(x => x.Identifier == identifier)
                        .ToArrayAsync(cancellationToken);

            if (facts.Length == 0)
            {
                return(Optional <T> .Empty);
            }

            var aggregateRoot = _factory();

            aggregateRoot.LoadFromHistory(facts.Select(x => x.Event));
            _facts.Attach(aggregateRoot.Id, aggregateRoot);
            return(aggregateRoot);
        }