public ExceptionCentricTestSpecificationRunner(IExceptionComparer comparer, IFactWriter factWriter, IFactReader factReader, IHandlerResolver handlerResolver) { _comparer = comparer ?? throw new ArgumentNullException(nameof(comparer)); _factWriter = factWriter ?? throw new ArgumentNullException(nameof(factWriter)); _factReader = factReader ?? throw new ArgumentNullException(nameof(factReader)); _handlerResolver = handlerResolver ?? throw new ArgumentNullException(nameof(handlerResolver)); }
public static void UseAggregateSourceTesting( this ContainerBuilder builder, IFactComparer factComparer, IExceptionComparer exceptionComparer) { builder .RegisterType <StreamStoreFactRepository>() .AsImplementedInterfaces(); builder .RegisterType <ReflectionBasedHandlerResolver>() .AsImplementedInterfaces(); builder .RegisterInstance(factComparer) .AsImplementedInterfaces(); builder .RegisterInstance(exceptionComparer) .AsImplementedInterfaces(); builder .RegisterType <ExceptionCentricTestSpecificationRunner>() .AsImplementedInterfaces(); builder. RegisterType <EventCentricTestSpecificationRunner>() .AsImplementedInterfaces(); }
/// <summary> /// Initializes a new instance of the <see cref="ExceptionCentricAggregateQueryTestRunner"/> class. /// </summary> /// <param name="comparer">The comparer to use when comparing exceptions.</param> /// <exception cref="System.ArgumentNullException">Thrown when <paramref name="comparer"/> is <c>null</c>.</exception> public ExceptionCentricAggregateQueryTestRunner(IExceptionComparer comparer) { if (comparer == null) { throw new ArgumentNullException(nameof(comparer)); } _comparer = comparer; }
/// <summary> /// Initializes a new instance of the <see cref="ExceptionCentricAggregateConstructorTestRunner"/> class. /// </summary> /// <param name="comparer">The comparer to use when comparing exceptions.</param> /// <exception cref="System.ArgumentNullException">Thrown when <paramref name="comparer"/> is <c>null</c>.</exception> public ExceptionCentricAggregateConstructorTestRunner(IExceptionComparer comparer) { if (comparer == null) { throw new ArgumentNullException("comparer"); } _comparer = comparer; }
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()); } }
public ExceptionCentricTestSpecificationRunner(IExceptionComparer comparer, object handler, IFactRecorder factRecorder) { if (comparer == null) { throw new ArgumentNullException(nameof(comparer)); } if (handler == null) { throw new ArgumentNullException(nameof(handler)); } if (factRecorder == null) { throw new ArgumentNullException(nameof(factRecorder)); } _comparer = comparer; _factRecorder = factRecorder; _handler = handler; }
/// <summary> /// Asserts that the specification is met. /// </summary> /// <param name="builder">The specification builder.</param> /// <param name="comparer">The exception comparer.</param> public static void Assert( this IExceptionCentricAggregateConstructorTestSpecificationBuilder builder, IExceptionComparer comparer) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } if (comparer == null) { throw new ArgumentNullException(nameof(comparer)); } var specification = builder.Build(); var runner = new ExceptionCentricAggregateConstructorTestRunner(comparer); var result = runner.Run(specification); if (result.Failed) { if (result.ButException.HasValue) { 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 } } if (result.ButEvents.HasValue) { 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())); #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 } } }
/// <summary> /// Initializes a new instance of the <see cref="ExceptionCentricAggregateCommandTestRunner"/> class. /// </summary> /// <param name="comparer">The comparer to use when comparing exceptions.</param> /// <exception cref="System.ArgumentNullException">Thrown when <paramref name="comparer"/> is <c>null</c>.</exception> public ExceptionCentricAggregateCommandTestRunner(IExceptionComparer comparer) { if (comparer == null) throw new ArgumentNullException("comparer"); _comparer = comparer; }
public void SetUp() { _comparer = new EqualsExceptionComparer(); _sut = new ExceptionCentricAggregateCommandTestRunner(_comparer); }
/// <summary> /// Initializes a new instance of the <see cref="ExceptionCentricAggregateFactoryTestRunner"/> class. /// </summary> /// <param name="comparer">The comparer to use when comparing exceptions.</param> /// <exception cref="System.ArgumentNullException">Thrown when <paramref name="comparer"/> is <c>null</c>.</exception> public ExceptionCentricAggregateFactoryTestRunner(IExceptionComparer comparer) => _comparer = comparer ?? throw new ArgumentNullException(nameof(comparer));
/// <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)}"); } }
/// <summary> /// Asserts that the specification is met. /// </summary> /// <param name="builder">The specification builder.</param> /// <param name="comparer">The exception comparer.</param> public static void Assert(this IExceptionCentricAggregateCommandTestSpecificationBuilder builder, IExceptionComparer comparer) { if (builder == null) throw new ArgumentNullException("builder"); if (comparer == null) throw new ArgumentNullException("comparer"); var specification = builder.Build(); var runner = new ExceptionCentricAggregateCommandTestRunner(comparer); var result = runner.Run(specification); if (result.Failed) { if (result.ButException.HasValue) { 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.AssertException(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.AssertException(writer.ToString()); #endif } } if (result.ButEvents.HasValue) { 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())); #if NUNIT throw new NUnit.Framework.AssertionException(writer.ToString()); #elif XUNIT throw new Xunit.Sdk.AssertException(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.AssertException(writer.ToString()); #endif } } }