Skip to content

Commit 77823f1

Browse files
committed
Added With for better readability where parameter values are not important (closes #209)
1 parent c58e5bb commit 77823f1

File tree

9 files changed

+83
-14
lines changed

9 files changed

+83
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Please see the [documentation](https://github.com/ivaylokenov/MyWebApi/tree/mast
1111

1212
## Installation
1313

14-
You can install this library using NuGet into your Test class project. It will automatically reference the needed dependencies of Microsoft.AspNet.WebApi.Core (≥ 5.1.0), Microsoft.Owin.Testing (≥ 3.0.1) and Microsoft.Owin.Host.HttpListener (≥ 3.0.1) for you. .NET 4.5+ is needed. Make sure your solution has the same versions of the mentioned dependencies in all projects where you are using them. For example, if you are using Microsoft.AspNet.WebApi.Core 5.2.3 in your Web project, the same version should be used after installing MyWebApi in your Tests project.
14+
You can install this library using NuGet into your Test class project. It will automatically reference the needed dependencies of Microsoft.AspNet.WebApi.Core (≥ 5.1.0) and Microsoft.Owin.Testing (≥ 3.0.1) for you. .NET 4.5+ is needed. Make sure your solution has the same versions of the mentioned dependencies in all projects where you are using them. For example, if you are using Microsoft.AspNet.WebApi.Core 5.2.3 in your Web project, the same version should be used after installing MyWebApi in your Tests project.
1515

1616
Install-Package MyWebApi
1717

documentation/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,12 @@ MyWebApi
726726
MyWebApi
727727
.Controller<WebApiController>()
728728
.CallingAsync(c => c.SomeActionAsync());
729+
730+
// if action has non-important parameters, instead of adding dummy values
731+
// use With.Any<TParameter> for better readability
732+
MyWebApi
733+
.Controller<WebApiController>()
734+
.Calling(c => c.SomeAction(With.Any<int>()));
729735
```
730736
[To top](#table-of-contents)
731737

@@ -844,6 +850,15 @@ MyWebApi
844850
.ShouldHave()
845851
.ActionAttributes();
846852

853+
// since testing for attributes does not require valid parameters,
854+
// instead of adding dummy values
855+
// use With.Any<TParameter> for better readability
856+
MyWebApi
857+
.Controller<WebApiController>()
858+
.Calling(c => c.SomeAction(With.Any<int>()))
859+
.ShouldHave()
860+
.ActionAttributes();
861+
847862
// tests whether action has specific number of attributes
848863
MyWebApi
849864
.Controller<WebApiController>()

src/MyWebApi.Tests/BuildersTests/AttributesTests/ControllerAttributesTestBuilderTests.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void ChangingRouteToShouldNotThrowExceptionWithControllerWithTheAttribute
3939
MyWebApi
4040
.Controller<AttributesController>()
4141
.ShouldHave()
42-
.Attributes(attributes => attributes.ChangingRouteTo("/api/test"));
42+
.Attributes(attributes => attributes.ChangingRouteTo("api/test"));
4343
}
4444

4545
[Test]
@@ -48,19 +48,19 @@ public void ChangingRouteToShouldNotThrowExceptionWithControllerWithTheAttribute
4848
MyWebApi
4949
.Controller<AttributesController>()
5050
.ShouldHave()
51-
.Attributes(attributes => attributes.ChangingRouteTo("/api/Test"));
51+
.Attributes(attributes => attributes.ChangingRouteTo("api/Test"));
5252
}
5353

5454
[Test]
5555
[ExpectedException(
5656
typeof(AttributeAssertionException),
57-
ExpectedMessage = "When testing AttributesController was expected to have RouteAttribute with '/api/another' template, but in fact found '/api/test'.")]
57+
ExpectedMessage = "When testing AttributesController was expected to have RouteAttribute with 'api/another' template, but in fact found 'api/test'.")]
5858
public void ChangingRouteToShouldThrowExceptionWithControllerWithTheAttributeAndWrongTemplate()
5959
{
6060
MyWebApi
6161
.Controller<AttributesController>()
6262
.ShouldHave()
63-
.Attributes(attributes => attributes.ChangingRouteTo("/api/another"));
63+
.Attributes(attributes => attributes.ChangingRouteTo("api/another"));
6464
}
6565

6666
[Test]
@@ -69,19 +69,19 @@ public void ChangingRouteToShouldNotThrowExceptionWithControllerWithTheAttribute
6969
MyWebApi
7070
.Controller<AttributesController>()
7171
.ShouldHave()
72-
.Attributes(attributes => attributes.ChangingRouteTo("/api/test", withName: "TestRoute"));
72+
.Attributes(attributes => attributes.ChangingRouteTo("api/test", withName: "TestRouteAttributes"));
7373
}
7474

7575
[Test]
7676
[ExpectedException(
7777
typeof(AttributeAssertionException),
78-
ExpectedMessage = "When testing AttributesController was expected to have RouteAttribute with 'AnotherRoute' name, but in fact found 'TestRoute'.")]
78+
ExpectedMessage = "When testing AttributesController was expected to have RouteAttribute with 'AnotherRoute' name, but in fact found 'TestRouteAttributes'.")]
7979
public void ChangingRouteToShouldThrowExceptionWithActionWithTheAttributeAndWrongName()
8080
{
8181
MyWebApi
8282
.Controller<AttributesController>()
8383
.ShouldHave()
84-
.Attributes(attributes => attributes.ChangingRouteTo("/api/test", withName: "AnotherRoute"));
84+
.Attributes(attributes => attributes.ChangingRouteTo("api/test", withName: "AnotherRoute"));
8585
}
8686

8787
[Test]
@@ -90,7 +90,7 @@ public void ChangingRouteToShouldNotThrowExceptionWithActionWithTheAttributeAndC
9090
MyWebApi
9191
.Controller<AttributesController>()
9292
.ShouldHave()
93-
.Attributes(attributes => attributes.ChangingRouteTo("/api/test", withOrder: 1));
93+
.Attributes(attributes => attributes.ChangingRouteTo("api/test", withOrder: 1));
9494
}
9595

9696
[Test]
@@ -102,7 +102,7 @@ public void ChangingRouteToShouldThrowExceptionWithActionWithTheAttributeAndWron
102102
MyWebApi
103103
.Controller<AttributesController>()
104104
.ShouldHave()
105-
.Attributes(attributes => attributes.ChangingRouteTo("/api/test", withOrder: 2));
105+
.Attributes(attributes => attributes.ChangingRouteTo("api/test", withOrder: 2));
106106
}
107107

108108
[Test]
@@ -114,7 +114,7 @@ public void ChangingRouteToShouldThrowExceptionWithActionWithoutTheAttribute()
114114
MyWebApi
115115
.Controller<WebApiController>()
116116
.ShouldHave()
117-
.Attributes(attributes => attributes.ChangingRouteTo("/api/test"));
117+
.Attributes(attributes => attributes.ChangingRouteTo("api/test"));
118118
}
119119

120120
[Test]
@@ -253,7 +253,7 @@ public void AndAlsoShouldWorkCorrectly()
253253
=> attributes
254254
.AllowingAnonymousRequests()
255255
.AndAlso()
256-
.ChangingRouteTo("/api/test"));
256+
.ChangingRouteTo("api/test"));
257257
}
258258
}
259259
}

src/MyWebApi.Tests/MyWebApi.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@
168168
<Compile Include="UtilitiesTests\ValidatorsTests\MediaTypeFormatterValidatorTests.cs" />
169169
<Compile Include="UtilitiesTests\ValidatorsTests\RuntimeBinderValidatorTests.cs" />
170170
<Compile Include="UtilitiesTests\ValidatorsTests\VersionValidatorTests.cs" />
171+
<Compile Include="WithTests.cs" />
171172
</ItemGroup>
172173
<ItemGroup>
173174
<None Include="app.config" />

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ namespace MyTested.WebApi.Tests.Setups.Controllers
77
using System.Web.Http;
88

99
[AllowAnonymous]
10-
[Route("/api/test", Name = "TestRoute", Order = 1)]
10+
[Route("api/test", Name = "TestRouteAttributes", Order = 1)]
1111
public class AttributesController : ApiController
1212
{
13+
[AllowAnonymous]
14+
public IHttpActionResult WithAttributesAndParameters(int id)
15+
{
16+
return this.Ok(id);
17+
}
1318
}
1419
}

src/MyWebApi.Tests/WithTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// MyWebApi - ASP.NET Web API Fluent Testing Framework
2+
// Copyright (C) 2015 Ivaylo Kenov.
3+
//
4+
// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL).
5+
6+
namespace MyTested.WebApi.Tests
7+
{
8+
using NUnit.Framework;
9+
using Setups.Controllers;
10+
11+
[TestFixture]
12+
public class WithTests
13+
{
14+
[Test]
15+
public void WithAnyShouldWorkCorrectlyWhereTheValueOfActionParametersAreNotImportant()
16+
{
17+
MyWebApi
18+
.Controller<AttributesController>()
19+
.Calling(c => c.WithAttributesAndParameters(With.Any<int>()))
20+
.ShouldHave()
21+
.ActionAttributes(attrs => attrs.AllowingAnonymousRequests());
22+
}
23+
}
24+
}

src/MyWebApi/Common/Servers/OwinTestServer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace MyTested.WebApi.Common.Servers
1414
public static class OwinTestServer
1515
{
1616
/// <summary>
17-
/// Default host on which the OWIN server will listen - http://localhost.
17+
/// Default host on which the OWIN server will listen - local host.
1818
/// </summary>
1919
public const string DefaultHost = "http://localhost";
2020

src/MyWebApi/MyWebApi.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@
279279
<Compile Include="Utilities\Validators\RouteValidator.cs" />
280280
<Compile Include="Utilities\Validators\RuntimeBinderValidator.cs" />
281281
<Compile Include="Utilities\Validators\VersionValidator.cs" />
282+
<Compile Include="With.cs" />
282283
</ItemGroup>
283284
<ItemGroup>
284285
<None Include="MyWebApi.licenseheader" />

src/MyWebApi/With.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// MyWebApi - ASP.NET Web API Fluent Testing Framework
2+
// Copyright (C) 2015 Ivaylo Kenov.
3+
//
4+
// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL).
5+
6+
namespace MyTested.WebApi
7+
{
8+
/// <summary>
9+
/// Provides easier parameter selection in lambda expression where the value of the parameter does not matter.
10+
/// </summary>
11+
public class With
12+
{
13+
/// <summary>
14+
/// Provides any parameter to lambda expression where the value of the parameter does not matter.
15+
/// </summary>
16+
/// <typeparam name="TParameter">Type of parameter.</typeparam>
17+
/// <returns>Default value of TParameter.</returns>
18+
public static TParameter Any<TParameter>()
19+
{
20+
return default(TParameter);
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)