Update Specification and other packages to latest version (#582)

* Updating repositories and specification version
Need to fix broken tests

* removing test that would just be testing mocked result now

* Refactored from IAsyncRepository and removed it.
Tests pass.

* Update packages
This commit is contained in:
Steve Smith
2021-10-25 15:13:02 -04:00
committed by GitHub
parent fee2bbce3d
commit 8a45a2c858
39 changed files with 281 additions and 289 deletions

View File

@@ -7,7 +7,7 @@
<ItemGroup>
<PackageReference Include="Ardalis.GuardClauses" Version="3.0.1" />
<PackageReference Include="Ardalis.Specification" Version="4.1.0" />
<PackageReference Include="Ardalis.Specification" Version="5.2.0" />
<PackageReference Include="MediatR" Version="9.0.0" />
<PackageReference Include="System.Security.Claims" Version="4.3.0" />
<PackageReference Include="System.Text.Json" Version="5.0.0" />

View File

@@ -1,21 +0,0 @@
using Ardalis.Specification;
using Microsoft.eShopWeb.ApplicationCore.Entities;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.eShopWeb.ApplicationCore.Interfaces
{
public interface IAsyncRepository<T> where T : BaseEntity, IAggregateRoot
{
Task<T> GetByIdAsync(int id, CancellationToken cancellationToken = default);
Task<IReadOnlyList<T>> ListAllAsync(CancellationToken cancellationToken = default);
Task<IReadOnlyList<T>> ListAsync(ISpecification<T> spec, CancellationToken cancellationToken = default);
Task<T> AddAsync(T entity, CancellationToken cancellationToken = default);
Task UpdateAsync(T entity, CancellationToken cancellationToken = default);
Task DeleteAsync(T entity, CancellationToken cancellationToken = default);
Task<int> CountAsync(ISpecification<T> spec, CancellationToken cancellationToken = default);
Task<T> FirstAsync(ISpecification<T> spec, CancellationToken cancellationToken = default);
Task<T> FirstOrDefaultAsync(ISpecification<T> spec, CancellationToken cancellationToken = default);
}
}

View File

@@ -4,8 +4,8 @@ using System.Threading.Tasks;
namespace Microsoft.eShopWeb.ApplicationCore.Interfaces
{
public interface IOrderRepository : IAsyncRepository<Order>
{
Task<Order> GetByIdWithItemsAsync(int id);
}
//public interface IOrderRepository : IAsyncRepository<Order>
//{
// Task<Order> GetByIdWithItemsAsync(int id);
//}
}

View File

@@ -0,0 +1,8 @@
using Ardalis.Specification;
namespace Microsoft.eShopWeb.ApplicationCore.Interfaces
{
public interface IReadRepository<T> : IReadRepositoryBase<T> where T : class, IAggregateRoot
{
}
}

View File

@@ -0,0 +1,8 @@
using Ardalis.Specification;
namespace Microsoft.eShopWeb.ApplicationCore.Interfaces
{
public interface IRepository<T> : IRepositoryBase<T> where T : class, IAggregateRoot
{
}
}

View File

@@ -9,10 +9,10 @@ namespace Microsoft.eShopWeb.ApplicationCore.Services
{
public class BasketService : IBasketService
{
private readonly IAsyncRepository<Basket> _basketRepository;
private readonly IRepository<Basket> _basketRepository;
private readonly IAppLogger<BasketService> _logger;
public BasketService(IAsyncRepository<Basket> basketRepository,
public BasketService(IRepository<Basket> basketRepository,
IAppLogger<BasketService> logger)
{
_basketRepository = basketRepository;
@@ -22,7 +22,7 @@ namespace Microsoft.eShopWeb.ApplicationCore.Services
public async Task AddItemToBasket(int basketId, int catalogItemId, decimal price, int quantity = 1)
{
var basketSpec = new BasketWithItemsSpecification(basketId);
var basket = await _basketRepository.FirstOrDefaultAsync(basketSpec);
var basket = await _basketRepository.GetBySpecAsync(basketSpec);
Guard.Against.NullBasket(basketId, basket);
basket.AddItem(catalogItemId, price, quantity);
@@ -40,7 +40,7 @@ namespace Microsoft.eShopWeb.ApplicationCore.Services
{
Guard.Against.Null(quantities, nameof(quantities));
var basketSpec = new BasketWithItemsSpecification(basketId);
var basket = await _basketRepository.FirstOrDefaultAsync(basketSpec);
var basket = await _basketRepository.GetBySpecAsync(basketSpec);
Guard.Against.NullBasket(basketId, basket);
foreach (var item in basket.Items)
@@ -60,10 +60,10 @@ namespace Microsoft.eShopWeb.ApplicationCore.Services
Guard.Against.NullOrEmpty(anonymousId, nameof(anonymousId));
Guard.Against.NullOrEmpty(userName, nameof(userName));
var anonymousBasketSpec = new BasketWithItemsSpecification(anonymousId);
var anonymousBasket = await _basketRepository.FirstOrDefaultAsync(anonymousBasketSpec);
var anonymousBasket = await _basketRepository.GetBySpecAsync(anonymousBasketSpec);
if (anonymousBasket == null) return;
var userBasketSpec = new BasketWithItemsSpecification(userName);
var userBasket = await _basketRepository.FirstOrDefaultAsync(userBasketSpec);
var userBasket = await _basketRepository.GetBySpecAsync(userBasketSpec);
if (userBasket == null)
{
userBasket = new Basket(userName);

View File

@@ -11,14 +11,14 @@ namespace Microsoft.eShopWeb.ApplicationCore.Services
{
public class OrderService : IOrderService
{
private readonly IAsyncRepository<Order> _orderRepository;
private readonly IRepository<Order> _orderRepository;
private readonly IUriComposer _uriComposer;
private readonly IAsyncRepository<Basket> _basketRepository;
private readonly IAsyncRepository<CatalogItem> _itemRepository;
private readonly IRepository<Basket> _basketRepository;
private readonly IRepository<CatalogItem> _itemRepository;
public OrderService(IAsyncRepository<Basket> basketRepository,
IAsyncRepository<CatalogItem> itemRepository,
IAsyncRepository<Order> orderRepository,
public OrderService(IRepository<Basket> basketRepository,
IRepository<CatalogItem> itemRepository,
IRepository<Order> orderRepository,
IUriComposer uriComposer)
{
_orderRepository = orderRepository;
@@ -30,7 +30,7 @@ namespace Microsoft.eShopWeb.ApplicationCore.Services
public async Task CreateOrderAsync(int basketId, Address shippingAddress)
{
var basketSpec = new BasketWithItemsSpecification(basketId);
var basket = await _basketRepository.FirstOrDefaultAsync(basketSpec);
var basket = await _basketRepository.GetBySpecAsync(basketSpec);
Guard.Against.NullBasket(basketId, basket);
Guard.Against.EmptyBasketOnCheckout(basket.Items);

View File

@@ -3,7 +3,7 @@ using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
namespace Microsoft.eShopWeb.ApplicationCore.Specifications
{
public sealed class BasketWithItemsSpecification : Specification<Basket>
public sealed class BasketWithItemsSpecification : Specification<Basket>, ISingleResultSpecification
{
public BasketWithItemsSpecification(int basketId)
{

View File

@@ -0,0 +1,16 @@
using Ardalis.Specification;
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
namespace Microsoft.eShopWeb.ApplicationCore.Specifications
{
public class OrderWithItemsByIdSpec : Specification<Order>, ISingleResultSpecification
{
public OrderWithItemsByIdSpec(int orderId)
{
Query
.Where(order => order.Id == orderId)
.Include(o => o.OrderItems)
.ThenInclude(i => i.ItemOrdered);
}
}
}

View File

@@ -11,7 +11,6 @@ namespace BlazorAdmin.Services
private readonly HttpClient _httpClient;
private readonly string _apiUrl;
public HttpService(HttpClient httpClient, BaseUrlConfiguration baseUrlConfiguration)
{
_httpClient = httpClient;

View File

@@ -10,79 +10,85 @@ using System.Threading.Tasks;
namespace Microsoft.eShopWeb.Infrastructure.Data
{
public class EfRepository<T> : RepositoryBase<T>, IReadRepository<T>, IRepository<T> where T : class, IAggregateRoot
{
public EfRepository(CatalogContext dbContext) : base(dbContext)
{
}
}
/// <summary>
/// "There's some repetition here - couldn't we have some the sync methods call the async?"
/// https://blogs.msdn.microsoft.com/pfxteam/2012/04/13/should-i-expose-synchronous-wrappers-for-asynchronous-methods/
/// </summary>
/// <typeparam name="T"></typeparam>
public class EfRepository<T> : IAsyncRepository<T> where T : BaseEntity, IAggregateRoot
{
protected readonly CatalogContext _dbContext;
// public class EfRepository<T> : IAsyncRepository<T> where T : BaseEntity, IAggregateRoot
// {
// protected readonly CatalogContext _dbContext;
public EfRepository(CatalogContext dbContext)
{
_dbContext = dbContext;
}
// public EfRepository(CatalogContext dbContext)
// {
// _dbContext = dbContext;
// }
public virtual async Task<T> GetByIdAsync(int id, CancellationToken cancellationToken = default)
{
var keyValues = new object[] { id };
return await _dbContext.Set<T>().FindAsync(keyValues, cancellationToken);
}
// public virtual async Task<T> GetByIdAsync(int id, CancellationToken cancellationToken = default)
// {
// var keyValues = new object[] { id };
// return await _dbContext.Set<T>().FindAsync(keyValues, cancellationToken);
// }
public async Task<IReadOnlyList<T>> ListAllAsync(CancellationToken cancellationToken = default)
{
return await _dbContext.Set<T>().ToListAsync(cancellationToken);
}
// public async Task<IReadOnlyList<T>> ListAllAsync(CancellationToken cancellationToken = default)
// {
// return await _dbContext.Set<T>().ToListAsync(cancellationToken);
// }
public async Task<IReadOnlyList<T>> ListAsync(ISpecification<T> spec, CancellationToken cancellationToken = default)
{
var specificationResult = ApplySpecification(spec);
return await specificationResult.ToListAsync(cancellationToken);
}
// public async Task<IReadOnlyList<T>> ListAsync(ISpecification<T> spec, CancellationToken cancellationToken = default)
// {
// var specificationResult = ApplySpecification(spec);
// return await specificationResult.ToListAsync(cancellationToken);
// }
public async Task<int> CountAsync(ISpecification<T> spec, CancellationToken cancellationToken = default)
{
var specificationResult = ApplySpecification(spec);
return await specificationResult.CountAsync(cancellationToken);
}
// public async Task<int> CountAsync(ISpecification<T> spec, CancellationToken cancellationToken = default)
// {
// var specificationResult = ApplySpecification(spec);
// return await specificationResult.CountAsync(cancellationToken);
// }
public async Task<T> AddAsync(T entity, CancellationToken cancellationToken = default)
{
await _dbContext.Set<T>().AddAsync(entity, cancellationToken);
await _dbContext.SaveChangesAsync(cancellationToken);
// public async Task<T> AddAsync(T entity, CancellationToken cancellationToken = default)
// {
// await _dbContext.Set<T>().AddAsync(entity, cancellationToken);
// await _dbContext.SaveChangesAsync(cancellationToken);
return entity;
}
// return entity;
// }
public async Task UpdateAsync(T entity, CancellationToken cancellationToken = default)
{
_dbContext.Entry(entity).State = EntityState.Modified;
await _dbContext.SaveChangesAsync(cancellationToken);
}
// public async Task UpdateAsync(T entity, CancellationToken cancellationToken = default)
// {
// _dbContext.Entry(entity).State = EntityState.Modified;
// await _dbContext.SaveChangesAsync(cancellationToken);
// }
public async Task DeleteAsync(T entity, CancellationToken cancellationToken = default)
{
_dbContext.Set<T>().Remove(entity);
await _dbContext.SaveChangesAsync(cancellationToken);
}
// public async Task DeleteAsync(T entity, CancellationToken cancellationToken = default)
// {
// _dbContext.Set<T>().Remove(entity);
// await _dbContext.SaveChangesAsync(cancellationToken);
// }
public async Task<T> FirstAsync(ISpecification<T> spec, CancellationToken cancellationToken = default)
{
var specificationResult = ApplySpecification(spec);
return await specificationResult.FirstAsync(cancellationToken);
}
// public async Task<T> FirstAsync(ISpecification<T> spec, CancellationToken cancellationToken = default)
// {
// var specificationResult = ApplySpecification(spec);
// return await specificationResult.FirstAsync(cancellationToken);
// }
public async Task<T> FirstOrDefaultAsync(ISpecification<T> spec, CancellationToken cancellationToken = default)
{
var specificationResult = ApplySpecification(spec);
return await specificationResult.FirstOrDefaultAsync(cancellationToken);
}
// public async Task<T> FirstOrDefaultAsync(ISpecification<T> spec, CancellationToken cancellationToken = default)
// {
// var specificationResult = ApplySpecification(spec);
// return await specificationResult.FirstOrDefaultAsync(cancellationToken);
// }
private IQueryable<T> ApplySpecification(ISpecification<T> spec)
{
var evaluator = new SpecificationEvaluator<T>();
return evaluator.GetQuery(_dbContext.Set<T>().AsQueryable(), spec);
}
}
// private IQueryable<T> ApplySpecification(ISpecification<T> spec)
// {
// var evaluator = new SpecificationEvaluator<T>();
// return evaluator.GetQuery(_dbContext.Set<T>().AsQueryable(), spec);
// }
// }
}

View File

@@ -5,18 +5,18 @@ using System.Threading.Tasks;
namespace Microsoft.eShopWeb.Infrastructure.Data
{
public class OrderRepository : EfRepository<Order>, IOrderRepository
{
public OrderRepository(CatalogContext dbContext) : base(dbContext)
{
}
//public class OrderRepository : EfRepository<Order>, IOrderRepository
//{
// public OrderRepository(CatalogContext dbContext) : base(dbContext)
// {
// }
public Task<Order> GetByIdWithItemsAsync(int id)
{
return _dbContext.Orders
.Include(o => o.OrderItems)
.ThenInclude(i => i.ItemOrdered)
.FirstOrDefaultAsync(x => x.Id == id);
}
}
// public Task<Order> GetByIdWithItemsAsync(int id)
// {
// return _dbContext.Orders
// .Include(o => o.OrderItems)
// .ThenInclude(i => i.ItemOrdered)
// .FirstOrDefaultAsync(x => x.Id == id);
// }
//}
}

View File

@@ -6,7 +6,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Ardalis.Specification.EntityFrameworkCore" Version="4.1.0" />
<PackageReference Include="Ardalis.Specification.EntityFrameworkCore" Version="5.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.11" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.8.0" />

View File

@@ -14,10 +14,10 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogBrandEndpoints
.WithoutRequest
.WithResponse<ListCatalogBrandsResponse>
{
private readonly IAsyncRepository<CatalogBrand> _catalogBrandRepository;
private readonly IRepository<CatalogBrand> _catalogBrandRepository;
private readonly IMapper _mapper;
public List(IAsyncRepository<CatalogBrand> catalogBrandRepository,
public List(IRepository<CatalogBrand> catalogBrandRepository,
IMapper mapper)
{
_catalogBrandRepository = catalogBrandRepository;
@@ -35,7 +35,7 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogBrandEndpoints
{
var response = new ListCatalogBrandsResponse();
var items = await _catalogBrandRepository.ListAllAsync(cancellationToken);
var items = await _catalogBrandRepository.ListAsync(cancellationToken);
response.CatalogBrands.AddRange(items.Select(_mapper.Map<CatalogBrandDto>));

View File

@@ -17,10 +17,11 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
.WithRequest<CreateCatalogItemRequest>
.WithResponse<CreateCatalogItemResponse>
{
private readonly IAsyncRepository<CatalogItem> _itemRepository;
private readonly IRepository<CatalogItem> _itemRepository;
private readonly IUriComposer _uriComposer;
public Create(IAsyncRepository<CatalogItem> itemRepository, IUriComposer uriComposer)
public Create(IRepository<CatalogItem> itemRepository,
IUriComposer uriComposer)
{
_itemRepository = itemRepository;
_uriComposer = uriComposer;

View File

@@ -15,9 +15,9 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
.WithRequest<DeleteCatalogItemRequest>
.WithResponse<DeleteCatalogItemResponse>
{
private readonly IAsyncRepository<CatalogItem> _itemRepository;
private readonly IRepository<CatalogItem> _itemRepository;
public Delete(IAsyncRepository<CatalogItem> itemRepository)
public Delete(IRepository<CatalogItem> itemRepository)
{
_itemRepository = itemRepository;
}

View File

@@ -12,10 +12,10 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
.WithRequest<GetByIdCatalogItemRequest>
.WithResponse<GetByIdCatalogItemResponse>
{
private readonly IAsyncRepository<CatalogItem> _itemRepository;
private readonly IRepository<CatalogItem> _itemRepository;
private readonly IUriComposer _uriComposer;
public GetById(IAsyncRepository<CatalogItem> itemRepository, IUriComposer uriComposer)
public GetById(IRepository<CatalogItem> itemRepository, IUriComposer uriComposer)
{
_itemRepository = itemRepository;
_uriComposer = uriComposer;

View File

@@ -16,11 +16,11 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
.WithRequest<ListPagedCatalogItemRequest>
.WithResponse<ListPagedCatalogItemResponse>
{
private readonly IAsyncRepository<CatalogItem> _itemRepository;
private readonly IRepository<CatalogItem> _itemRepository;
private readonly IUriComposer _uriComposer;
private readonly IMapper _mapper;
public ListPaged(IAsyncRepository<CatalogItem> itemRepository,
public ListPaged(IRepository<CatalogItem> itemRepository,
IUriComposer uriComposer,
IMapper mapper)
{

View File

@@ -16,10 +16,10 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
.WithRequest<UpdateCatalogItemRequest>
.WithResponse<UpdateCatalogItemResponse>
{
private readonly IAsyncRepository<CatalogItem> _itemRepository;
private readonly IRepository<CatalogItem> _itemRepository;
private readonly IUriComposer _uriComposer;
public Update(IAsyncRepository<CatalogItem> itemRepository, IUriComposer uriComposer)
public Update(IRepository<CatalogItem> itemRepository, IUriComposer uriComposer)
{
_itemRepository = itemRepository;
_uriComposer = uriComposer;

View File

@@ -14,10 +14,10 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogTypeEndpoints
.WithoutRequest
.WithResponse<ListCatalogTypesResponse>
{
private readonly IAsyncRepository<CatalogType> _catalogTypeRepository;
private readonly IRepository<CatalogType> _catalogTypeRepository;
private readonly IMapper _mapper;
public List(IAsyncRepository<CatalogType> catalogTypeRepository,
public List(IRepository<CatalogType> catalogTypeRepository,
IMapper mapper)
{
_catalogTypeRepository = catalogTypeRepository;
@@ -35,7 +35,7 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogTypeEndpoints
{
var response = new ListCatalogTypesResponse();
var items = await _catalogTypeRepository.ListAllAsync(cancellationToken);
var items = await _catalogTypeRepository.ListAsync(cancellationToken);
response.CatalogTypes.AddRange(items.Select(_mapper.Map<CatalogTypeDto>));

View File

@@ -13,8 +13,8 @@
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1" />
<PackageReference Include="MediatR" Version="9.0.0" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="9.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.2.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.2.3" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.6.3" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.11" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="5.0.10" />

View File

@@ -88,7 +88,8 @@ namespace Microsoft.eShopWeb.PublicApi
.AddEntityFrameworkStores<AppIdentityDbContext>()
.AddDefaultTokenProviders();
services.AddScoped(typeof(IAsyncRepository<>), typeof(EfRepository<>));
services.AddScoped(typeof(IRepository<>), typeof(EfRepository<>));
services.AddScoped(typeof(IReadRepository<>), typeof(EfRepository<>));
services.Configure<CatalogSettings>(Configuration);
services.AddSingleton<IUriComposer>(new UriComposer(Configuration.Get<CatalogSettings>()));
services.AddScoped(typeof(IAppLogger<>), typeof(LoggerAdapter<>));

View File

@@ -10,12 +10,16 @@ namespace Microsoft.eShopWeb.Web.Configuration
{
public static class ConfigureCoreServices
{
public static IServiceCollection AddCoreServices(this IServiceCollection services, IConfiguration configuration)
public static IServiceCollection AddCoreServices(this IServiceCollection services,
IConfiguration configuration)
{
services.AddScoped(typeof(IAsyncRepository<>), typeof(EfRepository<>));
//services.AddScoped(typeof(IAsyncRepository<>), typeof(EfRepository<>));
services.AddScoped(typeof(IReadRepository<>), typeof(EfRepository<>));
services.AddScoped(typeof(IRepository<>), typeof(EfRepository<>));
services.AddScoped<IBasketService, BasketService>();
services.AddScoped<IOrderService, OrderService>();
services.AddScoped<IOrderRepository, OrderRepository>();
//services.AddScoped<IOrderRepository, OrderRepository>();
services.AddSingleton<IUriComposer>(new UriComposer(configuration.Get<CatalogSettings>()));
services.AddScoped(typeof(IAppLogger<>), typeof(LoggerAdapter<>));
services.AddTransient<IEmailSender, EmailSender>();

View File

@@ -1,4 +1,5 @@
using MediatR;
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using Microsoft.eShopWeb.ApplicationCore.Specifications;
using Microsoft.eShopWeb.Web.ViewModels;
@@ -11,14 +12,15 @@ namespace Microsoft.eShopWeb.Web.Features.MyOrders
{
public class GetMyOrdersHandler : IRequestHandler<GetMyOrders, IEnumerable<OrderViewModel>>
{
private readonly IOrderRepository _orderRepository;
private readonly IReadRepository<Order> _orderRepository;
public GetMyOrdersHandler(IOrderRepository orderRepository)
public GetMyOrdersHandler(IReadRepository<Order> orderRepository)
{
_orderRepository = orderRepository;
}
public async Task<IEnumerable<OrderViewModel>> Handle(GetMyOrders request, CancellationToken cancellationToken)
public async Task<IEnumerable<OrderViewModel>> Handle(GetMyOrders request,
CancellationToken cancellationToken)
{
var specification = new CustomerOrdersWithItemsSpecification(request.UserName);
var orders = await _orderRepository.ListAsync(specification, cancellationToken);

View File

@@ -1,4 +1,5 @@
using MediatR;
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using Microsoft.eShopWeb.ApplicationCore.Specifications;
using Microsoft.eShopWeb.Web.ViewModels;
@@ -10,17 +11,18 @@ namespace Microsoft.eShopWeb.Web.Features.OrderDetails
{
public class GetOrderDetailsHandler : IRequestHandler<GetOrderDetails, OrderViewModel>
{
private readonly IOrderRepository _orderRepository;
private readonly IReadRepository<Order> _orderRepository;
public GetOrderDetailsHandler(IOrderRepository orderRepository)
public GetOrderDetailsHandler(IReadRepository<Order> orderRepository)
{
_orderRepository = orderRepository;
}
public async Task<OrderViewModel> Handle(GetOrderDetails request, CancellationToken cancellationToken)
public async Task<OrderViewModel> Handle(GetOrderDetails request,
CancellationToken cancellationToken)
{
var customerOrders = await _orderRepository.ListAsync(new CustomerOrdersWithItemsSpecification(request.UserName), cancellationToken);
var order = customerOrders.FirstOrDefault(o => o.Id == request.OrderId);
var spec = new OrderWithItemsByIdSpec(request.OrderId);
var order = await _orderRepository.GetBySpecAsync(spec, cancellationToken);
if (order == null)
{

View File

@@ -12,12 +12,12 @@ namespace Microsoft.eShopWeb.Web.Services
{
public class BasketViewModelService : IBasketViewModelService
{
private readonly IAsyncRepository<Basket> _basketRepository;
private readonly IRepository<Basket> _basketRepository;
private readonly IUriComposer _uriComposer;
private readonly IAsyncRepository<CatalogItem> _itemRepository;
private readonly IRepository<CatalogItem> _itemRepository;
public BasketViewModelService(IAsyncRepository<Basket> basketRepository,
IAsyncRepository<CatalogItem> itemRepository,
public BasketViewModelService(IRepository<Basket> basketRepository,
IRepository<CatalogItem> itemRepository,
IUriComposer uriComposer)
{
_basketRepository = basketRepository;
@@ -28,7 +28,7 @@ namespace Microsoft.eShopWeb.Web.Services
public async Task<BasketViewModel> GetOrCreateBasketForUser(string userName)
{
var basketSpec = new BasketWithItemsSpecification(userName);
var basket = (await _basketRepository.FirstOrDefaultAsync(basketSpec));
var basket = (await _basketRepository.GetBySpecAsync(basketSpec));
if (basket == null)
{

View File

@@ -8,9 +8,9 @@ namespace Microsoft.eShopWeb.Web.Services
{
public class CatalogItemViewModelService : ICatalogItemViewModelService
{
private readonly IAsyncRepository<CatalogItem> _catalogItemRepository;
private readonly IRepository<CatalogItem> _catalogItemRepository;
public CatalogItemViewModelService(IAsyncRepository<CatalogItem> catalogItemRepository)
public CatalogItemViewModelService(IRepository<CatalogItem> catalogItemRepository)
{
_catalogItemRepository = catalogItemRepository;
}

View File

@@ -18,16 +18,16 @@ namespace Microsoft.eShopWeb.Web.Services
public class CatalogViewModelService : ICatalogViewModelService
{
private readonly ILogger<CatalogViewModelService> _logger;
private readonly IAsyncRepository<CatalogItem> _itemRepository;
private readonly IAsyncRepository<CatalogBrand> _brandRepository;
private readonly IAsyncRepository<CatalogType> _typeRepository;
private readonly IRepository<CatalogItem> _itemRepository;
private readonly IRepository<CatalogBrand> _brandRepository;
private readonly IRepository<CatalogType> _typeRepository;
private readonly IUriComposer _uriComposer;
public CatalogViewModelService(
ILoggerFactory loggerFactory,
IAsyncRepository<CatalogItem> itemRepository,
IAsyncRepository<CatalogBrand> brandRepository,
IAsyncRepository<CatalogType> typeRepository,
IRepository<CatalogItem> itemRepository,
IRepository<CatalogBrand> brandRepository,
IRepository<CatalogType> typeRepository,
IUriComposer uriComposer)
{
_logger = loggerFactory.CreateLogger<CatalogViewModelService>();
@@ -80,7 +80,7 @@ namespace Microsoft.eShopWeb.Web.Services
public async Task<IEnumerable<SelectListItem>> GetBrands()
{
_logger.LogInformation("GetBrands called.");
var brands = await _brandRepository.ListAllAsync();
var brands = await _brandRepository.ListAsync();
var items = brands
.Select(brand => new SelectListItem() { Value = brand.Id.ToString(), Text = brand.Brand })
@@ -96,7 +96,7 @@ namespace Microsoft.eShopWeb.Web.Services
public async Task<IEnumerable<SelectListItem>> GetTypes()
{
_logger.LogInformation("GetTypes called.");
var types = await _typeRepository.ListAllAsync();
var types = await _typeRepository.ListAsync();
var items = types
.Select(type => new SelectListItem() { Value = type.Id.ToString(), Text = type.Type })

View File

@@ -22,14 +22,14 @@
<ItemGroup>
<PackageReference Include="Ardalis.ApiEndpoints" Version="2.0.0" />
<PackageReference Include="Ardalis.ListStartupServices" Version="1.1.3" />
<PackageReference Include="Ardalis.Specification" Version="4.1.0" />
<PackageReference Include="Ardalis.Specification" Version="5.2.0" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1" />
<PackageReference Include="MediatR" Version="9.0.0" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="9.0.0" />
<PackageReference Include="BuildBundlerMinifier" Version="3.2.449" Condition="'$(Configuration)'=='Release'" PrivateAssets="All" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="5.0.0" />
<PackageReference Include="Microsoft.CodeAnalysis" Version="3.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis" Version="3.11.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.0" />
<PackageReference Include="Microsoft.Web.LibraryManager.Build" Version="2.1.113" />

View File

@@ -13,7 +13,7 @@ namespace Microsoft.eShopWeb.IntegrationTests.Repositories.BasketRepositoryTests
public class SetQuantities
{
private readonly CatalogContext _catalogContext;
private readonly IAsyncRepository<Basket> _basketRepository;
private readonly EfRepository<Basket> _basketRepository;
private readonly BasketBuilder BasketBuilder = new BasketBuilder();
public SetQuantities()

View File

@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
using Microsoft.eShopWeb.Infrastructure.Data;
using Microsoft.eShopWeb.UnitTests.Builders;
using System.Linq;
@@ -11,7 +12,7 @@ namespace Microsoft.eShopWeb.IntegrationTests.Repositories.OrderRepositoryTests
public class GetById
{
private readonly CatalogContext _catalogContext;
private readonly OrderRepository _orderRepository;
private readonly EfRepository<Order> _orderRepository;
private OrderBuilder OrderBuilder { get; } = new OrderBuilder();
private readonly ITestOutputHelper _output;
public GetById(ITestOutputHelper output)
@@ -21,7 +22,7 @@ namespace Microsoft.eShopWeb.IntegrationTests.Repositories.OrderRepositoryTests
.UseInMemoryDatabase(databaseName: "TestCatalog")
.Options;
_catalogContext = new CatalogContext(dbOptions);
_orderRepository = new OrderRepository(_catalogContext);
_orderRepository = new EfRepository<Order>(_catalogContext);
}
[Fact]
@@ -37,6 +38,7 @@ namespace Microsoft.eShopWeb.IntegrationTests.Repositories.OrderRepositoryTests
Assert.Equal(OrderBuilder.TestBuyerId, orderFromRepo.BuyerId);
// Note: Using InMemoryDatabase OrderItems is available. Will be null if using SQL DB.
// Use the OrderWithItemsByIdSpec instead of just GetById to get the full aggregate
var firstItem = orderFromRepo.OrderItems.FirstOrDefault();
Assert.Equal(OrderBuilder.TestUnits, firstItem.Units);
}

View File

@@ -1,5 +1,6 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
using Microsoft.eShopWeb.ApplicationCore.Specifications;
using Microsoft.eShopWeb.Infrastructure.Data;
using Microsoft.eShopWeb.UnitTests.Builders;
using System.Collections.Generic;
@@ -12,7 +13,7 @@ namespace Microsoft.eShopWeb.IntegrationTests.Repositories.OrderRepositoryTests
public class GetByIdWithItemsAsync
{
private readonly CatalogContext _catalogContext;
private readonly OrderRepository _orderRepository;
private readonly EfRepository<Order> _orderRepository;
private OrderBuilder OrderBuilder { get; } = new OrderBuilder();
public GetByIdWithItemsAsync()
@@ -21,7 +22,7 @@ namespace Microsoft.eShopWeb.IntegrationTests.Repositories.OrderRepositoryTests
.UseInMemoryDatabase(databaseName: "TestCatalog")
.Options;
_catalogContext = new CatalogContext(dbOptions);
_orderRepository = new OrderRepository(_catalogContext);
_orderRepository = new EfRepository<Order>(_catalogContext);
}
[Fact]
@@ -47,7 +48,8 @@ namespace Microsoft.eShopWeb.IntegrationTests.Repositories.OrderRepositoryTests
_catalogContext.SaveChanges();
//Act
var orderFromRepo = await _orderRepository.GetByIdWithItemsAsync(secondOrderId);
var spec = new OrderWithItemsByIdSpec(secondOrderId);
var orderFromRepo = await _orderRepository.GetBySpecAsync(spec);
//Assert
Assert.Equal(secondOrderId, orderFromRepo.Id);

View File

@@ -11,25 +11,20 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
public class AddItemToBasket
{
private readonly string _buyerId = "Test buyerId";
private readonly Mock<IAsyncRepository<Basket>> _mockBasketRepo;
public AddItemToBasket()
{
_mockBasketRepo = new Mock<IAsyncRepository<Basket>>();
}
private readonly Mock<IRepository<Basket>> _mockBasketRepo = new();
[Fact]
public async Task InvokesBasketRepositoryFirstOrDefaultAsyncOnce()
{
var basket = new Basket(_buyerId);
basket.AddItem(1, It.IsAny<decimal>(), It.IsAny<int>());
_mockBasketRepo.Setup(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default)).ReturnsAsync(basket);
_mockBasketRepo.Setup(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default)).ReturnsAsync(basket);
var basketService = new BasketService(_mockBasketRepo.Object, null);
await basketService.AddItemToBasket(basket.Id, 1, 1.50m);
_mockBasketRepo.Verify(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default), Times.Once);
_mockBasketRepo.Verify(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default), Times.Once);
}
[Fact]
@@ -37,7 +32,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
{
var basket = new Basket(_buyerId);
basket.AddItem(1, It.IsAny<decimal>(), It.IsAny<int>());
_mockBasketRepo.Setup(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default)).ReturnsAsync(basket);
_mockBasketRepo.Setup(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default)).ReturnsAsync(basket);
var basketService = new BasketService(_mockBasketRepo.Object, null);

View File

@@ -10,12 +10,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
public class DeleteBasket
{
private readonly string _buyerId = "Test buyerId";
private readonly Mock<IAsyncRepository<Basket>> _mockBasketRepo;
public DeleteBasket()
{
_mockBasketRepo = new Mock<IAsyncRepository<Basket>>();
}
private readonly Mock<IRepository<Basket>> _mockBasketRepo = new();
[Fact]
public async Task ShouldInvokeBasketRepositoryDeleteAsyncOnce()

View File

@@ -1,40 +1,35 @@
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
using Microsoft.eShopWeb.ApplicationCore.Exceptions;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using Microsoft.eShopWeb.ApplicationCore.Services;
using Moq;
using System;
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
using Microsoft.eShopWeb.ApplicationCore.Exceptions;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using Microsoft.eShopWeb.ApplicationCore.Services;
using Moq;
using System;
using System.Threading.Tasks;
using Xunit;
using Xunit;
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests
{
public class SetQuantities
{
private readonly int _invalidId = -1;
private readonly Mock<IAsyncRepository<Basket>> _mockBasketRepo;
public SetQuantities()
{
_mockBasketRepo = new Mock<IAsyncRepository<Basket>>();
}
[Fact]
public async Task ThrowsGivenInvalidBasketId()
{
var basketService = new BasketService(_mockBasketRepo.Object, null);
await Assert.ThrowsAsync<BasketNotFoundException>(async () =>
await basketService.SetQuantities(_invalidId, new System.Collections.Generic.Dictionary<string, int>()));
}
[Fact]
public async Task ThrowsGivenNullQuantities()
{
var basketService = new BasketService(null, null);
await Assert.ThrowsAsync<ArgumentNullException>(async () =>
await basketService.SetQuantities(123, null));
}
}
}
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests
{
public class SetQuantities
{
private readonly int _invalidId = -1;
private readonly Mock<IRepository<Basket>> _mockBasketRepo = new();
[Fact]
public async Task ThrowsGivenInvalidBasketId()
{
var basketService = new BasketService(_mockBasketRepo.Object, null);
await Assert.ThrowsAsync<BasketNotFoundException>(async () =>
await basketService.SetQuantities(_invalidId, new System.Collections.Generic.Dictionary<string, int>()));
}
[Fact]
public async Task ThrowsGivenNullQuantities()
{
var basketService = new BasketService(null, null);
await Assert.ThrowsAsync<ArgumentNullException>(async () =>
await basketService.SetQuantities(123, null));
}
}
}

View File

@@ -1,41 +1,36 @@
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using Microsoft.eShopWeb.ApplicationCore.Services;
using Microsoft.eShopWeb.ApplicationCore.Services;
using Microsoft.eShopWeb.ApplicationCore.Specifications;
using Moq;
using System;
using System;
using System.Threading.Tasks;
using Xunit;
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests
{
public class TransferBasket
{
private readonly string _nonexistentAnonymousBasketBuyerId = "nonexistent-anonymous-basket-buyer-id";
private readonly string _existentAnonymousBasketBuyerId = "existent-anonymous-basket-buyer-id";
private readonly string _nonexistentUserBasketBuyerId = "newuser@microsoft.com";
private readonly string _existentUserBasketBuyerId = "testuser@microsoft.com";
private readonly Mock<IAsyncRepository<Basket>> _mockBasketRepo;
public TransferBasket()
using Xunit;
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests
{
public class TransferBasket
{
private readonly string _nonexistentAnonymousBasketBuyerId = "nonexistent-anonymous-basket-buyer-id";
private readonly string _existentAnonymousBasketBuyerId = "existent-anonymous-basket-buyer-id";
private readonly string _nonexistentUserBasketBuyerId = "newuser@microsoft.com";
private readonly string _existentUserBasketBuyerId = "testuser@microsoft.com";
private readonly Mock<IRepository<Basket>> _mockBasketRepo = new();
[Fact]
public async Task ThrowsGivenNullAnonymousId()
{
_mockBasketRepo = new Mock<IAsyncRepository<Basket>>();
}
[Fact]
public async Task ThrowsGivenNullAnonymousId()
{
var basketService = new BasketService(null, null);
await Assert.ThrowsAsync<ArgumentNullException>(async () => await basketService.TransferBasketAsync(null, "steve"));
}
[Fact]
public async Task ThrowsGivenNullUserId()
{
var basketService = new BasketService(null, null);
await Assert.ThrowsAsync<ArgumentNullException>(async () => await basketService.TransferBasketAsync("abcdefg", null));
var basketService = new BasketService(null, null);
await Assert.ThrowsAsync<ArgumentNullException>(async () => await basketService.TransferBasketAsync(null, "steve"));
}
[Fact]
public async Task ThrowsGivenNullUserId()
{
var basketService = new BasketService(null, null);
await Assert.ThrowsAsync<ArgumentNullException>(async () => await basketService.TransferBasketAsync("abcdefg", null));
}
[Fact]
@@ -43,12 +38,12 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
{
var anonymousBasket = null as Basket;
var userBasket = new Basket(_existentUserBasketBuyerId);
_mockBasketRepo.SetupSequence(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default))
_mockBasketRepo.SetupSequence(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default))
.ReturnsAsync(anonymousBasket)
.ReturnsAsync(userBasket);
var basketService = new BasketService(_mockBasketRepo.Object, null);
await basketService.TransferBasketAsync(_nonexistentAnonymousBasketBuyerId, _existentUserBasketBuyerId);
_mockBasketRepo.Verify(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default), Times.Once);
_mockBasketRepo.Verify(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default), Times.Once);
}
[Fact]
@@ -60,7 +55,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
var userBasket = new Basket(_existentUserBasketBuyerId);
userBasket.AddItem(1, 10, 4);
userBasket.AddItem(2, 99, 3);
_mockBasketRepo.SetupSequence(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default))
_mockBasketRepo.SetupSequence(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default))
.ReturnsAsync(anonymousBasket)
.ReturnsAsync(userBasket);
var basketService = new BasketService(_mockBasketRepo.Object, null);
@@ -77,7 +72,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
{
var anonymousBasket = new Basket(_existentAnonymousBasketBuyerId);
var userBasket = new Basket(_existentUserBasketBuyerId);
_mockBasketRepo.SetupSequence(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default))
_mockBasketRepo.SetupSequence(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default))
.ReturnsAsync(anonymousBasket)
.ReturnsAsync(userBasket);
var basketService = new BasketService(_mockBasketRepo.Object, null);
@@ -91,12 +86,12 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
{
var anonymousBasket = new Basket(_existentAnonymousBasketBuyerId);
var userBasket = null as Basket;
_mockBasketRepo.SetupSequence(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default))
_mockBasketRepo.SetupSequence(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default))
.ReturnsAsync(anonymousBasket)
.ReturnsAsync(userBasket);
var basketService = new BasketService(_mockBasketRepo.Object, null);
await basketService.TransferBasketAsync(_existentAnonymousBasketBuyerId, _nonexistentUserBasketBuyerId);
_mockBasketRepo.Verify(x => x.AddAsync(It.Is<Basket>(x => x.BuyerId == _nonexistentUserBasketBuyerId), default), Times.Once);
}
}
}
}
}

View File

@@ -1,5 +1,4 @@
using Ardalis.Specification.EntityFrameworkCore;
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
using Microsoft.eShopWeb.ApplicationCore.Specifications;
using Moq;
using System.Collections.Generic;
@@ -13,16 +12,12 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications
private readonly int _testBasketId = 123;
private readonly string _buyerId = "Test buyerId";
// tests with specifications can use an evaluator or just WhereExpressions.FirstOrDefault if only one
private readonly SpecificationEvaluator<Basket> _evaluator = new SpecificationEvaluator<Basket>();
[Fact]
public void MatchesBasketWithGivenBasketId()
{
var spec = new BasketWithItemsSpecification(_testBasketId);
var result = _evaluator.GetQuery(GetTestBasketCollection().AsQueryable(), spec)
.FirstOrDefault();
var result = spec.Evaluate(GetTestBasketCollection()).FirstOrDefault();
Assert.NotNull(result);
Assert.Equal(_testBasketId, result.Id);
@@ -34,8 +29,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications
int badBasketId = -1;
var spec = new BasketWithItemsSpecification(badBasketId);
var result = _evaluator.GetQuery(GetTestBasketCollection().AsQueryable(), spec)
.Any();
var result = spec.Evaluate(GetTestBasketCollection()).Any();
Assert.False(result);
}
@@ -45,8 +39,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications
{
var spec = new BasketWithItemsSpecification(_buyerId);
var result = _evaluator.GetQuery(GetTestBasketCollection().AsQueryable(), spec)
.FirstOrDefault();
var result = spec.Evaluate(GetTestBasketCollection()).FirstOrDefault();
Assert.NotNull(result);
Assert.Equal(_buyerId, result.BuyerId);
@@ -58,8 +51,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications
string badBuyerId = "badBuyerId";
var spec = new BasketWithItemsSpecification(badBuyerId);
var result = _evaluator.GetQuery(GetTestBasketCollection().AsQueryable(), spec)
.Any();
var result = spec.Evaluate(GetTestBasketCollection()).Any();
Assert.False(result);
}

View File

@@ -12,7 +12,7 @@ namespace Microsoft.eShopWeb.UnitTests.MediatorHandlers.OrdersTests
{
public class GetMyOrders
{
private readonly Mock<IOrderRepository> _mockOrderRepository;
private readonly Mock<IReadRepository<Order>> _mockOrderRepository;
public GetMyOrders()
{
@@ -20,12 +20,12 @@ namespace Microsoft.eShopWeb.UnitTests.MediatorHandlers.OrdersTests
var address = new Address(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>());
Order order = new Order("buyerId", address, new List<OrderItem> { item });
_mockOrderRepository = new Mock<IOrderRepository>();
_mockOrderRepository = new Mock<IReadRepository<Order>>();
_mockOrderRepository.Setup(x => x.ListAsync(It.IsAny<ISpecification<Order>>(),default)).ReturnsAsync(new List<Order> { order });
}
[Fact]
public async Task NotReturnNullIfOrdersArePresent()
public async Task NotReturnNullIfOrdersArePresIent()
{
var request = new eShopWeb.Web.Features.MyOrders.GetMyOrders("SomeUserName");

View File

@@ -1,6 +1,7 @@
using Ardalis.Specification;
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using Microsoft.eShopWeb.ApplicationCore.Specifications;
using Microsoft.eShopWeb.Web.Features.OrderDetails;
using Moq;
using System.Collections.Generic;
@@ -12,7 +13,7 @@ namespace Microsoft.eShopWeb.UnitTests.MediatorHandlers.OrdersTests
{
public class GetOrderDetails
{
private readonly Mock<IOrderRepository> _mockOrderRepository;
private readonly Mock<IReadRepository<Order>> _mockOrderRepository;
public GetOrderDetails()
{
@@ -20,8 +21,9 @@ namespace Microsoft.eShopWeb.UnitTests.MediatorHandlers.OrdersTests
var address = new Address(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>());
Order order = new Order("buyerId", address, new List<OrderItem> { item });
_mockOrderRepository = new Mock<IOrderRepository>();
_mockOrderRepository.Setup(x => x.ListAsync(It.IsAny<ISpecification<Order>>(),default)).ReturnsAsync(new List<Order> { order });
_mockOrderRepository = new Mock<IReadRepository<Order>>();
_mockOrderRepository.Setup(x => x.GetBySpecAsync(It.IsAny<OrderWithItemsByIdSpec>(),default))
.ReturnsAsync(order);
}
[Fact]
@@ -35,17 +37,5 @@ namespace Microsoft.eShopWeb.UnitTests.MediatorHandlers.OrdersTests
Assert.NotNull(result);
}
[Fact]
public async Task BeNullIfOrderNotFound()
{
var request = new eShopWeb.Web.Features.OrderDetails.GetOrderDetails("SomeUserName", 100);
var handler = new GetOrderDetailsHandler(_mockOrderRepository.Object);
var result = await handler.Handle(request, CancellationToken.None);
Assert.Null(result);
}
}
}