Exemplo n.º 1
0
        public static async Task Assert(this IExceptionCentricTestSpecificationBuilder builder,
                                        IExceptionComparer comparer, object handler, IFactRecorder factRecorder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }
            if (comparer == null)
            {
                throw new ArgumentNullException(nameof(comparer));
            }
            if (handler == null)
            {
                throw new ArgumentNullException(nameof(handler));
            }
            if (factRecorder == null)
            {
                throw new ArgumentNullException(nameof(factRecorder));
            }
            var specification = builder.Build();
            var runner        = new ExceptionCentricTestSpecificationRunner(comparer, handler, factRecorder);
            var result        = await runner.Run(specification);

            if (!result.Failed)
            {
                return;
            }
            if (result.ButException.HasValue)
            {
                await using var writer = new StringWriter();
                writer.WriteLine("  Expected: {0},", result.Specification.Throws);
                writer.WriteLine("  But was:  {0}", result.ButException.Value);

                throw new Xunit.Sdk.XunitException(writer.ToString());
            }

            if (!result.ButEvents.HasValue)
            {
                return;
            }

            await using (var writer = new StringWriter()) {
                writer.WriteLine("  Expected: {0},", result.Specification.Throws);
                writer.WriteLine("  But was:  {0} event(s) ({1})",
                                 result.ButEvents.Value.Length,
                                 string.Join(",", result.ButEvents.Value.Select(_ => _.GetType().Name).ToArray()));

                throw new Xunit.Sdk.XunitException(writer.ToString());
            }
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Asserts that the specification is met.
        /// </summary>
        /// <param name="builder">The specification builder.</param>
        /// <param name="runner"></param>
        /// <param name="comparer">The exception comparer.</param>
        /// <param name="logger">A logger.</param>
        public static void Assert(
            this IExceptionCentricTestSpecificationBuilder builder,
            IExceptionCentricTestSpecificationRunner runner,
            IExceptionComparer comparer,
            ILogger logger)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (runner == null)
            {
                throw new ArgumentNullException(nameof(runner));
            }

            if (comparer == null)
            {
                throw new ArgumentNullException(nameof(comparer));
            }

            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            var specification = builder.Build();

            logger.LogTrace($"Given: \r\n{specification.Givens.ToLogStringVerbose()}");
            logger.LogTrace($"When: \r\n{specification.When.ToLogString()}");
            logger.LogTrace($"Throws: \r\n{specification.Throws.ToLogString(includeStackTrace: false)}");

            var result = runner.Run(specification);

            if (result.Failed)
            {
                if (result.ButException.HasValue)
                {
                    logger.LogTrace($"Actual exception: \r\n{result.ButException.Value.ToLogString(includeStackTrace: true)}");

                    if (result.ButException.Value.GetType() != result.Specification.Throws.GetType())
                    {
                        using (var writer = new StringWriter())
                        {
                            writer.WriteLine("  Expected: {0},", result.Specification.Throws);
                            writer.WriteLine("  But was:  {0}", result.ButException.Value);

#if NUNIT
                            throw new NUnit.Framework.AssertionException(writer.ToString());
#elif XUNIT
                            throw new Xunit.Sdk.XunitException(writer.ToString());
#endif
                        }
                    }

                    using (var writer = new StringWriter())
                    {
                        writer.WriteLine("  Expected: {0},", result.Specification.Throws);
                        writer.WriteLine("  But found the following differences:");

                        foreach (var difference in comparer.Compare(result.Specification.Throws, result.ButException.Value))
                        {
                            writer.WriteLine("    {0}", difference.Message);
                        }

#if NUNIT
                        throw new NUnit.Framework.AssertionException(writer.ToString());
#elif XUNIT
                        throw new Xunit.Sdk.XunitException(writer.ToString());
#endif
                    }
                }
                logger.LogTrace("Actual exception: None");

                if (result.ButEvents.HasValue)
                {
                    logger.LogTrace($"Actual events: \r\n{result.ButEvents.Value.ToLogStringVerbose()}");

                    using (var writer = new StringWriter())
                    {
                        writer.WriteLine("  Expected: {0},", result.Specification.Throws);
                        writer.WriteLine("  But was:  {0} event(s) ({1})",
                                         result.ButEvents.Value.Length,
                                         result.ButEvents.Value.ToLogStringShort());

#if NUNIT
                        throw new NUnit.Framework.AssertionException(writer.ToString());
#elif XUNIT
                        throw new Xunit.Sdk.XunitException(writer.ToString());
#endif
                    }
                }

                using (var writer = new StringWriter())
                {
                    writer.WriteLine("  Expected: {0},", result.Specification.Throws);
                    writer.WriteLine("  But no exception occurred");

#if NUNIT
                    throw new NUnit.Framework.AssertionException(writer.ToString());
#elif XUNIT
                    throw new Xunit.Sdk.XunitException(writer.ToString());
#endif
                }
            }

            if (result.ButException.HasValue)
            {
                logger.LogTrace($"Actual exception: \r\n{result.ButException.Value.ToLogString(includeStackTrace: true)}");
            }
        }