Skip to content

Commit 24ae51b

Browse files
committed
Added testing for total number of inner exceptions in AggregateException (#73)
1 parent 50d0ac6 commit 24ae51b

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

src/MyWebApi.Tests/BuildersTests/ActionsTests/ShouldHaveModelStateTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public void ShouldHaveInvalidModelStateShouldBeValidWithInvalidRequestModelAndCo
9595
[Test]
9696
[ExpectedException(
9797
typeof(ModelErrorAssertionException),
98-
ExpectedMessage = "When calling ModelStateCheck action in WebApiController expected to have invalid model state with 5 errors, but contained 2.")]
98+
ExpectedMessage = "When calling ModelStateCheck action in WebApiController expected to have invalid model state with 5 errors, but in fact contained 2.")]
9999
public void ShouldHaveInvalidModelStateShouldBeInvalidWithInvalidRequestModelAndIncorrectNumberOfErrors()
100100
{
101101
var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();
@@ -125,7 +125,7 @@ public void ShouldHaveInvalidModelStateShouldThrowExceptionWithValidRequestModel
125125
[Test]
126126
[ExpectedException(
127127
typeof(ModelErrorAssertionException),
128-
ExpectedMessage = "When calling ModelStateCheck action in WebApiController expected to have invalid model state with 5 errors, but contained 0.")]
128+
ExpectedMessage = "When calling ModelStateCheck action in WebApiController expected to have invalid model state with 5 errors, but in fact contained 0.")]
129129
public void ShouldHaveInvalidModelStateShouldThrowExceptionWithValidRequestModelAndProvidedNumberOfErrors()
130130
{
131131
var requestModel = TestObjectFactory.GetValidRequestModel();

src/MyWebApi/Builders/Actions/ShouldHave/ShouldHaveModelState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public IAndTestBuilder<TActionResult> InvalidModelState(int? withNumberOfErrors
6464
this.ActionName,
6565
this.Controller.GetName(),
6666
withNumberOfErrors == null ? string.Empty : string.Format(" with {0} errors", withNumberOfErrors),
67-
withNumberOfErrors == null ? "but was in fact valid" : string.Format("but contained {0}", actualModelStateErrors)));
67+
withNumberOfErrors == null ? "but was in fact valid" : string.Format("but in fact contained {0}", actualModelStateErrors)));
6868
}
6969

7070
return this.NewAndTestBuilder();

src/MyWebApi/Builders/Actions/ShouldThrowTestBuilder.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ namespace MyWebApi.Builders.Actions
1919
using System;
2020
using System.Web.Http;
2121
using Base;
22+
using Common.Extensions;
2223
using Contracts.Actions;
2324
using Contracts.ExceptionErrors;
2425
using ExceptionErrors;
26+
using Exceptions;
2527

2628
/// <summary>
2729
/// Used for testing whether action throws exception.
@@ -57,14 +59,28 @@ public IExceptionTestBuilder Exception()
5759
/// <summary>
5860
/// Tests whether action throws any AggregateException.
5961
/// </summary>
62+
/// <param name="withNumberOfInnerExceptions">Optional expected number of total inner exceptions.</param>
6063
/// <returns>AggregateException test builder.</returns>
61-
public IAggregateExceptionTestBuilder AggregateException()
64+
public IAggregateExceptionTestBuilder AggregateException(int? withNumberOfInnerExceptions = null)
6265
{
6366
this.exceptionTestBuilder.OfType<AggregateException>();
67+
var aggregateException = this.CaughtException as AggregateException;
68+
var innerExceptionsCount = aggregateException.InnerExceptions.Count;
69+
if (withNumberOfInnerExceptions.HasValue &&
70+
withNumberOfInnerExceptions != innerExceptionsCount)
71+
{
72+
throw new InvalidExceptionAssertionException(string.Format(
73+
"When calling {0} action in {1} expected AggregateException to contain {2} inner exceptions, but in fact contained {3}.",
74+
this.ActionName,
75+
this.Controller.GetName(),
76+
withNumberOfInnerExceptions,
77+
innerExceptionsCount));
78+
}
79+
6480
return new AggregateExceptionTestBuilder(
6581
this.Controller,
6682
this.ActionName,
67-
this.CaughtException as AggregateException);
83+
aggregateException);
6884
}
6985

7086
/// <summary>

src/MyWebApi/Builders/Contracts/Actions/IShouldThrowTestBuilder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ public interface IShouldThrowTestBuilder : IBaseTestBuilder
3333
/// <summary>
3434
/// Tests whether action throws any AggregateException.
3535
/// </summary>
36+
/// <param name="withNumberOfInnerExceptions">Optional expected number of total inner exceptions.</param>
3637
/// <returns>AggregateException test builder.</returns>
37-
IAggregateExceptionTestBuilder AggregateException();
38+
IAggregateExceptionTestBuilder AggregateException(int? withNumberOfInnerExceptions = null);
3839

3940
/// <summary>
4041
/// Tests whether action throws any HttpResponseException.

0 commit comments

Comments
 (0)