Skip to content

Commit 247d5ec

Browse files
committed
Merge pull request #158 from ivaylokenov/development
Version 0.9
2 parents 8394890 + 7c8e446 commit 247d5ec

File tree

20 files changed

+827
-92
lines changed

20 files changed

+827
-92
lines changed

documentation/README.md

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ MyWebApi
6363
.Routes();
6464

6565
// configures the HTTP configuration
66-
// for a specific test case by passing it to the metho
66+
// for a specific test case by passing it to the method
6767
MyWebApi
6868
.Routes(httpConfiguration);
6969

@@ -130,20 +130,23 @@ MyWebApi
130130

131131
// sets the URI location to test as string
132132
// and adds custom HTTP content header to the request
133+
// * adding content headers requires content to be initialized and set
133134
MyWebApi
134135
.Routes()
135136
.ShouldMap("api/Route/To/Test")
136137
.WithContentHeader("SomeContentHeader", "SomeContentHeaderValue");
137138

138139
// sets the URI location to test as string
139140
// and adds custom HTTP content header with multiple values to the request
141+
// * adding content headers requires content to be initialized and set
140142
MyWebApi
141143
.Routes()
142144
.ShouldMap("api/Route/To/Test")
143145
.WithContentHeader("SomeContentHeader", new[] { "SomeContentHeaderValue", "AnotherContentHeaderValue" });
144146

145147
// sets the URI location to test as string
146148
// and adds custom HTTP content headers as dictionary to the request
149+
// * adding content headers requires content to be initialized and set
147150
MyWebApi
148151
.Routes()
149152
.ShouldMap("api/Route/To/Test")
@@ -463,18 +466,21 @@ MyWebApi
463466
.WithHeaders(someDictionaryWithHeaders));
464467

465468
// adding custom content header to the request message
469+
// * adding content headers requires content to be initialized and set
466470
MyWebApi
467471
.Controller<WebApiController>()
468472
.WithHttpRequestMessage(request => request
469473
.WithContentHeader("SomeContentHeader", "SomeContentHeaderValue"));
470474

471475
// adding custom content header with multiple values to the request message
476+
// * adding content headers requires content to be initialized and set
472477
MyWebApi
473478
.Controller<WebApiController>()
474479
.WithHttpRequestMessage(request => request
475480
.WithContentHeader("SomeContentHeader", new[] { "SomeContentHeaderValue", "AnotherContentHeaderValue" }));
476481

477482
// adding custom content headers provided as dictionary to the request message
483+
// * adding content headers requires content to be initialized and set
478484
MyWebApi
479485
.Controller<WebApiController>()
480486
.WithHttpRequestMessage(request => request
@@ -1261,13 +1267,40 @@ MyWebApi
12611267
.ContainingHeaders(someDictionaryWithHeaders);
12621268

12631269
// tests whether the action returns HttpResponseMessage
1264-
// containing headers provided as HttpResponseHeaders
1270+
// containing content header with specific name
12651271
MyWebApi
12661272
.Controller<WebApiController>()
12671273
.Calling(c => c.SomeAction())
12681274
.ShouldReturn()
12691275
.HttpResponseMessage()
1270-
.ContainingHeaders(someHttpReponseHeaders);
1276+
.ContainingContentHeader("SomeContentHeader");
1277+
1278+
// tests whether the action returns HttpResponseMessage
1279+
// containing content header with specific name and value
1280+
MyWebApi
1281+
.Controller<WebApiController>()
1282+
.Calling(c => c.SomeAction())
1283+
.ShouldReturn()
1284+
.HttpResponseMessage()
1285+
.ContainingContentHeader("SomeContentHeader", "SomeContentValue");
1286+
1287+
// tests whether the action returns HttpResponseMessage
1288+
// containing content header with specific name and values
1289+
MyWebApi
1290+
.Controller<WebApiController>()
1291+
.Calling(c => c.SomeAction())
1292+
.ShouldReturn()
1293+
.HttpResponseMessage()
1294+
.ContainingContentHeader("SomeContentHeader", new[] { "SomeContentHeaderValue", "AnotherContentHeaderValue" });
1295+
1296+
// tests whether the action returns HttpResponseMessage
1297+
// containing content headers provided as dictionary
1298+
MyWebApi
1299+
.Controller<WebApiController>()
1300+
.Calling(c => c.SomeAction())
1301+
.ShouldReturn()
1302+
.HttpResponseMessage()
1303+
.ContainingContentHeaders(someDictionaryWithContentHeaders);
12711304

12721305
// tests whether the action returns HttpResponseMessage
12731306
// with specific status code
@@ -1783,8 +1816,18 @@ MyWebApi
17831816
.Calling(c => c.SomeAction())
17841817
.ShouldReturn()
17851818
.Redirect();
1819+
1820+
// tests whether the action returns
1821+
// RedirectToRouteResult to specific route values
1822+
// provided by lambda expression
1823+
MyWebApi
1824+
.Controller<WebApiController>()
1825+
.Calling(c => c.SomeAction())
1826+
.ShouldReturn()
1827+
.Redirect()
1828+
.To<AnotherController>(c => c.AnotherAction("ParameterRouteValue"));
17861829

1787-
// tests whether the action returns redirect result
1830+
// tests whether the action returns RedirectResult
17881831
// with location provided as string
17891832
MyWebApi
17901833
.Controller<WebApiController>()
@@ -1793,7 +1836,7 @@ MyWebApi
17931836
.Redirect()
17941837
.AtLocation("http://somehost.com/someuri/1?query=someQuery");
17951838

1796-
// tests whether the action returns redirect result
1839+
// tests whether the action returns RedirectResult
17971840
// with location provided as URI
17981841
MyWebApi
17991842
.Controller<WebApiController>()
@@ -1802,7 +1845,7 @@ MyWebApi
18021845
.Redirect()
18031846
.AtLocation(someUri);
18041847

1805-
// tests whether the action returns redirect result
1848+
// tests whether the action returns RedirectResult
18061849
// with location provided as URI builder
18071850
MyWebApi
18081851
.Controller<WebApiController>()
@@ -2006,6 +2049,16 @@ MyWebApi
20062049
.Created()
20072050
.WithResponseModelOfType<ResponseModel>();
20082051

2052+
// tests whether the action returns
2053+
// CreatedAtRouteNegotiatedContentResult<T> to specific route values
2054+
// provided by lambda expression
2055+
MyWebApi
2056+
.Controller<WebApiController>()
2057+
.Calling(c => c.SomeAction())
2058+
.ShouldReturn()
2059+
.Created()
2060+
.At<AnotherController>(c => c.AnotherAction("ParameterRouteValue"));
2061+
20092062
// tests whether the action returns created result
20102063
// with DefaultContentNegotiator
20112064
MyWebApi

src/MyWebApi.Tests/BuildersTests/HttpActionResultsTests/CreatedTests/CreatedTestBuilderTests.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,5 +411,69 @@ public void WithResponseModelOfTypeShouldWorkCorrectly()
411411
.Created()
412412
.WithResponseModelOfType<ICollection<ResponseModel>>();
413413
}
414+
415+
[Test]
416+
public void AtShouldWorkCorrectlyWithCorrectActionCall()
417+
{
418+
MyWebApi
419+
.Controller<WebApiController>()
420+
.Calling(c => c.CreatedAtRouteAction())
421+
.ShouldReturn()
422+
.Created()
423+
.At<NoAttributesController>(c => c.WithParameter(1));
424+
}
425+
426+
[Test]
427+
public void AtShouldWorkCorrectlyWithCorrectVoidActionCall()
428+
{
429+
MyWebApi
430+
.Controller<WebApiController>()
431+
.Calling(c => c.CreatedAtRouteVoidAction())
432+
.ShouldReturn()
433+
.Created()
434+
.At<NoAttributesController>(c => c.VoidAction());
435+
}
436+
437+
[Test]
438+
[ExpectedException(
439+
typeof(CreatedResultAssertionException),
440+
ExpectedMessage = "When calling CreatedAtRouteAction action in WebApiController expected created result to redirect to '/api/Redirect/WithParameter?id=2', but in fact redirected to '/api/Redirect/WithParameter?id=1'.")]
441+
public void AtShouldThrowExceptionWithIncorrectActionParameter()
442+
{
443+
MyWebApi
444+
.Controller<WebApiController>()
445+
.Calling(c => c.CreatedAtRouteAction())
446+
.ShouldReturn()
447+
.Created()
448+
.At<NoAttributesController>(c => c.WithParameter(2));
449+
}
450+
451+
[Test]
452+
[ExpectedException(
453+
typeof(CreatedResultAssertionException),
454+
ExpectedMessage = "When calling CreatedAtRouteAction action in WebApiController expected created result to redirect to a specific URI, but such URI could not be resolved from the 'Redirect' route template.")]
455+
public void AtShouldThrowExceptionWithIncorrectActionCall()
456+
{
457+
MyWebApi
458+
.Controller<WebApiController>()
459+
.Calling(c => c.CreatedAtRouteAction())
460+
.ShouldReturn()
461+
.Created()
462+
.At<RouteController>(c => c.VoidAction());
463+
}
464+
465+
[Test]
466+
[ExpectedException(
467+
typeof(ActionCallAssertionException),
468+
ExpectedMessage = "Expected action result to contain a 'RouteName' property to test, but in fact such property was not found.")]
469+
public void AtShouldThrowExceptionWithIncorrectActionResult()
470+
{
471+
MyWebApi
472+
.Controller<WebApiController>()
473+
.Calling(c => c.CreatedAction())
474+
.ShouldReturn()
475+
.Created()
476+
.At<RouteController>(c => c.VoidAction());
477+
}
414478
}
415479
}

src/MyWebApi.Tests/BuildersTests/HttpActionResultsTests/RedirectTests/RedirectTestBuilderTests.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,5 +136,69 @@ public void AtLocationWithBuilderShouldThrowExceptionIfTheLocationIsIncorrect()
136136
.AndAlso()
137137
.WithQuery("?query=Test"));
138138
}
139+
140+
[Test]
141+
public void ToShouldWorkCorrectlyWithCorrectActionCall()
142+
{
143+
MyWebApi
144+
.Controller<WebApiController>()
145+
.Calling(c => c.RedirectToRouteAction())
146+
.ShouldReturn()
147+
.Redirect()
148+
.To<NoAttributesController>(c => c.WithParameter(1));
149+
}
150+
151+
[Test]
152+
public void ToShouldWorkCorrectlyWithCorrectVoidActionCall()
153+
{
154+
MyWebApi
155+
.Controller<WebApiController>()
156+
.Calling(c => c.RedirectToRouteVoidAction())
157+
.ShouldReturn()
158+
.Redirect()
159+
.To<NoAttributesController>(c => c.VoidAction());
160+
}
161+
162+
[Test]
163+
[ExpectedException(
164+
typeof(RedirectResultAssertionException),
165+
ExpectedMessage = "When calling RedirectToRouteAction action in WebApiController expected redirect result to redirect to '/api/Redirect/WithParameter?id=2', but in fact redirected to '/api/Redirect/WithParameter?id=1'.")]
166+
public void ToShouldThrowExceptionWithIncorrectActionParameter()
167+
{
168+
MyWebApi
169+
.Controller<WebApiController>()
170+
.Calling(c => c.RedirectToRouteAction())
171+
.ShouldReturn()
172+
.Redirect()
173+
.To<NoAttributesController>(c => c.WithParameter(2));
174+
}
175+
176+
[Test]
177+
[ExpectedException(
178+
typeof(RedirectResultAssertionException),
179+
ExpectedMessage = "When calling RedirectToRouteAction action in WebApiController expected redirect result to redirect to a specific URI, but such URI could not be resolved from the 'Redirect' route template.")]
180+
public void ToShouldThrowExceptionWithIncorrectActionCall()
181+
{
182+
MyWebApi
183+
.Controller<WebApiController>()
184+
.Calling(c => c.RedirectToRouteAction())
185+
.ShouldReturn()
186+
.Redirect()
187+
.To<RouteController>(c => c.VoidAction());
188+
}
189+
190+
[Test]
191+
[ExpectedException(
192+
typeof(RedirectResultAssertionException),
193+
ExpectedMessage = "When calling RedirectAction action in WebApiController expected redirect result to contain route name, but it could not be found.")]
194+
public void ToShouldThrowExceptionWithIncorrectActionResult()
195+
{
196+
MyWebApi
197+
.Controller<WebApiController>()
198+
.Calling(c => c.RedirectAction())
199+
.ShouldReturn()
200+
.Redirect()
201+
.To<RouteController>(c => c.VoidAction());
202+
}
139203
}
140204
}

src/MyWebApi.Tests/BuildersTests/HttpRequestMessageBuilderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ public void WithContentHeadersDictionaryShouldPopulateCorrectHeaders()
284284
[Test]
285285
[ExpectedException(
286286
typeof(InvalidHttpRequestMessageException),
287-
ExpectedMessage = "When building HttpRequestMessage expected content to be initialized and set in order to add content headers, but instead received null.")]
287+
ExpectedMessage = "When building HttpRequestMessage expected content to be initialized and set in order to add content headers.")]
288288
public void WithContentHeadersShouldThrowExpcetionIfNoContentIsPresent()
289289
{
290290
MyWebApi

0 commit comments

Comments
 (0)