Files
eShopOnWeb/tests/IntegrationTests/Repositories/OrderRepositoryTests/GetById.cs
Steve Smith 8a45a2c858 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
2021-10-25 15:13:02 -04:00

47 lines
1.8 KiB
C#

using Microsoft.EntityFrameworkCore;
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
using Microsoft.eShopWeb.Infrastructure.Data;
using Microsoft.eShopWeb.UnitTests.Builders;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
namespace Microsoft.eShopWeb.IntegrationTests.Repositories.OrderRepositoryTests
{
public class GetById
{
private readonly CatalogContext _catalogContext;
private readonly EfRepository<Order> _orderRepository;
private OrderBuilder OrderBuilder { get; } = new OrderBuilder();
private readonly ITestOutputHelper _output;
public GetById(ITestOutputHelper output)
{
_output = output;
var dbOptions = new DbContextOptionsBuilder<CatalogContext>()
.UseInMemoryDatabase(databaseName: "TestCatalog")
.Options;
_catalogContext = new CatalogContext(dbOptions);
_orderRepository = new EfRepository<Order>(_catalogContext);
}
[Fact]
public async Task GetsExistingOrder()
{
var existingOrder = OrderBuilder.WithDefaultValues();
_catalogContext.Orders.Add(existingOrder);
_catalogContext.SaveChanges();
int orderId = existingOrder.Id;
_output.WriteLine($"OrderId: {orderId}");
var orderFromRepo = await _orderRepository.GetByIdAsync(orderId);
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);
}
}
}