Skip to content

Commit 1f039f8

Browse files
committed
Added unit tests for ShouldReturnContent (#43)
1 parent 3815385 commit 1f039f8

File tree

7 files changed

+166
-1
lines changed

7 files changed

+166
-1
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.ActionsTests.ShouldReturn
18+
{
19+
using Exceptions;
20+
using NUnit.Framework;
21+
using Setups.Controllers;
22+
23+
[TestFixture]
24+
public class ShouldReturnContentTests
25+
{
26+
[Test]
27+
public void ShouldReturnContentShouldNotThrowExceptionWithNegotiatedContentResult()
28+
{
29+
MyWebApi
30+
.Controller<WebApiController>()
31+
.Calling(c => c.ContentAction())
32+
.ShouldReturn()
33+
.Content();
34+
}
35+
36+
[Test]
37+
public void ShouldReturnContentShouldNotThrowExceptionWithMediaTypeContentResult()
38+
{
39+
MyWebApi
40+
.Controller<WebApiController>()
41+
.Calling(c => c.ContentActionWithMediaType())
42+
.ShouldReturn()
43+
.Content();
44+
}
45+
46+
[Test]
47+
[ExpectedException(
48+
typeof(HttpActionResultAssertionException),
49+
ExpectedMessage = "When calling BadRequestAction action in WebApiController expected action result to be NegotiatedContentResult<T> or FormattedContentResult<T>, but instead received BadRequestResult.")]
50+
public void ShouldReturnContentShouldThrowExceptionWithBadRequestResult()
51+
{
52+
MyWebApi
53+
.Controller<WebApiController>()
54+
.Calling(c => c.BadRequestAction())
55+
.ShouldReturn()
56+
.Content();
57+
}
58+
}
59+
}
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.HttpActionResultsTests.ContentTests
18+
{
19+
using System.Collections.Generic;
20+
using System.Net;
21+
using Exceptions;
22+
using NUnit.Framework;
23+
using Setups.Controllers;
24+
using Setups.Models;
25+
26+
[TestFixture]
27+
public class ContentTestBuilderTests
28+
{
29+
[Test]
30+
public void WithStatusCodeShouldNotThrowExceptionWhenActionReturnsCorrectStatusCode()
31+
{
32+
MyWebApi
33+
.Controller<WebApiController>()
34+
.Calling(c => c.ContentAction())
35+
.ShouldReturn()
36+
.Content()
37+
.WithStatusCode(HttpStatusCode.OK);
38+
}
39+
40+
[Test]
41+
[ExpectedException(
42+
typeof(ContentResultAssertionException),
43+
ExpectedMessage = "When calling ContentAction action in WebApiController expected to have 404 (NotFound) status code, but received 200 (OK).")]
44+
public void WithStatusCodeShouldThrowExceptionWhenActionReturnsWrongStatusCode()
45+
{
46+
MyWebApi
47+
.Controller<WebApiController>()
48+
.Calling(c => c.ContentAction())
49+
.ShouldReturn()
50+
.Content()
51+
.WithStatusCode(HttpStatusCode.NotFound);
52+
}
53+
54+
[Test]
55+
public void WithResponseModelOfTypeShouldWorkCorrectly()
56+
{
57+
MyWebApi
58+
.Controller<WebApiController>()
59+
.Calling(c => c.ContentAction())
60+
.ShouldReturn()
61+
.Content()
62+
.WithResponseModelOfType<ICollection<ResponseModel>>();
63+
}
64+
}
65+
}

src/MyWebApi.Tests/MyWebApi.Tests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<ItemGroup>
6060
<Compile Include="BuildersTests\ActionsTests\ShouldReturn\ShouldReturnBadRequestTests.cs" />
6161
<Compile Include="BuildersTests\ActionsTests\ShouldReturn\ShouldReturnConflictTests.cs" />
62+
<Compile Include="BuildersTests\ActionsTests\ShouldReturn\ShouldReturnContentTests.cs" />
6263
<Compile Include="BuildersTests\ActionsTests\ShouldReturn\ShouldReturnCreatedTests.cs" />
6364
<Compile Include="BuildersTests\ActionsTests\ShouldReturn\ShouldReturnInternalServerErrorTests.cs" />
6465
<Compile Include="BuildersTests\ActionsTests\ShouldReturn\ShouldReturnJsonTests.cs" />
@@ -73,6 +74,7 @@
7374
<Compile Include="BuildersTests\HttpActionResultsTests\BadRequestTests\BadRequestErrorMessageTestBuilderTests.cs" />
7475
<Compile Include="BuildersTests\HttpActionResultsTests\BadRequestTests\BadRequestTestBuilderTests.cs" />
7576
<Compile Include="BuildersTests\ControllerBuilderTests.cs" />
77+
<Compile Include="BuildersTests\HttpActionResultsTests\ContentTests\ContentTestBuilderTests.cs" />
7678
<Compile Include="BuildersTests\HttpActionResultsTests\CreatedTests\CreatedTestBuilderTests.cs" />
7779
<Compile Include="BuildersTests\ExceptionErrorsTests\ExceptionMessageTestBuilderTests.cs" />
7880
<Compile Include="BuildersTests\ExceptionErrorsTests\ExceptionTestBuilderTests.cs" />
@@ -125,6 +127,7 @@
125127
<ItemGroup>
126128
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
127129
</ItemGroup>
130+
<ItemGroup />
128131
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
129132
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
130133
Other similar extension points exist, see Microsoft.Common.targets.

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,15 @@ public IHttpActionResult ContentAction()
121121
return this.Content(HttpStatusCode.OK, this.responseModel);
122122
}
123123

124+
public IHttpActionResult ContentActionWithMediaType()
125+
{
126+
return this.Content(
127+
HttpStatusCode.OK,
128+
this.responseModel,
129+
TestObjectFactory.GetCustomMediaTypeFormatter(),
130+
TestObjectFactory.MediaType);
131+
}
132+
124133
public IHttpActionResult CreatedAction()
125134
{
126135
return this.Created(TestObjectFactory.GetUri().OriginalString, this.responseModel);

src/MyWebApi.Tests/Setups/TestObjectFactory.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ namespace MyWebApi.Tests.Setups
3030

3131
public static class TestObjectFactory
3232
{
33+
public const string MediaType = "application/json";
34+
3335
public static IEnumerable<MediaTypeFormatter> GetFormatters()
3436
{
3537
return new List<MediaTypeFormatter>

src/MyWebApi.Tests/UtilitiesTests/ValidatorsTests/MediaTypeFormatterValidatorTests.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ namespace MyWebApi.Tests.UtilitiesTests.ValidatorsTests
1818
{
1919
using System;
2020
using System.Linq;
21+
using System.Net;
2122
using System.Net.Http.Formatting;
23+
using System.Net.Http.Headers;
2224
using System.Web.Http.ModelBinding;
2325
using System.Web.Http.Results;
2426
using NUnit.Framework;
@@ -52,14 +54,32 @@ public void GetDefaultMediaTypeFormattersShouldReturnProperFormatters()
5254
public void ValidateMediaTypeFormatterShouldNotThrowExceptionWithCorrectMediaTypeFormatter()
5355
{
5456
var actionResultWithFormatters = new CreatedNegotiatedContentResult<int>(
55-
TestObjectFactory.GetUri(), 5, MyWebApi.Controller<WebApiController>().Controller);
57+
TestObjectFactory.GetUri(),
58+
5,
59+
MyWebApi.Controller<WebApiController>().Controller);
5660

5761
MediaTypeFormatterValidator.ValidateMediaTypeFormatter(
5862
actionResultWithFormatters,
5963
new FormUrlEncodedMediaTypeFormatter(),
6064
TestObjectFactory.GetFailingValidationAction());
6165
}
6266

67+
[Test]
68+
public void ValidateMediaTypeFormatterShouldNotThrowExceptionWithSingleCorrectMediaTypeFormatter()
69+
{
70+
var actionResultWithFormatter = new FormattedContentResult<int>(
71+
HttpStatusCode.OK,
72+
5,
73+
TestObjectFactory.GetCustomMediaTypeFormatter(),
74+
new MediaTypeHeaderValue(TestObjectFactory.MediaType),
75+
MyWebApi.Controller<WebApiController>().Controller);
76+
77+
MediaTypeFormatterValidator.ValidateMediaTypeFormatter(
78+
actionResultWithFormatter,
79+
TestObjectFactory.GetCustomMediaTypeFormatter(),
80+
TestObjectFactory.GetFailingValidationAction());
81+
}
82+
6383
[Test]
6484
[ExpectedException(
6585
typeof(NullReferenceException),

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace MyWebApi.Builders.Contracts.Actions
2020
using System.Net;
2121
using Base;
2222
using HttpActionResults.BadRequest;
23+
using HttpActionResults.Content;
2324
using HttpActionResults.Created;
2425
using HttpActionResults.InternalServerError;
2526
using HttpActionResults.Json;
@@ -64,6 +65,12 @@ public interface IShouldReturnTestBuilder<TActionResult> : IBaseTestBuilderWithA
6465
/// <returns>Created test builder.</returns>
6566
ICreatedTestBuilder Created();
6667

68+
/// <summary>
69+
/// Tests whether action result is NegotiatedContentResult{T} or FormattedContentResult{T}.
70+
/// </summary>
71+
/// <returns>Content test builder.</returns>
72+
IContentTestBuilder Content();
73+
6774
/// <summary>
6875
/// Tests whether action result is RedirectResult or RedirectToRouteResult.
6976
/// </summary>

0 commit comments

Comments
 (0)