diff --git a/test/MusicStore.Test/CartSummaryComponentTest.cs b/test/MusicStore.Test/CartSummaryComponentTest.cs index f64983ad19..f2be34e695 100644 --- a/test/MusicStore.Test/CartSummaryComponentTest.cs +++ b/test/MusicStore.Test/CartSummaryComponentTest.cs @@ -5,11 +5,8 @@ using Microsoft.AspNet.Http; using Microsoft.AspNet.Http.Features; using Microsoft.AspNet.Http.Internal; using Microsoft.AspNet.Mvc; -using Microsoft.AspNet.Session; -using Microsoft.Framework.Caching.Distributed; -using Microsoft.Framework.Caching.Memory; using Microsoft.Framework.DependencyInjection; -using Microsoft.Framework.Logging.Testing; +using MusicStore.Controllers; using MusicStore.Models; using Xunit; @@ -42,11 +39,7 @@ namespace MusicStore.Components // Session initialization var cartId = "CartId_A"; - var sessionFeature = new SessionFeature() - { - Session = CreateTestSession(), - }; - viewContext.HttpContext.SetFeature(sessionFeature); + viewContext.HttpContext.SetFeature(new TestSessionFeature()); viewContext.HttpContext.Session.SetString("Session", cartId); // DbContext initialization @@ -71,17 +64,6 @@ namespace MusicStore.Components Assert.Equal("AlbumA", cartSummaryComponent.ViewBag.CartSummary); } - private static ISession CreateTestSession() - { - return new DistributedSession( - new LocalCache(new MemoryCache(new MemoryCacheOptions())), - "sessionId_A", - idleTimeout: TimeSpan.MaxValue, - tryEstablishSession: () => true, - loggerFactory: new NullLoggerFactory(), - isNewSessionKey: true); - } - private static void PopulateData(MusicStoreContext context, string cartId, string albumTitle, int itemCount) { var album = new Album() diff --git a/test/MusicStore.Test/CheckoutControllerTest.cs b/test/MusicStore.Test/CheckoutControllerTest.cs index d05c0e5052..a0af8edb6c 100644 --- a/test/MusicStore.Test/CheckoutControllerTest.cs +++ b/test/MusicStore.Test/CheckoutControllerTest.cs @@ -8,11 +8,7 @@ using Microsoft.AspNet.Http; using Microsoft.AspNet.Http.Features; using Microsoft.AspNet.Http.Internal; using Microsoft.AspNet.Mvc; -using Microsoft.AspNet.Session; -using Microsoft.Framework.Caching.Distributed; -using Microsoft.Framework.Caching.Memory; using Microsoft.Framework.DependencyInjection; -using Microsoft.Framework.Logging.Testing; using MusicStore.Models; using Xunit; @@ -62,11 +58,7 @@ namespace MusicStore.Controllers // Session initialization var cartId = "CartId_A"; - var sessionFeature = new SessionFeature() - { - Session = CreateTestSession(), - }; - httpContext.SetFeature(sessionFeature); + httpContext.SetFeature(new TestSessionFeature()); httpContext.Session.SetString("Session", cartId); // FormCollection initialization @@ -241,17 +233,6 @@ namespace MusicStore.Controllers Assert.Equal("Error", viewResult.ViewName); } - private static ISession CreateTestSession() - { - return new DistributedSession( - new LocalCache(new MemoryCache(new MemoryCacheOptions())), - "sessionId_A", - idleTimeout: TimeSpan.MaxValue, - tryEstablishSession: () => true, - loggerFactory: new NullLoggerFactory(), - isNewSessionKey: true); - } - private static CartItem[] CreateTestCartItems(string cartId, decimal itemPrice, int numberOfItem) { var albums = Enumerable.Range(1, 10).Select(n => diff --git a/test/MusicStore.Test/ShoppingCartControllerTest.cs b/test/MusicStore.Test/ShoppingCartControllerTest.cs index abe1ed7625..bbaebe6152 100644 --- a/test/MusicStore.Test/ShoppingCartControllerTest.cs +++ b/test/MusicStore.Test/ShoppingCartControllerTest.cs @@ -41,13 +41,8 @@ namespace MusicStore.Controllers public async Task Index_ReturnsNoCartItems_WhenSessionEmpty() { // Arrange - var sessionFeature = new SessionFeature() - { - Session = CreateTestSession(), - }; - var httpContext = new DefaultHttpContext(); - httpContext.SetFeature(sessionFeature); + httpContext.SetFeature(new TestSessionFeature()); var controller = new ShoppingCartController() { @@ -72,13 +67,8 @@ namespace MusicStore.Controllers public async Task Index_ReturnsNoCartItems_WhenNoItemsInCart() { // Arrange - var sessionFeature = new SessionFeature() - { - Session = CreateTestSession(), - }; - var httpContext = new DefaultHttpContext(); - httpContext.SetFeature(sessionFeature); + httpContext.SetFeature(new TestSessionFeature()); httpContext.Session.SetString("Session", "CartId_A"); var controller = new ShoppingCartController() @@ -105,13 +95,8 @@ namespace MusicStore.Controllers { // Arrange var cartId = "CartId_A"; - var sessionFeature = new SessionFeature() - { - Session = CreateTestSession(), - }; - var httpContext = new DefaultHttpContext(); - httpContext.SetFeature(sessionFeature); + httpContext.SetFeature(new TestSessionFeature()); httpContext.Session.SetString("Session", cartId); var dbContext = _serviceProvider.GetRequiredService(); @@ -147,13 +132,8 @@ namespace MusicStore.Controllers { // Arrange var albumId = 3; - var sessionFeature = new SessionFeature() - { - Session = CreateTestSession(), - }; - var httpContext = new DefaultHttpContext(); - httpContext.SetFeature(sessionFeature); + httpContext.SetFeature(new TestSessionFeature()); httpContext.Session.SetString("Session", "CartId_A"); // Creates the albums of AlbumId = 1 ~ 10. @@ -192,11 +172,7 @@ namespace MusicStore.Controllers var httpContext = new DefaultHttpContext(); // Session and cart initialization - var sessionFeature = new SessionFeature() - { - Session = CreateTestSession(), - }; - httpContext.SetFeature(sessionFeature); + httpContext.SetFeature(new TestSessionFeature()); httpContext.Session.SetString("Session", cartId); // DbContext initialization @@ -243,18 +219,6 @@ namespace MusicStore.Controllers Assert.False((await cart.GetCartItems()).Any(c => c.CartItemId == cartItemId)); } - private static ISession CreateTestSession() - { - - return new DistributedSession( - new LocalCache(new MemoryCache(new MemoryCacheOptions())), - "sessionId_A", - idleTimeout: TimeSpan.MaxValue, - tryEstablishSession: () => true, - loggerFactory: new NullLoggerFactory(), - isNewSessionKey: true); - } - private static CartItem[] CreateTestCartItems(string cartId, decimal itemPrice, int numberOfItem) { var albums = CreateTestAlbums(itemPrice); diff --git a/test/MusicStore.Test/TestSessionFeature.cs b/test/MusicStore.Test/TestSessionFeature.cs new file mode 100644 index 0000000000..74e0b0d7da --- /dev/null +++ b/test/MusicStore.Test/TestSessionFeature.cs @@ -0,0 +1,26 @@ +using System; +using Microsoft.AspNet.Http.Features; +using Microsoft.AspNet.Session; +using Microsoft.Framework.Caching.Distributed; +using Microsoft.Framework.Caching.Memory; +using Microsoft.Framework.Logging.Testing; + +namespace MusicStore.Controllers +{ + public class TestSessionFeature : ISessionFeature + { + public ISession Session + { + get + { + return new DistributedSession( + new LocalCache(new MemoryCache(new MemoryCacheOptions())), + "sessionId_A", + idleTimeout: TimeSpan.MaxValue, + tryEstablishSession: () => true, + loggerFactory: new NullLoggerFactory(), + isNewSessionKey: true); + } + } + } +}