Skip to content

Commit ec71126

Browse files
committed
Merge pull request #105 from ivaylokenov/development
Version 0.6
2 parents 46b8d68 + 45bbbc8 commit ec71126

File tree

94 files changed

+3019
-404
lines changed

Some content is hidden

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

94 files changed

+3019
-404
lines changed

documentation/README.md

Lines changed: 279 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)
@@ -136,6 +137,14 @@ MyWebApi
136137
.ShouldHave()
137138
.InvalidModelState();
138139

140+
// tests whether model state is not valid
141+
// with specific number of errors
142+
MyWebApi
143+
.Controller<WebApiController>()
144+
.Calling(c => c.SomeAction(requestModel))
145+
.ShouldHave()
146+
.InvalidModelState(withNumberOfErrors: 5);
147+
139148
// tests whether model state is valid and returns some action result
140149
MyWebApi
141150
.Controller<WebApiController>()
@@ -333,6 +342,97 @@ MyWebApi
333342
.Ok()
334343
.WithNoResponseModel();
335344

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+
336436
// tests whether the action returns OkResult with specific object
337437
MyWebApi
338438
.Controller<WebApiController>()
@@ -373,6 +473,17 @@ MyWebApi
373473
.WithResponseModelOfType<ResponseModel>()
374474
.Passing(m => m.Id == 1);
375475

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+
376487
// tests for model state errors for the response model
377488
// * not very useful in practice
378489
MyWebApi
@@ -710,6 +821,162 @@ MyWebApi
710821

711822
[To top](#table-of-contents)
712823

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+
713980
#### Created result
714981

715982
```c#
@@ -722,6 +989,17 @@ MyWebApi
722989
.ShouldReturn()
723990
.Created();
724991

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+
7251003
// tests whether the action returns created result
7261004
// with DefaultContentNegotiator
7271005
MyWebApi
@@ -857,7 +1135,7 @@ MyWebApi
8571135
// with different types of properties by using AndAlso()
8581136
MyWebApi
8591137
.Controller<WebApiController>()
860-
.Calling(c => c.CreatedActionWithCustomContentNegotiator())
1138+
.Calling(c => c.SomeAction())
8611139
.ShouldReturn()
8621140
.Created()
8631141
.WithDefaultContentNegotiator()

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,33 @@ public void ShouldHaveInvalidModelStateShouldBeValidWithInvalidRequestModel()
8080
.InvalidModelState();
8181
}
8282

83+
[Test]
84+
public void ShouldHaveInvalidModelStateShouldBeValidWithInvalidRequestModelAndCorrectNumberOfErrors()
85+
{
86+
var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();
87+
88+
MyWebApi
89+
.Controller<WebApiController>()
90+
.Calling(c => c.ModelStateCheck(requestModelWithErrors))
91+
.ShouldHave()
92+
.InvalidModelState(2);
93+
}
94+
95+
[Test]
96+
[ExpectedException(
97+
typeof(ModelErrorAssertionException),
98+
ExpectedMessage = "When calling ModelStateCheck action in WebApiController expected to have invalid model state with 5 errors, but contained 2.")]
99+
public void ShouldHaveInvalidModelStateShouldBeInvalidWithInvalidRequestModelAndIncorrectNumberOfErrors()
100+
{
101+
var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();
102+
103+
MyWebApi
104+
.Controller<WebApiController>()
105+
.Calling(c => c.ModelStateCheck(requestModelWithErrors))
106+
.ShouldHave()
107+
.InvalidModelState(5);
108+
}
109+
83110
[Test]
84111
[ExpectedException(
85112
typeof(ModelErrorAssertionException),
@@ -95,6 +122,21 @@ public void ShouldHaveInvalidModelStateShouldThrowExceptionWithValidRequestModel
95122
.InvalidModelState();
96123
}
97124

125+
[Test]
126+
[ExpectedException(
127+
typeof(ModelErrorAssertionException),
128+
ExpectedMessage = "When calling ModelStateCheck action in WebApiController expected to have invalid model state with 5 errors, but contained 0.")]
129+
public void ShouldHaveInvalidModelStateShouldThrowExceptionWithValidRequestModelAndProvidedNumberOfErrors()
130+
{
131+
var requestModel = TestObjectFactory.GetValidRequestModel();
132+
133+
MyWebApi
134+
.Controller<WebApiController>()
135+
.Calling(c => c.ModelStateCheck(requestModel))
136+
.ShouldHave()
137+
.InvalidModelState(withNumberOfErrors: 5);
138+
}
139+
98140
[Test]
99141
public void AndShouldWorkCorrectlyWithValidModelState()
100142
{

0 commit comments

Comments
 (0)