Skip to content

Commit 5565054

Browse files
committed
Merge pull request #103 from ivaylokenov/content-test-builder
Content test builder
2 parents 76769fe + fb0406a commit 5565054

File tree

84 files changed

+2843
-391
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+2843
-391
lines changed

documentation/README.md

Lines changed: 271 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- [JSON result](#json-result)
1919
- [StatusCode result](#statuscode-result)
2020
- [Redirect result](#redirect-result)
21+
- [Content result](#content-result)
2122
- [Created result](#created-result)
2223
- [NotFound result](#notfound-result)
2324
- [Conflict result](#conflict-result)
@@ -341,6 +342,97 @@ MyWebApi
341342
.Ok()
342343
.WithNoResponseModel();
343344

345+
// tests whether the action returns OkNegotiatedContentResult<T>
346+
// with DefaultContentNegotiator
347+
MyWebApi
348+
.Controller<WebApiController>()
349+
.Calling(c => c.SomeAction())
350+
.ShouldReturn()
351+
.Ok()
352+
.WithDefaultContentNegotiator();
353+
354+
// tests whether the action returns OkNegotiatedContentResult<T>
355+
// with custom IContentNegotiator provided by instance
356+
MyWebApi
357+
.Controller<WebApiController>()
358+
.Calling(c => c.SomeAction())
359+
.ShouldReturn()
360+
.Ok()
361+
.WithContentNegotiator(customContentNegotiator);
362+
363+
// tests whether the action returns OkNegotiatedContentResult<T>
364+
// with custom IContentNegotiator provided by generic definition
365+
MyWebApi
366+
.Controller<WebApiController>()
367+
.Calling(c => c.SomeAction())
368+
.ShouldReturn()
369+
.Ok()
370+
.WithContentNegotiatorOfType<CustomContentNegotiator>();
371+
372+
// tests whether the action returns OkNegotiatedContentResult<T>
373+
// with exactly the default media type formatters
374+
MyWebApi
375+
.Controller<WebApiController>()
376+
.Calling(c => c.SomeAction())
377+
.ShouldReturn()
378+
.Ok()
379+
.ContainingDefaultFormatters();
380+
381+
// tests whether the action returns OkNegotiatedContentResult<T>
382+
// containing media type formatter provided by instance
383+
MyWebApi
384+
.Controller<WebApiController>()
385+
.Calling(c => c.SomeAction())
386+
.ShouldReturn()
387+
.Ok()
388+
.ContainingMediaTypeFormatter(someMediaTypeFormatter);
389+
390+
// tests whether the action returns OkNegotiatedContentResult<T>
391+
// containing media type formatter provided by generic definition
392+
MyWebApi
393+
.Controller<WebApiController>()
394+
.Calling(c => c.SomeAction())
395+
.ShouldReturn()
396+
.Ok()
397+
.ContainingMediaTypeFormatterOfType<JsonMediaTypeFormatter>();
398+
399+
// tests whether the action returns OkNegotiatedContentResult<T>
400+
// with exactly the provided media type formatters
401+
MyWebApi
402+
.Controller<WebApiController>()
403+
.Calling(c => c.SomeAction())
404+
.ShouldReturn()
405+
.Ok()
406+
.ContainingMediaTypeFormatters(someCollectionOfMediaTypeFormatters);
407+
408+
// tests whether the action returns OkNegotiatedContentResult<T>
409+
// with exactly the provided media type formatters
410+
// by using params
411+
MyWebApi
412+
.Controller<WebApiController>()
413+
.Calling(c => c.SomeAction())
414+
.ShouldReturn()
415+
.Ok()
416+
.ContainingMediaTypeFormatters(
417+
someMediaTypeFormatter,
418+
anotherMediaTypeFormatter,
419+
yetAnotherMediaTypeFormatter);
420+
421+
// tests whether the action returns OkNegotiatedContentResult<T>
422+
// with exactly the provided media type formatters
423+
// by using formatters builder
424+
MyWebApi
425+
.Controller<WebApiController>()
426+
.Calling(c => c.SomeAction())
427+
.ShouldReturn()
428+
.Ok()
429+
.ContainingMediaTypeFormatters(
430+
formatters =>
431+
formatters
432+
.ContainingMediaTypeFormatter(someMediaTypeFormatter)
433+
.AndAlso()
434+
.ContainingMediaTypeFormatterOfType<SomeMediaTypeFormatter>());
435+
344436
// tests whether the action returns OkResult with specific object
345437
MyWebApi
346438
.Controller<WebApiController>()
@@ -381,6 +473,17 @@ MyWebApi
381473
.WithResponseModelOfType<ResponseModel>()
382474
.Passing(m => m.Id == 1);
383475

476+
// tests whether the action returns OkNegotiatedContentResult<T>
477+
// with different types of properties by using AndAlso()
478+
MyWebApi
479+
.Controller<WebApiController>()
480+
.Calling(c => c.SomeAction())
481+
.ShouldReturn()
482+
.Ok()
483+
.WithDefaultContentNegotiator()
484+
.AndAlso() // AndAlso is not necessary
485+
.WithResponseModelOfType<ResponseModel>();
486+
384487
// tests for model state errors for the response model
385488
// * not very useful in practice
386489
MyWebApi
@@ -718,6 +821,162 @@ MyWebApi
718821

719822
[To top](#table-of-contents)
720823

824+
#### Content result
825+
826+
```c#
827+
// tests whether the action returns
828+
// NegotiatedContentResult<T>
829+
// or FormattedContentResult<T>
830+
MyWebApi
831+
.Controller<WebApiController>()
832+
.Calling(c => c.SomeAction())
833+
.ShouldReturn()
834+
.Content();
835+
836+
// tests whether the action returns
837+
// content with specific response model
838+
MyWebApi
839+
.Controller<WebApiController>()
840+
.Calling(c => c.SomeAction())
841+
.ShouldReturn()
842+
.Content()
843+
.WithResponseModelOfType<ResponseModel>();
844+
845+
// tests whether the action returns
846+
// content with specific status code result
847+
MyWebApi
848+
.Controller<WebApiController>()
849+
.Calling(c => c.SomeAction())
850+
.ShouldReturn()
851+
.Content()
852+
.WithStatusCode(HttpStatusCode.OK);
853+
854+
// tests whether the action returns
855+
// content with specific media type
856+
// provided by string
857+
MyWebApi
858+
.Controller<WebApiController>()
859+
.Calling(c => c.SomeAction())
860+
.ShouldReturn()
861+
.Content()
862+
.WithMediaType("application/json");
863+
864+
// tests whether the action returns
865+
// content with specific media type
866+
// provided by predefined in the library constants
867+
MyWebApi
868+
.Controller<WebApiController>()
869+
.Calling(c => c.SomeAction())
870+
.ShouldReturn()
871+
.Content()
872+
.WithMediaType(MediaType.ApplicationJson);
873+
874+
// tests whether the action returns
875+
// content with specific media type
876+
// provided by MediaTypeHeaderValue class
877+
MyWebApi
878+
.Controller<WebApiController>()
879+
.Calling(c => c.SomeAction())
880+
.ShouldReturn()
881+
.Content()
882+
.WithMediaType(new MediaTypeHeaderValue("application/json"));
883+
884+
// tests whether the action returns
885+
// content with DefaultContentNegotiator
886+
MyWebApi
887+
.Controller<WebApiController>()
888+
.Calling(c => c.SomeAction())
889+
.ShouldReturn()
890+
.Content()
891+
.WithDefaultContentNegotiator();
892+
893+
// tests whether the action returns
894+
// content with custom content negotiator
895+
MyWebApi
896+
.Controller<WebApiController>()
897+
.Calling(c => c.SomeAction())
898+
.ShouldReturn()
899+
.Content()
900+
.WithContentNegotiator(customContentNegotiator);
901+
902+
// tests whether the action returns
903+
// content with custom content negotiator
904+
// provided by generic definition
905+
MyWebApi
906+
.Controller<WebApiController>()
907+
.Calling(c => c.SomeAction())
908+
.ShouldReturn()
909+
.Content()
910+
.WithContentNegotiatorOfType<CustomContentNegotiator>();
911+
912+
// tests whether the action returns
913+
// content containing media type formatter
914+
// provided by an instance
915+
MyWebApi
916+
.Controller<WebApiController>()
917+
.Calling(c => c.SomeAction())
918+
.ShouldReturn()
919+
.Content()
920+
.ContainingMediaTypeFormatter(customMediaTypeFormatter);
921+
922+
// tests whether the action returns
923+
// content containing media type formatter
924+
// provided by an generic definition
925+
MyWebApi
926+
.Controller<WebApiController>()
927+
.Calling(c => c.SomeAction())
928+
.ShouldReturn()
929+
.Content()
930+
.ContainingMediaTypeFormatterOfType<JsonMediaTypeFormatter>();
931+
932+
// tests whether the action returns
933+
// content containing the default media type formatter
934+
MyWebApi
935+
.Controller<WebApiController>()
936+
.Calling(c => c.SomeAction())
937+
.ShouldReturn()
938+
.Content()
939+
.ContainingDefaultFormatters();
940+
941+
// tests whether the action returns
942+
// content containing the media type formatters
943+
// provided in a collection
944+
MyWebApi
945+
.Controller<WebApiController>()
946+
.Calling(c => c.ContentActionWithCustomFormatters())
947+
.ShouldReturn()
948+
.Content()
949+
.ContainingMediaTypeFormatters(collectionOfMediaTypeFormatters);
950+
951+
// tests whether the action returns
952+
// content containing the media type formatters
953+
// provided by a builder
954+
MyWebApi
955+
.Controller<WebApiController>()
956+
.Calling(c => c.ContentAction())
957+
.ShouldReturn()
958+
.Content()
959+
.ContainingMediaTypeFormatters(
960+
formatters => formatters
961+
.ContainingMediaTypeFormatter(new JsonMediaTypeFormatter)
962+
.AndAlso()
963+
.ContainingMediaTypeFormatterOfType<FormUrlEncodedMediaTypeFormatter>());
964+
965+
// tests whether the action returns
966+
// content with status code and response model
967+
// combined by AndAlso
968+
MyWebApi
969+
.Controller<WebApiController>()
970+
.Calling(c => c.ContentAction())
971+
.ShouldReturn()
972+
.Content()
973+
.WithStatusCode(HttpStatusCode.OK)
974+
.AndAlso() // AndAlso is not necessary
975+
.WithResponseModelOfType<ResponseModel>();
976+
```
977+
978+
[To top](#table-of-contents)
979+
721980
#### Created result
722981

723982
```c#
@@ -730,6 +989,17 @@ MyWebApi
730989
.ShouldReturn()
731990
.Created();
732991

992+
// tests whether the action returns
993+
// CreatedNegotiatedContentResult<T>
994+
// or CreatedAtRouteNegotiatedContentResult<T>
995+
// with specific response model type
996+
MyWebApi
997+
.Controller<WebApiController>()
998+
.Calling(c => c.SomeAction())
999+
.ShouldReturn()
1000+
.Created()
1001+
.WithResponseModelOfType<ResponseModel>();
1002+
7331003
// tests whether the action returns created result
7341004
// with DefaultContentNegotiator
7351005
MyWebApi
@@ -865,7 +1135,7 @@ MyWebApi
8651135
// with different types of properties by using AndAlso()
8661136
MyWebApi
8671137
.Controller<WebApiController>()
868-
.Calling(c => c.CreatedActionWithCustomContentNegotiator())
1138+
.Calling(c => c.SomeAction())
8691139
.ShouldReturn()
8701140
.Created()
8711141
.WithDefaultContentNegotiator()
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+
}

src/MyWebApi.Tests/BuildersTests/BadRequestsTests/BadRequestErrorMessageTestBuilderTests.cs renamed to src/MyWebApi.Tests/BuildersTests/HttpActionResultsTests/BadRequestTests/BadRequestErrorMessageTestBuilderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with this program. If not, see http://www.gnu.org/licenses/.
1616

17-
namespace MyWebApi.Tests.BuildersTests.BadRequestsTests
17+
namespace MyWebApi.Tests.BuildersTests.HttpActionResultsTests.BadRequestTests
1818
{
1919
using Exceptions;
2020
using NUnit.Framework;

src/MyWebApi.Tests/BuildersTests/BadRequestsTests/BadRequestTestBuilderTests.cs renamed to src/MyWebApi.Tests/BuildersTests/HttpActionResultsTests/BadRequestTests/BadRequestTestBuilderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with this program. If not, see http://www.gnu.org/licenses/.
1616

17-
namespace MyWebApi.Tests.BuildersTests.BadRequestsTests
17+
namespace MyWebApi.Tests.BuildersTests.HttpActionResultsTests.BadRequestTests
1818
{
1919
using System.Web.Http.ModelBinding;
2020
using Exceptions;

0 commit comments

Comments
 (0)