Music Store sample Unit Tests - (except usermanager and signinmanager)
This commit is contained in:
parent
47b34cd1da
commit
1b502cbee7
|
|
@ -58,7 +58,7 @@ namespace MusicStore.Controllers
|
|||
public async Task<IActionResult> RemoveFromCart(int id, CancellationToken requestAborted)
|
||||
{
|
||||
var cookieToken = string.Empty;
|
||||
string formToken = string.Empty;
|
||||
var formToken = string.Empty;
|
||||
string[] tokenHeaders = null;
|
||||
string[] tokens = null;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,17 +1,22 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.AspNet.Http.Core;
|
||||
using Microsoft.AspNet.Http.Core.Collections;
|
||||
using Microsoft.AspNet.Mvc;
|
||||
using Microsoft.AspNet.Routing;
|
||||
using Microsoft.AspNet.Session;
|
||||
using Microsoft.AspNet.Testing.Logging;
|
||||
using Microsoft.Framework.Caching.Distributed;
|
||||
using Microsoft.Framework.Caching.Memory;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using MusicStore.Models;
|
||||
using Xunit;
|
||||
|
||||
namespace MusicStore.Controllers
|
||||
namespace MusicStore.Controllers
|
||||
{
|
||||
public class CheckoutControllerTest
|
||||
{
|
||||
|
|
@ -42,6 +47,66 @@ namespace MusicStore.Controllers
|
|||
Assert.Null(viewResult.ViewName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AddressAndPayment_RedirectToCompleteWhenSuccessful()
|
||||
{
|
||||
// Arrange
|
||||
var httpContext = new DefaultHttpContext();
|
||||
|
||||
var orderId = 10;
|
||||
var order = new Order()
|
||||
{
|
||||
OrderId = orderId,
|
||||
};
|
||||
|
||||
// Session initialization
|
||||
var cartId = "CartId_A";
|
||||
var sessionFeature = new SessionFeature()
|
||||
{
|
||||
Session = CreateTestSession(),
|
||||
};
|
||||
httpContext.SetFeature<ISessionFeature>(sessionFeature);
|
||||
httpContext.Session.SetString("Session", cartId);
|
||||
|
||||
// FormCollection initialization
|
||||
httpContext.Request.Form =
|
||||
new FormCollection(
|
||||
new Dictionary<string, string[]>()
|
||||
{ { "PromoCode", new string[] { "FREE" } } }
|
||||
);
|
||||
|
||||
// UserName initialization
|
||||
var claims = new List<Claim> { new Claim(ClaimTypes.Name, "TestUserName") };
|
||||
httpContext.User = new ClaimsPrincipal(new ClaimsIdentity(claims));
|
||||
|
||||
// DbContext initialization
|
||||
var dbContext = _serviceProvider.GetRequiredService<MusicStoreContext>();
|
||||
var cartItems = CreateTestCartItems(
|
||||
cartId,
|
||||
itemPrice: 10,
|
||||
numberOfItem: 1);
|
||||
dbContext.AddRange(cartItems.Select(n => n.Album).Distinct());
|
||||
dbContext.AddRange(cartItems);
|
||||
dbContext.SaveChanges();
|
||||
|
||||
var controller = new CheckoutController()
|
||||
{
|
||||
DbContext = dbContext,
|
||||
};
|
||||
controller.ActionContext.HttpContext = httpContext;
|
||||
|
||||
// Act
|
||||
var result = await controller.AddressAndPayment(order, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
var redirectResult = Assert.IsType<RedirectToActionResult>(result);
|
||||
Assert.Equal("Complete", redirectResult.ActionName);
|
||||
Assert.Null(redirectResult.ControllerName);
|
||||
Assert.NotNull(redirectResult.RouteValues);
|
||||
|
||||
Assert.Equal(orderId, redirectResult.RouteValues["Id"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AddressAndPayment_ReturnsOrderIfInvalidPromoCode()
|
||||
{
|
||||
|
|
@ -174,5 +239,37 @@ 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 =>
|
||||
new Album()
|
||||
{
|
||||
AlbumId = n,
|
||||
Price = itemPrice,
|
||||
}).ToArray();
|
||||
|
||||
var cartItems = Enumerable.Range(1, numberOfItem).Select(n =>
|
||||
new CartItem()
|
||||
{
|
||||
Count = 1,
|
||||
CartId = cartId,
|
||||
AlbumId = n % 10,
|
||||
Album = albums[n % 10],
|
||||
}).ToArray();
|
||||
|
||||
return cartItems;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -3,7 +3,6 @@ using System.Collections.Generic;
|
|||
using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Hosting;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.AspNet.Http.Authentication;
|
||||
using Microsoft.AspNet.Http.Core;
|
||||
using Microsoft.AspNet.Http.Core.Authentication;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.AspNet.Http.Core;
|
||||
|
|
@ -27,6 +29,8 @@ namespace MusicStore.Controllers
|
|||
.AddInMemoryStore()
|
||||
.AddDbContext<MusicStoreContext>();
|
||||
|
||||
services.AddMvc();
|
||||
|
||||
_serviceProvider = services.BuildServiceProvider();
|
||||
}
|
||||
|
||||
|
|
@ -135,6 +139,107 @@ namespace MusicStore.Controllers
|
|||
Assert.Equal(5 * 10, model.CartTotal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AddToCart_AddsItemToCart()
|
||||
{
|
||||
// Arrange
|
||||
var albumId = 3;
|
||||
var sessionFeature = new SessionFeature()
|
||||
{
|
||||
Session = CreateTestSession(),
|
||||
};
|
||||
|
||||
var httpContext = new DefaultHttpContext();
|
||||
httpContext.SetFeature<ISessionFeature>(sessionFeature);
|
||||
httpContext.Session.SetString("Session", "CartId_A");
|
||||
|
||||
// Creates the albums of AlbumId = 1 ~ 10.
|
||||
var dbContext = _serviceProvider.GetRequiredService<MusicStoreContext>();
|
||||
var albums = CreateTestAlbums(itemPrice: 10);
|
||||
dbContext.AddRange(albums);
|
||||
dbContext.SaveChanges();
|
||||
|
||||
var controller = new ShoppingCartController()
|
||||
{
|
||||
DbContext = dbContext
|
||||
};
|
||||
controller.ActionContext.HttpContext = httpContext;
|
||||
|
||||
// Act
|
||||
var result = await controller.AddToCart(albumId, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
var cart = ShoppingCart.GetCart(dbContext, httpContext);
|
||||
Assert.Equal(1, (await cart.GetCartItems()).Count);
|
||||
Assert.Equal(albumId, (await cart.GetCartItems()).Single().AlbumId);
|
||||
|
||||
var redirectResult = Assert.IsType<RedirectToActionResult>(result);
|
||||
Assert.Null(redirectResult.ControllerName);
|
||||
Assert.Equal("Index", redirectResult.ActionName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RemoveFromCart_RemovesItemFromCart()
|
||||
{
|
||||
// Arrange
|
||||
var cartId = "CartId_A";
|
||||
var cartItemId = 3;
|
||||
var numberOfItem = 5;
|
||||
var unitPrice = 10;
|
||||
var httpContext = new DefaultHttpContext();
|
||||
|
||||
// Session and cart initialization
|
||||
var sessionFeature = new SessionFeature()
|
||||
{
|
||||
Session = CreateTestSession(),
|
||||
};
|
||||
httpContext.SetFeature<ISessionFeature>(sessionFeature);
|
||||
httpContext.Session.SetString("Session", cartId);
|
||||
|
||||
// DbContext initialization
|
||||
var dbContext = _serviceProvider.GetRequiredService<MusicStoreContext>();
|
||||
var cartItems = CreateTestCartItems(cartId, unitPrice, numberOfItem);
|
||||
dbContext.AddRange(cartItems.Select(n => n.Album).Distinct());
|
||||
dbContext.AddRange(cartItems);
|
||||
dbContext.SaveChanges();
|
||||
|
||||
// ServiceProvder initialization
|
||||
var serviceProviderFeature = new ServiceProvidersFeature();
|
||||
httpContext.SetFeature<IServiceProvidersFeature>(serviceProviderFeature);
|
||||
|
||||
// AntiForgery initialization
|
||||
serviceProviderFeature.RequestServices = _serviceProvider;
|
||||
var antiForgery = serviceProviderFeature.RequestServices.GetRequiredService<AntiForgery>();
|
||||
var tokens = antiForgery.GetTokens(httpContext, "testToken");
|
||||
|
||||
// Header initialization for AntiForgery
|
||||
var headers = new KeyValuePair<string, string[]>(
|
||||
"RequestVerificationToken",
|
||||
new string[] { tokens.CookieToken + ":" + tokens.FormToken });
|
||||
httpContext.Request.Headers.Add(headers);
|
||||
|
||||
// Cotroller initialization
|
||||
var controller = new ShoppingCartController()
|
||||
{
|
||||
DbContext = dbContext,
|
||||
AntiForgery = antiForgery,
|
||||
};
|
||||
controller.ActionContext.HttpContext = httpContext;
|
||||
|
||||
// Act
|
||||
var result = await controller.RemoveFromCart(cartItemId, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
var jsonResult = Assert.IsType<JsonResult>(result);
|
||||
var viewModel = Assert.IsType<ShoppingCartRemoveViewModel>(jsonResult.Value);
|
||||
Assert.Equal(numberOfItem - 1, viewModel.CartCount);
|
||||
Assert.Equal((numberOfItem - 1) * 10, viewModel.CartTotal);
|
||||
Assert.Equal(" has been removed from your shopping cart.", viewModel.Message);
|
||||
|
||||
var cart = ShoppingCart.GetCart(dbContext, httpContext);
|
||||
Assert.False((await cart.GetCartItems()).Any(c => c.CartItemId == cartItemId));
|
||||
}
|
||||
|
||||
private static ISession CreateTestSession()
|
||||
{
|
||||
|
||||
|
|
@ -149,23 +254,28 @@ namespace MusicStore.Controllers
|
|||
|
||||
private static CartItem[] CreateTestCartItems(string cartId, decimal itemPrice, int numberOfItem)
|
||||
{
|
||||
var albums = Enumerable.Range(1, 10).Select(n =>
|
||||
new Album()
|
||||
{
|
||||
AlbumId = n,
|
||||
Price = itemPrice,
|
||||
}).ToArray();
|
||||
var albums = CreateTestAlbums(itemPrice);
|
||||
|
||||
var cartItems = Enumerable.Range(1, numberOfItem).Select(n =>
|
||||
new CartItem()
|
||||
{
|
||||
Count = 1,
|
||||
CartId = cartId,
|
||||
AlbumId = n,
|
||||
Album = albums[n - 1],
|
||||
AlbumId = n % albums.Length,
|
||||
Album = albums[n % albums.Length],
|
||||
}).ToArray();
|
||||
|
||||
return cartItems;
|
||||
}
|
||||
|
||||
private static Album[] CreateTestAlbums(decimal itemPrice)
|
||||
{
|
||||
return Enumerable.Range(1, 10).Select(n =>
|
||||
new Album()
|
||||
{
|
||||
AlbumId = n,
|
||||
Price = itemPrice,
|
||||
}).ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,175 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Mvc;
|
||||
using Microsoft.Data.Entity;
|
||||
using Microsoft.Framework.Caching.Memory;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using MusicStore.Models;
|
||||
using Xunit;
|
||||
|
||||
namespace MusicStore.Controllers
|
||||
{
|
||||
public class StoreControllerTest
|
||||
{
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
public StoreControllerTest()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
|
||||
services.AddEntityFramework()
|
||||
.AddInMemoryStore()
|
||||
.AddDbContext<MusicStoreContext>();
|
||||
|
||||
_serviceProvider = services.BuildServiceProvider();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Index_CreatesViewWithGenres()
|
||||
{
|
||||
// Arrange
|
||||
var dbContext = _serviceProvider.GetRequiredService<MusicStoreContext>();
|
||||
CreateTestGenres(numberOfGenres: 10, numberOfAlbums: 1, dbContext: dbContext);
|
||||
|
||||
var controller = new StoreController()
|
||||
{
|
||||
DbContext = dbContext,
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = await controller.Index();
|
||||
|
||||
// Assert
|
||||
var viewResult = Assert.IsType<ViewResult>(result);
|
||||
Assert.Null(viewResult.ViewName);
|
||||
|
||||
Assert.NotNull(viewResult.ViewData);
|
||||
var viewModel = Assert.IsType<List<Genre>>(viewResult.ViewData.Model);
|
||||
Assert.Equal(10, viewModel.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Browse_ReturnsHttpNotFoundWhenNoGenreData()
|
||||
{
|
||||
// Arrange
|
||||
var controller = new StoreController()
|
||||
{
|
||||
DbContext = _serviceProvider.GetRequiredService<MusicStoreContext>(),
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = await controller.Browse(string.Empty);
|
||||
|
||||
// Assert
|
||||
Assert.IsType<HttpNotFoundResult>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Browse_ReturnsViewWithGenre()
|
||||
{
|
||||
// Arrange
|
||||
var genreName = "Genre 1";
|
||||
|
||||
var dbContext = _serviceProvider.GetRequiredService<MusicStoreContext>();
|
||||
CreateTestGenres(numberOfGenres: 3, numberOfAlbums: 3, dbContext: dbContext);
|
||||
|
||||
var controller = new StoreController()
|
||||
{
|
||||
DbContext = dbContext,
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = await controller.Browse(genreName);
|
||||
|
||||
// Assert
|
||||
var viewResult = Assert.IsType<ViewResult>(result);
|
||||
Assert.Null(viewResult.ViewName);
|
||||
|
||||
Assert.NotNull(viewResult.ViewData);
|
||||
var viewModel = Assert.IsType<Genre>(viewResult.ViewData.Model);
|
||||
Assert.Equal(genreName, viewModel.Name);
|
||||
Assert.NotNull(viewModel.Albums);
|
||||
Assert.Equal(3, viewModel.Albums.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Details_ReturnsHttpNotFoundWhenNoAlbumData()
|
||||
{
|
||||
// Arrange
|
||||
var albumId = int.MinValue;
|
||||
var controller = new StoreController()
|
||||
{
|
||||
DbContext = _serviceProvider.GetRequiredService<MusicStoreContext>(),
|
||||
Cache = _serviceProvider.GetRequiredService<IMemoryCache>(),
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = await controller.Details(albumId);
|
||||
|
||||
// Assert
|
||||
Assert.IsType<HttpNotFoundResult>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Details_ReturnsAlbumDetail()
|
||||
{
|
||||
// Arrange
|
||||
var albumId = 1;
|
||||
|
||||
var dbContext = _serviceProvider.GetRequiredService<MusicStoreContext>();
|
||||
var genres = CreateTestGenres(numberOfGenres: 3, numberOfAlbums: 3, dbContext: dbContext);
|
||||
|
||||
var cache = _serviceProvider.GetRequiredService<IMemoryCache>();
|
||||
|
||||
var controller = new StoreController()
|
||||
{
|
||||
DbContext = dbContext,
|
||||
Cache = cache,
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = await controller.Details(albumId);
|
||||
|
||||
// Assert
|
||||
var viewResult = Assert.IsType<ViewResult>(result);
|
||||
Assert.Null(viewResult.ViewName);
|
||||
|
||||
Assert.NotNull(viewResult.ViewData);
|
||||
var viewModel = Assert.IsType<Album>(viewResult.ViewData.Model);
|
||||
Assert.NotNull(viewModel.Genre);
|
||||
var genre = genres.SingleOrDefault(g => g.GenreId == viewModel.GenreId);
|
||||
Assert.NotNull(genre);
|
||||
Assert.NotNull(genre.Albums.SingleOrDefault(a => a.AlbumId == albumId));
|
||||
Assert.Null(viewModel.Artist);
|
||||
|
||||
var cachedAlbum = cache.Get<Task<Album>>("album_1");
|
||||
Assert.NotNull(cachedAlbum);
|
||||
Assert.Equal(albumId, cachedAlbum.Result.AlbumId);
|
||||
}
|
||||
|
||||
private static Genre[] CreateTestGenres(int numberOfGenres, int numberOfAlbums, DbContext dbContext)
|
||||
{
|
||||
var albums = Enumerable.Range(1, numberOfAlbums * numberOfGenres).Select(n =>
|
||||
new Album()
|
||||
{
|
||||
AlbumId = n,
|
||||
}).ToList();
|
||||
|
||||
var generes = Enumerable.Range(1, numberOfGenres).Select(n =>
|
||||
new Genre()
|
||||
{
|
||||
Albums = albums.Where(i => i.AlbumId % numberOfGenres == n - 1).ToList(),
|
||||
GenreId = n,
|
||||
Name = "Genre " + n,
|
||||
});
|
||||
|
||||
dbContext.AddRange(albums);
|
||||
dbContext.AddRange(generes);
|
||||
dbContext.SaveChanges();
|
||||
|
||||
return generes.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue