Skip to content

Commit 5f8735a

Browse files
committed
Added unit tests for AggregateException testing (#73)
1 parent 24ae51b commit 5f8735a

File tree

9 files changed

+122
-6
lines changed

9 files changed

+122
-6
lines changed

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

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
namespace MyWebApi.Tests.BuildersTests.ActionsTests
1818
{
1919
using System;
20-
using System.Net;
2120
using Exceptions;
2221
using NUnit.Framework;
2322
using Setups.Controllers;
@@ -73,6 +72,52 @@ public void ShouldThrowExceptionShouldThrowWithInvalidTypeOfException()
7372
.OfType<InvalidOperationException>();
7473
}
7574

75+
[Test]
76+
public void ShouldThrowAggregateExceptionShouldCatchAndValidateAggregateException()
77+
{
78+
MyWebApi
79+
.Controller<WebApiController>()
80+
.Calling(c => c.ActionWithAggregateException())
81+
.ShouldThrow()
82+
.AggregateException();
83+
}
84+
85+
[Test]
86+
[ExpectedException(
87+
typeof(InvalidExceptionAssertionException),
88+
ExpectedMessage = "When calling ActionWithException action in WebApiController expected AggregateException, but instead received NullReferenceException.")]
89+
public void ShouldThrowAggregateExceptionShouldThrowIfTheExceptionIsNotValidType()
90+
{
91+
MyWebApi
92+
.Controller<WebApiController>()
93+
.Calling(c => c.ActionWithException())
94+
.ShouldThrow()
95+
.AggregateException();
96+
}
97+
98+
[Test]
99+
public void ShouldThrowAggregateExceptionShouldCatchAndValidateAggregateExceptionWithSpecificNumberOfInnerExceptions()
100+
{
101+
MyWebApi
102+
.Controller<WebApiController>()
103+
.Calling(c => c.ActionWithAggregateException())
104+
.ShouldThrow()
105+
.AggregateException(2);
106+
}
107+
108+
[Test]
109+
[ExpectedException(
110+
typeof(InvalidExceptionAssertionException),
111+
ExpectedMessage = "When calling ActionWithAggregateException action in WebApiController expected AggregateException to contain 3 inner exceptions, but in fact contained 2.")]
112+
public void ShouldThrowAggregateExceptionShouldCatchAndValidateAggregateExceptionWithWrongNumberOfInnerExceptions()
113+
{
114+
MyWebApi
115+
.Controller<WebApiController>()
116+
.Calling(c => c.ActionWithAggregateException())
117+
.ShouldThrow()
118+
.AggregateException(3);
119+
}
120+
76121
[Test]
77122
public void ShouldThrowHttpResponseExceptionShouldCatchAndValidateHttpResponseException()
78123
{

src/MyWebApi.Tests/BuildersTests/ControllerBuilderTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ namespace MyWebApi.Tests.BuildersTests
2020
using System.Collections.Generic;
2121
using System.Linq;
2222
using System.Net.Http;
23-
using System.Net.Http.Headers;
2423
using System.Web.Http.Results;
2524
using Builders.Contracts.Actions;
2625
using Builders.Contracts.Base;
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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.Tests.BuildersTests.ExceptionErrorsTests
18+
{
19+
using System;
20+
using Exceptions;
21+
using NUnit.Framework;
22+
using Setups.Controllers;
23+
24+
[TestFixture]
25+
public class AggregateExceptionTestBuilderTests
26+
{
27+
[Test]
28+
public void ContainingInnerExceptionOfTypeShouldNotThrowIfInnerExceptionIsCorrect()
29+
{
30+
MyWebApi
31+
.Controller<WebApiController>()
32+
.Calling(c => c.ActionWithAggregateException())
33+
.ShouldThrow()
34+
.AggregateException()
35+
.ContainingInnerExceptionOfType<NullReferenceException>();
36+
}
37+
38+
[Test]
39+
[ExpectedException(
40+
typeof(InvalidExceptionAssertionException),
41+
ExpectedMessage = "When calling ActionWithAggregateException action in WebApiController expected AggregateException to contain ArgumentException, but none was found.")]
42+
public void ContainingInnerExceptionOfTypeShouldThrowIfInnerExceptionIsNotCorrect()
43+
{
44+
MyWebApi
45+
.Controller<WebApiController>()
46+
.Calling(c => c.ActionWithAggregateException())
47+
.ShouldThrow()
48+
.AggregateException()
49+
.ContainingInnerExceptionOfType<ArgumentException>();
50+
}
51+
52+
[Test]
53+
public void AndAlsoShouldWorkCorrectly()
54+
{
55+
MyWebApi
56+
.Controller<WebApiController>()
57+
.Calling(c => c.ActionWithAggregateException())
58+
.ShouldThrow()
59+
.AggregateException()
60+
.ContainingInnerExceptionOfType<NullReferenceException>()
61+
.AndAlso()
62+
.ContainingInnerExceptionOfType<InvalidOperationException>();
63+
}
64+
}
65+
}

src/MyWebApi.Tests/BuildersTests/HttpResponseMessageTestBuilderTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ namespace MyWebApi.Tests.BuildersTests
2121
using System.Net;
2222
using System.Net.Http;
2323
using System.Net.Http.Formatting;
24-
using System.Net.Http.Headers;
2524
using Exceptions;
2625
using NUnit.Framework;
2726
using Setups;

src/MyWebApi.Tests/MyWebApi.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
<Compile Include="BuildersTests\ActionsTests\ShouldThrowExceptionTests.cs" />
7373
<Compile Include="BuildersTests\ActionsTests\VoidActionResultTestBuilderTests.cs" />
7474
<Compile Include="BuildersTests\AndTests\AndProvideTestBuilderTests.cs" />
75+
<Compile Include="BuildersTests\ExceptionErrorsTests\AggregateExceptionTestBuilderTests.cs" />
7576
<Compile Include="BuildersTests\ExceptionErrorsTests\HttpResponseExceptionTestBuilderTests.cs" />
7677
<Compile Include="BuildersTests\HttpActionResultsTests\BadRequestTests\BadRequestErrorMessageTestBuilderTests.cs" />
7778
<Compile Include="BuildersTests\HttpActionResultsTests\BadRequestTests\BadRequestTestBuilderTests.cs" />

src/MyWebApi.Tests/Setups/Controllers/WebApiController.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,11 @@ public IHttpActionResult ActionWithException()
279279
throw new NullReferenceException("Test exception message");
280280
}
281281

282+
public IHttpActionResult ActionWithAggregateException()
283+
{
284+
throw new AggregateException(new NullReferenceException(), new InvalidOperationException());
285+
}
286+
282287
public IHttpActionResult ActionWithHttpResponseException()
283288
{
284289
throw new HttpResponseException(HttpStatusCode.NotFound);

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

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

1717
namespace MyWebApi.Builders.Contracts.ExceptionErrors
1818
{
19+
using System;
20+
1921
/// <summary>
2022
/// Used for testing AggregateException.
2123
/// </summary>
@@ -26,6 +28,7 @@ public interface IAggregateExceptionTestBuilder : IBaseExceptionTestBuilder
2628
/// </summary>
2729
/// <typeparam name="TInnerException">Expected inner exception type.</typeparam>
2830
/// <returns>The same aggregate exception test builder.</returns>
29-
IAndAggregateExceptionTestBuilder ContainingInnerExceptionOfType<TInnerException>();
31+
IAndAggregateExceptionTestBuilder ContainingInnerExceptionOfType<TInnerException>()
32+
where TInnerException : Exception;
3033
}
3134
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
namespace MyWebApi.Builders.Contracts.ExceptionErrors
1818
{
19-
using Base;
20-
2119
/// <summary>
2220
/// Used for testing expected exceptions.
2321
/// </summary>

src/MyWebApi/Builders/ExceptionErrors/AggregateExceptionTestBuilder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public AggregateExceptionTestBuilder(
5252
/// <typeparam name="TInnerException">Expected inner exception type.</typeparam>
5353
/// <returns>The same aggregate exception test builder.</returns>
5454
public IAndAggregateExceptionTestBuilder ContainingInnerExceptionOfType<TInnerException>()
55+
where TInnerException : Exception
5556
{
5657
var expectedInnerExceptionType = typeof(TInnerException);
5758
var innerExceptionFound = this.aggregateException.InnerExceptions.Any(e => e.GetType() == expectedInnerExceptionType);

0 commit comments

Comments
 (0)