Skip to content

Commit

Permalink
Adding Tests and Refactoring
Browse files Browse the repository at this point in the history
Functional Tests for RazorPages added
  • Loading branch information
ardalis committed May 31, 2018
1 parent 5fb9e74 commit 814d3e2
Show file tree
Hide file tree
Showing 22 changed files with 275 additions and 142 deletions.
9 changes: 5 additions & 4 deletions src/ApplicationCore/Entities/BuyerAggregate/Buyer.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using ApplicationCore.Interfaces;
using Ardalis.GuardClauses;
using Microsoft.eShopWeb.ApplicationCore.Entities;
using System;
using System.Collections.Generic;
using System.Text;

namespace ApplicationCore.Entities.BuyerAggregate
{
Expand All @@ -14,13 +13,15 @@ public class Buyer : BaseEntity, IAggregateRoot

public IEnumerable<PaymentMethod> PaymentMethods => _paymentMethods.AsReadOnly();

protected Buyer()
private Buyer()
{
// required by EF
}

public Buyer(string identity) : this()
{
IdentityGuid = !string.IsNullOrWhiteSpace(identity) ? identity : throw new ArgumentNullException(nameof(identity));
Guard.Against.NullOrEmpty(identity, nameof(identity));
IdentityGuid = identity;
}
}
}
1 change: 0 additions & 1 deletion src/ApplicationCore/Entities/OrderAggregate/Address.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;

namespace ApplicationCore.Entities.OrderAggregate
{
Expand Down
12 changes: 10 additions & 2 deletions src/ApplicationCore/Entities/OrderAggregate/CatalogItemOrdered.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
namespace ApplicationCore.Entities.OrderAggregate
using Ardalis.GuardClauses;

namespace ApplicationCore.Entities.OrderAggregate
{
/// <summary>
/// Represents the item that was ordered. If catalog item details change, details of
/// Represents a snapshot of the item that was ordered. If catalog item details change, details of
/// the item that was part of a completed order should not change.
/// </summary>
public class CatalogItemOrdered // ValueObject
{
public CatalogItemOrdered(int catalogItemId, string productName, string pictureUri)
{
Guard.Against.OutOfRange(catalogItemId, nameof(catalogItemId), 1, int.MaxValue);
Guard.Against.NullOrEmpty(productName, nameof(productName));
Guard.Against.NullOrEmpty(pictureUri, nameof(pictureUri));

CatalogItemId = catalogItemId;
ProductName = productName;
PictureUri = pictureUri;
}

private CatalogItemOrdered()
{
// required by EF
}

public int CatalogItemId { get; private set; }
public string ProductName { get; private set; }
public string PictureUri { get; private set; }
Expand Down
9 changes: 7 additions & 2 deletions src/ApplicationCore/Entities/OrderAggregate/Order.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ApplicationCore.Interfaces;
using Ardalis.GuardClauses;
using Microsoft.eShopWeb.ApplicationCore.Entities;
using System;
using System.Collections.Generic;
Expand All @@ -9,13 +10,18 @@ public class Order : BaseEntity, IAggregateRoot
{
private Order()
{
// required by EF
}

public Order(string buyerId, Address shipToAddress, List<OrderItem> items)
{
Guard.Against.NullOrEmpty(buyerId, nameof(buyerId));
Guard.Against.Null(shipToAddress, nameof(shipToAddress));
Guard.Against.Null(items, nameof(items));

BuyerId = buyerId;
ShipToAddress = shipToAddress;
_orderItems = items;
BuyerId = buyerId;
}
public string BuyerId { get; private set; }

Expand Down Expand Up @@ -43,6 +49,5 @@ public decimal Total()
}
return total;
}

}
}
4 changes: 3 additions & 1 deletion src/ApplicationCore/Entities/OrderAggregate/OrderItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ public class OrderItem : BaseEntity
public decimal UnitPrice { get; private set; }
public int Units { get; private set; }

protected OrderItem()
private OrderItem()
{
// required by EF
}

public OrderItem(CatalogItemOrdered itemOrdered, decimal unitPrice, int units)
{
ItemOrdered = itemOrdered;
Expand Down
1 change: 0 additions & 1 deletion src/ApplicationCore/Exceptions/GuardExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using ApplicationCore.Exceptions;
using Microsoft.eShopWeb.ApplicationCore.Entities;
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;

namespace Ardalis.GuardClauses
Expand Down
1 change: 0 additions & 1 deletion src/ApplicationCore/Interfaces/IOrderService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using ApplicationCore.Entities.OrderAggregate;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace ApplicationCore.Interfaces
Expand Down
4 changes: 2 additions & 2 deletions src/Infrastructure/Data/OrderRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ public Order GetByIdWithItems(int id)
{
return _dbContext.Orders
.Include(o => o.OrderItems)
.Include("OrderItems.ItemOrdered")
.Include($"{nameof(Order.OrderItems)}.{nameof(OrderItem.ItemOrdered)}")
.FirstOrDefault();
}

public Task<Order> GetByIdWithItemsAsync(int id)
{
return _dbContext.Orders
.Include(o => o.OrderItems)
.Include("OrderItems.ItemOrdered")
.Include($"{nameof(Order.OrderItems)}.{nameof(OrderItem.ItemOrdered)}")
.FirstOrDefaultAsync();
}
}
Expand Down
23 changes: 0 additions & 23 deletions src/Web/Controllers/OrderController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,28 +69,5 @@ public async Task<IActionResult> Detail(int orderId)
};
return View(viewModel);
}

private OrderViewModel GetOrder()
{
var order = new OrderViewModel()
{
OrderDate = DateTimeOffset.Now.AddDays(-1),
OrderNumber = 12354,
Status = "Submitted",
Total = 123.45m,
ShippingAddress = new Address("123 Main St.", "Kent", "OH", "United States", "44240")
};

order.OrderItems.Add(new OrderItemViewModel()
{
ProductId = 1,
PictureUrl = "",
ProductName = "Something",
UnitPrice = 5.05m,
Units = 2
});

return order;
}
}
}
12 changes: 8 additions & 4 deletions src/Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ public Startup(IConfiguration configuration)
public void ConfigureDevelopmentServices(IServiceCollection services)
{
// use in-memory database
ConfigureTestingServices(services);
ConfigureInMemoryDatabases(services);

// use real database
// ConfigureProductionServices(services);

ConfigureServices(services);
}
public void ConfigureTestingServices(IServiceCollection services)

private void ConfigureInMemoryDatabases(IServiceCollection services)
{
// use in-memory database
services.AddDbContext<CatalogContext>(c =>
Expand All @@ -46,8 +48,6 @@ public void ConfigureTestingServices(IServiceCollection services)
// Add Identity DbContext
services.AddDbContext<AppIdentityDbContext>(options =>
options.UseInMemoryDatabase("Identity"));

ConfigureServices(services);
}

public void ConfigureProductionServices(IServiceCollection services)
Expand Down Expand Up @@ -86,6 +86,10 @@ public void ConfigureServices(IServiceCollection services)
options.ExpireTimeSpan = TimeSpan.FromHours(1);
options.LoginPath = "/Account/Signin";
options.LogoutPath = "/Account/Signout";
options.Cookie = new CookieBuilder
{
IsEssential = true // required for auth to work without explicit user consent; adjust to suit your privacy policy
};
});

services.AddScoped(typeof(IRepository<>), typeof(EfRepository<>));
Expand Down
1 change: 1 addition & 0 deletions tests/FunctionalTests/FunctionalTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\ApplicationCore\ApplicationCore.csproj" />
<ProjectReference Include="..\..\src\WebRazorPages\WebRazorPages.csproj" />
<ProjectReference Include="..\..\src\Web\Web.csproj" />
</ItemGroup>

Expand Down
18 changes: 14 additions & 4 deletions tests/FunctionalTests/Web/Controllers/ApiCatalogControllerList.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
using Microsoft.eShopWeb.ViewModels;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.eShopWeb;
using Microsoft.eShopWeb.ViewModels;
using Newtonsoft.Json;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;

namespace FunctionalTests.Web.Controllers
{
public class ApiCatalogControllerList : BaseWebTest
public class ApiCatalogControllerList : IClassFixture<CustomWebApplicationFactory<Startup>>
{
public ApiCatalogControllerList(CustomWebApplicationFactory<Startup> factory)
{
Client = factory.CreateClient();
}

public HttpClient Client { get; }

[Fact]
public async Task ReturnsFirst10CatalogItems()
{
var response = await _client.GetAsync("/api/catalog/list");
var response = await Client.GetAsync("/api/catalog/list");
response.EnsureSuccessStatusCode();
var stringResponse = await response.Content.ReadAsStringAsync();
var model = JsonConvert.DeserializeObject<CatalogIndexViewModel>(stringResponse);
Expand All @@ -22,7 +32,7 @@ public async Task ReturnsFirst10CatalogItems()
[Fact]
public async Task ReturnsLast2CatalogItemsGivenPageIndex1()
{
var response = await _client.GetAsync("/api/catalog/list?page=1");
var response = await Client.GetAsync("/api/catalog/list?page=1");
response.EnsureSuccessStatusCode();
var stringResponse = await response.Content.ReadAsStringAsync();
var model = JsonConvert.DeserializeObject<CatalogIndexViewModel>(stringResponse);
Expand Down
89 changes: 0 additions & 89 deletions tests/FunctionalTests/Web/Controllers/BaseWebTest.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ public class CatalogControllerIndex : IClassFixture<CustomWebApplicationFactory<
{
public CatalogControllerIndex(CustomWebApplicationFactory<Startup> factory)
{
Client = factory.CreateClient(new WebApplicationFactoryClientOptions
{
AllowAutoRedirect = false
});
Client = factory.CreateClient();
}

public HttpClient Client { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Extensions.Logging;
using System;
using Microsoft.EntityFrameworkCore;
using Infrastructure.Identity;

namespace FunctionalTests.Web.Controllers
{
Expand All @@ -14,7 +15,6 @@ public class CustomWebApplicationFactory<TStartup>
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.UseEnvironment("Testing");
builder.ConfigureServices(services =>
{
// Create a new service provider.
Expand All @@ -30,6 +30,12 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
options.UseInternalServiceProvider(serviceProvider);
});

services.AddDbContext<AppIdentityDbContext>(options =>
{
options.UseInMemoryDatabase("Identity");
options.UseInternalServiceProvider(serviceProvider);
});

// Build the service provider.
var sp = services.BuildServiceProvider();

Expand Down
Loading

0 comments on commit 814d3e2

Please sign in to comment.