Skip to content

Commit 74db655

Browse files
committed
Added IAggregateExceptionTestBuilder to test for contained exception types (#73)
1 parent 1cc8b5b commit 74db655

File tree

6 files changed

+90
-16
lines changed

6 files changed

+90
-16
lines changed

src/MyWebApi/Builders/Contracts/ExceptionErrors/IAggregateExceptionTestBuilder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
namespace MyWebApi.Builders.Contracts.ExceptionErrors
1818
{
19-
public interface IAggregateExceptionTestBuilder : IExceptionTestBuilder
19+
public interface IAggregateExceptionTestBuilder : IBaseExceptionTestBuilder
2020
{
21+
IAndAggregateExceptionTestBuilder ContainingInnerExceptionOfType<TInnerException>();
2122
}
2223
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// MyWebApi - ASP.NET Web API Fluent Testing Framework
2+
// Copyright (C) 2015 Ivaylo Kenov.
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with this program. If not, see http://www.gnu.org/licenses/.
16+
17+
namespace MyWebApi.Builders.Contracts.ExceptionErrors
18+
{
19+
public interface IAndAggregateExceptionTestBuilder : IAggregateExceptionTestBuilder
20+
{
21+
IAggregateExceptionTestBuilder AndAlso();
22+
}
23+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// MyWebApi - ASP.NET Web API Fluent Testing Framework
2+
// Copyright (C) 2015 Ivaylo Kenov.
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with this program. If not, see http://www.gnu.org/licenses/.
16+
17+
namespace MyWebApi.Builders.Contracts.ExceptionErrors
18+
{
19+
using Base;
20+
21+
public interface IBaseExceptionTestBuilder : IBaseTestBuilder
22+
{
23+
/// <summary>
24+
/// Tests exception message using test builder.
25+
/// </summary>
26+
/// <returns>Exception message test builder.</returns>
27+
IExceptionMessageTestBuilder WithMessage();
28+
29+
/// <summary>
30+
/// Tests exception message whether it is equal to the provided message as string.
31+
/// </summary>
32+
/// <param name="message">Expected exception message as string.</param>
33+
/// <returns>The same exception test builder.</returns>
34+
IAndExceptionTestBuilder WithMessage(string message);
35+
}
36+
}

src/MyWebApi/Builders/Contracts/ExceptionErrors/IExceptionTestBuilder.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,13 @@ namespace MyWebApi.Builders.Contracts.ExceptionErrors
2121
/// <summary>
2222
/// Used for testing expected exceptions.
2323
/// </summary>
24-
public interface IExceptionTestBuilder : IBaseTestBuilder
24+
public interface IExceptionTestBuilder : IBaseExceptionTestBuilder
2525
{
2626
/// <summary>
2727
/// Tests whether certain type of exception is returned from the invoked action.
2828
/// </summary>
2929
/// <typeparam name="TException">Type of the expected exception.</typeparam>
3030
/// <returns>The same exception test builder.</returns>
3131
IAndExceptionTestBuilder OfType<TException>();
32-
33-
/// <summary>
34-
/// Tests exception message using test builder.
35-
/// </summary>
36-
/// <returns>Exception message test builder.</returns>
37-
IExceptionMessageTestBuilder WithMessage();
38-
39-
/// <summary>
40-
/// Tests exception message whether it is equal to the provided message as string.
41-
/// </summary>
42-
/// <param name="message">Expected exception message as string.</param>
43-
/// <returns>The same exception test builder.</returns>
44-
IAndExceptionTestBuilder WithMessage(string message);
4532
}
4633
}

src/MyWebApi/Builders/ExceptionErrors/AggregateExceptionTestBuilder.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@
1717
namespace MyWebApi.Builders.ExceptionErrors
1818
{
1919
using System;
20+
using System.Linq;
2021
using System.Web.Http;
22+
using Common.Extensions;
2123
using Contracts.ExceptionErrors;
24+
using Exceptions;
25+
using Utilities;
2226

23-
public class AggregateExceptionTestBuilder : ExceptionTestBuilder, IAggregateExceptionTestBuilder
27+
public class AggregateExceptionTestBuilder : ExceptionTestBuilder, IAndAggregateExceptionTestBuilder
2428
{
2529
private readonly AggregateException aggregateException;
2630

@@ -32,5 +36,26 @@ public AggregateExceptionTestBuilder(
3236
{
3337
this.aggregateException = caughtException;
3438
}
39+
40+
public IAndAggregateExceptionTestBuilder ContainingInnerExceptionOfType<TInnerException>()
41+
{
42+
var expectedInnerExceptionType = typeof(TInnerException);
43+
var innerExceptionFound = this.aggregateException.InnerExceptions.Any(e => e.GetType() == expectedInnerExceptionType);
44+
if (!innerExceptionFound)
45+
{
46+
throw new InvalidExceptionAssertionException(string.Format(
47+
"When calling {0} action in {1} expected AggregateException to contain {2}, but none was found.",
48+
this.ActionName,
49+
this.Controller.GetName(),
50+
expectedInnerExceptionType.ToFriendlyTypeName()));
51+
}
52+
53+
return this;
54+
}
55+
56+
public new IAggregateExceptionTestBuilder AndAlso()
57+
{
58+
return this;
59+
}
3560
}
3661
}

src/MyWebApi/MyWebApi.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@
7373
<Compile Include="Builders\And\AndProvideTestBuilder{TActionResult}.cs" />
7474
<Compile Include="Builders\And\AndTestBuilder.cs" />
7575
<Compile Include="Builders\Contracts\ExceptionErrors\IAggregateExceptionTestBuilder.cs" />
76+
<Compile Include="Builders\Contracts\ExceptionErrors\IAndAggregateExceptionTestBuilder.cs" />
77+
<Compile Include="Builders\Contracts\ExceptionErrors\IBaseExceptionTestBuilder.cs" />
7678
<Compile Include="Builders\Contracts\HttpActionResults\Ok\IAndOkTestBuilder.cs" />
7779
<Compile Include="Builders\Contracts\HttpActionResults\Ok\IOkTestBuilder.cs" />
7880
<Compile Include="Builders\Contracts\HttpRequests\IAndHttpRequestMessageBuilder.cs" />

0 commit comments

Comments
 (0)