Skip to content

Commit f80837d

Browse files
committed
Added route tests to the sample (#132)
1 parent 2728dd1 commit f80837d

File tree

15 files changed

+316
-13
lines changed

15 files changed

+316
-13
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
namespace MusicStore.Test.Routes
2+
{
3+
using System.Threading;
4+
using Models;
5+
using MusicStore.Controllers;
6+
using MyTested.AspNetCore.Mvc;
7+
using Xunit;
8+
9+
public class CheckoutRouteTest
10+
{
11+
[Fact]
12+
public void GetAddressAndPaymentShouldBeRoutedCorrectly()
13+
{
14+
MyMvc
15+
.Routes()
16+
.ShouldMap("/Checkout/AddressAndPayment")
17+
.To<CheckoutController>(c => c.AddressAndPayment());
18+
}
19+
20+
[Fact]
21+
public void PostAddressAndPaymentShouldBeRoutedCorrectly()
22+
{
23+
var firstName = "FirstNameTest";
24+
var lastName = "LastNameTest";
25+
var address = "AddressTest";
26+
var city = "CityTest";
27+
var state = "StateTest";
28+
var postalCode = "PostalTest";
29+
var country = "CountryTest";
30+
var phone = "PhoneTest";
31+
var email = "[email protected]";
32+
33+
MyMvc
34+
.Routes()
35+
.ShouldMap(request => request
36+
.WithMethod(HttpMethod.Post)
37+
.WithLocation("/Checkout/AddressAndPayment")
38+
.WithFormFields(new
39+
{
40+
FirstName = firstName,
41+
LastName = lastName,
42+
Address = address,
43+
City = city,
44+
State = state,
45+
PostalCode = postalCode,
46+
Country = country,
47+
Phone = phone,
48+
Email = email,
49+
})
50+
.WithAntiForgeryToken())
51+
.To<CheckoutController>(c => c.AddressAndPayment(
52+
With.Any<MusicStoreContext>(),
53+
new Order
54+
{
55+
FirstName = firstName,
56+
LastName = lastName,
57+
Address = address,
58+
City = city,
59+
State = state,
60+
PostalCode = postalCode,
61+
Country = country,
62+
Phone = phone,
63+
Email = email
64+
},
65+
With.Any<CancellationToken>()))
66+
.AndAlso()
67+
.ToValidModelState();
68+
}
69+
70+
[Fact]
71+
public void GetCompleteShouldBeRoutedCorrectly()
72+
{
73+
MyMvc
74+
.Routes()
75+
.ShouldMap("/Checkout/Complete/1")
76+
.To<CheckoutController>(c => c.Complete(With.Any<MusicStoreContext>(), 1));
77+
}
78+
}
79+
}

samples/MusicStore/MusicStore.Test/Routes/HomeRouteTest.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
public class HomeRouteTest
1010
{
1111
[Fact]
12-
public void IndexShouldBeRoutedCorrectly()
12+
public void GetIndexShouldBeRoutedCorrectly()
1313
{
1414
MyMvc
1515
.Routes()
@@ -20,12 +20,30 @@ public void IndexShouldBeRoutedCorrectly()
2020
}
2121

2222
[Fact]
23-
public void ErrorShouldBeRoutedCorrectly()
23+
public void GetErrorShouldBeRoutedCorrectly()
2424
{
2525
MyMvc
2626
.Routes()
2727
.ShouldMap("/Home/Error")
2828
.To<HomeController>(c => c.Error());
2929
}
30+
31+
[Fact]
32+
public void GetStatusCodeShouldBeRoutedCorrectly()
33+
{
34+
MyMvc
35+
.Routes()
36+
.ShouldMap("/Home/StatusCodePage")
37+
.To<HomeController>(c => c.StatusCodePage());
38+
}
39+
40+
[Fact]
41+
public void GetAccessDeniedShouldBeRoutedCorrectly()
42+
{
43+
MyMvc
44+
.Routes()
45+
.ShouldMap("/Home/AccessDenied")
46+
.To<HomeController>(c => c.AccessDenied());
47+
}
3048
}
3149
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
namespace MusicStore.Test.Routes
2+
{
3+
using MusicStore.Controllers;
4+
using MyTested.AspNetCore.Mvc;
5+
using System.Threading;
6+
using Xunit;
7+
8+
public class ShoppingCartRouteTest
9+
{
10+
[Fact]
11+
public void IndexShouldBeRoutedCorrectly()
12+
{
13+
MyMvc
14+
.Routes()
15+
.ShouldMap("/ShoppingCart/Index")
16+
.To<ShoppingCartController>(c => c.Index());
17+
}
18+
19+
[Fact]
20+
public void AddToCartShouldBeRoutedCorrectly()
21+
{
22+
MyMvc
23+
.Routes()
24+
.ShouldMap("/ShoppingCart/AddToCart/1")
25+
.To<ShoppingCartController>(c => c.AddToCart(1, With.Any<CancellationToken>()));
26+
}
27+
28+
[Fact]
29+
public void RemoveFromCartShouldBeRoutedCorrectly()
30+
{
31+
MyMvc
32+
.Routes()
33+
.ShouldMap(request => request
34+
.WithMethod(HttpMethod.Post)
35+
.WithLocation("/ShoppingCart/RemoveFromCart/1")
36+
.WithAntiForgeryToken())
37+
.To<ShoppingCartController>(c => c.RemoveFromCart(1, With.Any<CancellationToken>()));
38+
}
39+
}
40+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
namespace MusicStore.Test.Routes
2+
{
3+
using Microsoft.Extensions.Caching.Memory;
4+
using MusicStore.Controllers;
5+
using MyTested.AspNetCore.Mvc;
6+
using Xunit;
7+
8+
public class StoreRouteTests
9+
{
10+
[Fact]
11+
public void IndexShouldBeRoutedCorrectly()
12+
{
13+
MyMvc
14+
.Routes()
15+
.ShouldMap("/Store/Index")
16+
.To<StoreController>(c => c.Index());
17+
}
18+
19+
[Fact]
20+
public void BrowseShouldBeRoutedCorrectly()
21+
{
22+
MyMvc
23+
.Routes()
24+
.ShouldMap("/Store/Browse?genre=Disco")
25+
.To<StoreController>(c => c.Browse("Disco"));
26+
}
27+
28+
[Fact]
29+
public void DetailsShouldBeRoutedCorrectly()
30+
{
31+
MyMvc
32+
.Routes()
33+
.ShouldMap("/Store/Details/1")
34+
.To<StoreController>(c => c.Details(With.Any<IMemoryCache>(), 1)); ;
35+
}
36+
}
37+
}

src/MyTested.AspNetCore.Mvc.Core/Builders/Controllers/ControllerHttpBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public IAndControllerBuilder<TController> WithHttpRequest(HttpRequest httpReques
4343
/// <inheritdoc />
4444
public IAndControllerBuilder<TController> WithHttpRequest(Action<IHttpRequestBuilder> httpRequestBuilder)
4545
{
46-
var newHttpRequestBuilder = new HttpRequestBuilder();
46+
var newHttpRequestBuilder = new HttpRequestBuilder(this.HttpContext);
4747
httpRequestBuilder(newHttpRequestBuilder);
4848
newHttpRequestBuilder.ApplyTo(this.HttpRequest);
4949
return this;

src/MyTested.AspNetCore.Mvc.Core/Builders/Http/HttpRequestBuilder.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,19 @@ public class HttpRequestBuilder : IAndHttpRequestBuilder
2828
/// <summary>
2929
/// Initializes a new instance of the <see cref="HttpRequestBuilder"/> class.
3030
/// </summary>
31-
public HttpRequestBuilder()
31+
public HttpRequestBuilder(HttpContext httpContext)
3232
{
33-
this.request = new MockedHttpRequest
33+
CommonValidator.CheckForNullReference(httpContext, nameof(HttpContext));
34+
35+
this.request = new MockedHttpRequest(httpContext)
3436
{
3537
Scheme = HttpScheme.Http,
3638
Path = "/"
3739
};
3840
}
3941

42+
public HttpContext HttpContext => this.request.HttpContext;
43+
4044
/// <inheritdoc />
4145
public IAndHttpRequestBuilder WithBody(Stream body)
4246
{

src/MyTested.AspNetCore.Mvc.Core/Builders/Routes/RouteTestBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public IShouldMapTestBuilder ShouldMap(HttpRequest request)
5050
/// <inheritdoc />
5151
public IShouldMapTestBuilder ShouldMap(Action<IHttpRequestBuilder> httpRequestBuilder)
5252
{
53-
var httpBuilder = new HttpRequestBuilder();
53+
var httpBuilder = new HttpRequestBuilder(this.HttpContext);
5454
httpRequestBuilder(httpBuilder);
5555
httpBuilder.ApplyTo(this.HttpContext.Request);
5656
return this.ShouldMap(this.HttpContext);

src/MyTested.AspNetCore.Mvc.Core/From.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ public static class From
1212
/// </summary>
1313
/// <typeparam name="TService">Type of service.</typeparam>
1414
/// <returns>Service implementation.</returns>
15-
public static TService Services<TService>() => TestServiceProvider.GetService<TService>();
15+
public static TService Services<TService>() => TestServiceProvider.GetRequiredService<TService>();
1616
}
1717
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
namespace MyTested.AspNetCore.Mvc.Internal.Http
2+
{
3+
using System.Collections.Generic;
4+
using System.Threading.Tasks;
5+
using Builders.Authentication;
6+
using Microsoft.AspNetCore.Http.Features.Authentication;
7+
using Microsoft.AspNetCore.Mvc.Internal;
8+
9+
public class MockedAuthenticationHandler : IAuthenticationHandler
10+
{
11+
public Task AuthenticateAsync(AuthenticateContext context)
12+
{
13+
context.Authenticated(
14+
ClaimsPrincipalBuilder.DefaultAuthenticated,
15+
new Dictionary<string, string>(),
16+
new Dictionary<string, object>());
17+
18+
return TaskCache.CompletedTask;
19+
}
20+
21+
public Task ChallengeAsync(ChallengeContext context)
22+
{
23+
context.Accept();
24+
return TaskCache.CompletedTask;
25+
}
26+
27+
public void GetDescriptions(DescribeSchemesContext context)
28+
{
29+
// intentionally does nothing
30+
}
31+
32+
public Task SignInAsync(SignInContext context)
33+
{
34+
context.Accept();
35+
return TaskCache.CompletedTask;
36+
}
37+
38+
public Task SignOutAsync(SignOutContext context)
39+
{
40+
context.Accept();
41+
return TaskCache.CompletedTask;
42+
}
43+
}
44+
}

src/MyTested.AspNetCore.Mvc.Core/Internal/Http/MockedHttpRequest.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
using Microsoft.AspNetCore.Http.Features;
99
using Microsoft.AspNetCore.Http.Internal;
1010
using Microsoft.Extensions.Primitives;
11+
using Utilities.Validators;
1112

1213
/// <summary>
1314
/// Mocked HTTP request object.
1415
/// </summary>
1516
public class MockedHttpRequest : HttpRequest
1617
{
18+
private readonly HttpContext httpContext;
1719
private readonly IHeaderDictionary headerDictionary;
1820
private readonly FormFileCollection formFiles;
1921
private readonly Dictionary<string, string> cookieValues;
@@ -25,8 +27,11 @@ public class MockedHttpRequest : HttpRequest
2527
/// <summary>
2628
/// Initializes a new instance of the <see cref="MockedHttpRequest"/> class.
2729
/// </summary>
28-
public MockedHttpRequest()
30+
public MockedHttpRequest(HttpContext httpContext)
2931
{
32+
CommonValidator.CheckForNullReference(httpContext, nameof(HttpContext));
33+
34+
this.httpContext = httpContext;
3035
this.headerDictionary = new HeaderDictionary();
3136
this.formFiles = new FormFileCollection();
3237
this.cookieValues = new Dictionary<string, string>();
@@ -83,10 +88,10 @@ public MockedHttpRequest()
8388
public override HostString Host { get; set; }
8489

8590
/// <summary>
86-
/// Gets the HttpContext for this request. Not used. Always returns null.
91+
/// Gets the <see cref="Microsoft.AspNetCore.Http.HttpContext"/> for this request.
8792
/// </summary>
8893
/// <value>The HTTP request HTTP context.</value>
89-
public override HttpContext HttpContext => null;
94+
public override HttpContext HttpContext => this.httpContext;
9095

9196
/// <summary>
9297
/// Returns true if the Scheme is 'https'.

0 commit comments

Comments
 (0)