Upgrade to use Specification 4.0.0 (#444)

This commit is contained in:
Steve Smith
2020-07-31 14:52:14 -04:00
committed by GitHub
parent e520126857
commit 754c845e9f
15 changed files with 63 additions and 52 deletions

View File

@@ -4,22 +4,25 @@ using System.Collections.Generic;
using System.Linq;
using Xunit;
using Moq;
using Ardalis.Specification.EntityFrameworkCore;
namespace Microsoft.eShopWeb.UnitTests
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications
{
public class BasketWithItems
{
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 = GetTestBasketCollection()
.AsQueryable()
.FirstOrDefault(spec.Criterias.FirstOrDefault());
var result = _evaluator.GetQuery(GetTestBasketCollection().AsQueryable(), spec)
.FirstOrDefault();
Assert.NotNull(result);
Assert.Equal(_testBasketId, result.Id);
@@ -31,9 +34,10 @@ namespace Microsoft.eShopWeb.UnitTests
int badBasketId = -1;
var spec = new BasketWithItemsSpecification(badBasketId);
Assert.False(GetTestBasketCollection()
.AsQueryable()
.Any(spec.Criterias.FirstOrDefault()));
var result = _evaluator.GetQuery(GetTestBasketCollection().AsQueryable(), spec)
.Any();
Assert.False(result);
}
[Fact]
@@ -41,9 +45,8 @@ namespace Microsoft.eShopWeb.UnitTests
{
var spec = new BasketWithItemsSpecification(_buyerId);
var result = GetTestBasketCollection()
.AsQueryable()
.FirstOrDefault(spec.Criterias.FirstOrDefault());
var result = _evaluator.GetQuery(GetTestBasketCollection().AsQueryable(), spec)
.FirstOrDefault();
Assert.NotNull(result);
Assert.Equal(_buyerId, result.BuyerId);
@@ -55,9 +58,10 @@ namespace Microsoft.eShopWeb.UnitTests
string badBuyerId = "badBuyerId";
var spec = new BasketWithItemsSpecification(badBuyerId);
Assert.False(GetTestBasketCollection()
.AsQueryable()
.Any(spec.Criterias.FirstOrDefault()));
var result = _evaluator.GetQuery(GetTestBasketCollection().AsQueryable(), spec)
.Any();
Assert.False(result);
}
public List<Basket> GetTestBasketCollection()

View File

@@ -14,7 +14,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications
var result = GetTestCollection()
.AsQueryable()
.Where(spec.Criterias.FirstOrDefault());
.Where(spec.WhereExpressions.FirstOrDefault());
Assert.NotNull(result);
Assert.Equal(4, result.ToList().Count);
@@ -27,7 +27,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications
var result = GetTestCollection()
.AsQueryable()
.Where(spec.Criterias.FirstOrDefault());
.Where(spec.WhereExpressions.FirstOrDefault());
Assert.NotNull(result);
Assert.Equal(2, result.ToList().Count);

View File

@@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using Xunit;
namespace Microsoft.eShopWeb.UnitTests
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications
{
public class CatalogFilterSpecification
{
@@ -21,7 +21,7 @@ namespace Microsoft.eShopWeb.UnitTests
var result = GetTestItemCollection()
.AsQueryable()
.Where(spec.Criterias.FirstOrDefault());
.Where(spec.WhereExpressions.FirstOrDefault());
Assert.Equal(expectedCount, result.Count());
}

View File

@@ -16,7 +16,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications
var result = GetTestCollection()
.AsQueryable()
.Where(spec.Criterias.FirstOrDefault());
.Where(spec.WhereExpressions.FirstOrDefault());
Assert.NotNull(result);
Assert.Single(result.ToList());
@@ -30,7 +30,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications
var result = GetTestCollection()
.AsQueryable()
.Where(spec.Criterias.FirstOrDefault());
.Where(spec.WhereExpressions.FirstOrDefault());
Assert.NotNull(result);
Assert.Equal(2, result.ToList().Count);

View File

@@ -17,7 +17,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications
var result = GetTestCollection()
.AsQueryable()
.FirstOrDefault(spec.Criterias.FirstOrDefault());
.FirstOrDefault(spec.WhereExpressions.FirstOrDefault());
Assert.NotNull(result);
Assert.NotNull(result.OrderItems);
@@ -32,7 +32,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications
var result = GetTestCollection()
.AsQueryable()
.Where(spec.Criterias.FirstOrDefault())
.Where(spec.WhereExpressions.FirstOrDefault())
.ToList();
Assert.NotNull(result);