Updated catalog unit test project to Net 6.0
This commit is contained in:
parent
fb8bca5e8b
commit
90f0cff567
@ -1,4 +1,4 @@
|
||||
using Catalog.API.IntegrationEvents;
|
||||
using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.eShopOnContainers.Services.Catalog.API;
|
||||
@ -13,120 +13,118 @@ using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace UnitTest.Catalog.Application
|
||||
namespace UnitTest.Catalog.Application;
|
||||
|
||||
public class CatalogControllerTest
|
||||
{
|
||||
public class CatalogControllerTest
|
||||
private readonly DbContextOptions<CatalogContext> _dbOptions;
|
||||
|
||||
public CatalogControllerTest()
|
||||
{
|
||||
private readonly DbContextOptions<CatalogContext> _dbOptions;
|
||||
_dbOptions = new DbContextOptionsBuilder<CatalogContext>()
|
||||
.UseInMemoryDatabase(databaseName: "in-memory")
|
||||
.Options;
|
||||
|
||||
public CatalogControllerTest()
|
||||
using (var dbContext = new CatalogContext(_dbOptions))
|
||||
{
|
||||
_dbOptions = new DbContextOptionsBuilder<CatalogContext>()
|
||||
.UseInMemoryDatabase(databaseName: "in-memory")
|
||||
.Options;
|
||||
dbContext.AddRange(GetFakeCatalog());
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
}
|
||||
|
||||
using (var dbContext = new CatalogContext(_dbOptions))
|
||||
[Fact]
|
||||
public async Task Get_catalog_items_success()
|
||||
{
|
||||
//Arrange
|
||||
var brandFilterApplied = 1;
|
||||
var typesFilterApplied = 2;
|
||||
var pageSize = 4;
|
||||
var pageIndex = 1;
|
||||
|
||||
var expectedItemsInPage = 2;
|
||||
var expectedTotalItems = 6;
|
||||
|
||||
var catalogContext = new CatalogContext(_dbOptions);
|
||||
var catalogSettings = new TestCatalogSettings();
|
||||
|
||||
var integrationServicesMock = new Mock<ICatalogIntegrationEventService>();
|
||||
|
||||
//Act
|
||||
var orderController = new CatalogController(catalogContext, catalogSettings, integrationServicesMock.Object);
|
||||
var actionResult = await orderController.ItemsByTypeIdAndBrandIdAsync(typesFilterApplied, brandFilterApplied, pageSize, pageIndex);
|
||||
|
||||
//Assert
|
||||
Assert.IsType<ActionResult<PaginatedItemsViewModel<CatalogItem>>>(actionResult);
|
||||
var page = Assert.IsAssignableFrom<PaginatedItemsViewModel<CatalogItem>>(actionResult.Value);
|
||||
Assert.Equal(expectedTotalItems, page.Count);
|
||||
Assert.Equal(pageIndex, page.PageIndex);
|
||||
Assert.Equal(pageSize, page.PageSize);
|
||||
Assert.Equal(expectedItemsInPage, page.Data.Count());
|
||||
}
|
||||
|
||||
private List<CatalogItem> GetFakeCatalog()
|
||||
{
|
||||
return new List<CatalogItem>()
|
||||
{
|
||||
new CatalogItem()
|
||||
{
|
||||
dbContext.AddRange(GetFakeCatalog());
|
||||
dbContext.SaveChanges();
|
||||
Id = 1,
|
||||
Name = "fakeItemA",
|
||||
CatalogTypeId = 2,
|
||||
CatalogBrandId = 1,
|
||||
PictureFileName = "fakeItemA.png"
|
||||
},
|
||||
new CatalogItem()
|
||||
{
|
||||
Id = 2,
|
||||
Name = "fakeItemB",
|
||||
CatalogTypeId = 2,
|
||||
CatalogBrandId = 1,
|
||||
PictureFileName = "fakeItemB.png"
|
||||
},
|
||||
new CatalogItem()
|
||||
{
|
||||
Id = 3,
|
||||
Name = "fakeItemC",
|
||||
CatalogTypeId = 2,
|
||||
CatalogBrandId = 1,
|
||||
PictureFileName = "fakeItemC.png"
|
||||
},
|
||||
new CatalogItem()
|
||||
{
|
||||
Id = 4,
|
||||
Name = "fakeItemD",
|
||||
CatalogTypeId = 2,
|
||||
CatalogBrandId = 1,
|
||||
PictureFileName = "fakeItemD.png"
|
||||
},
|
||||
new CatalogItem()
|
||||
{
|
||||
Id = 5,
|
||||
Name = "fakeItemE",
|
||||
CatalogTypeId = 2,
|
||||
CatalogBrandId = 1,
|
||||
PictureFileName = "fakeItemE.png"
|
||||
},
|
||||
new CatalogItem()
|
||||
{
|
||||
Id = 6,
|
||||
Name = "fakeItemF",
|
||||
CatalogTypeId = 2,
|
||||
CatalogBrandId = 1,
|
||||
PictureFileName = "fakeItemF.png"
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Get_catalog_items_success()
|
||||
{
|
||||
//Arrange
|
||||
var brandFilterApplied = 1;
|
||||
var typesFilterApplied = 2;
|
||||
var pageSize = 4;
|
||||
var pageIndex = 1;
|
||||
|
||||
var expectedItemsInPage = 2;
|
||||
var expectedTotalItems = 6;
|
||||
|
||||
var catalogContext = new CatalogContext(_dbOptions);
|
||||
var catalogSettings = new TestCatalogSettings();
|
||||
|
||||
var integrationServicesMock = new Mock<ICatalogIntegrationEventService>();
|
||||
|
||||
//Act
|
||||
var orderController = new CatalogController(catalogContext, catalogSettings, integrationServicesMock.Object);
|
||||
var actionResult = await orderController.ItemsByTypeIdAndBrandIdAsync(typesFilterApplied, brandFilterApplied, pageSize, pageIndex);
|
||||
|
||||
//Assert
|
||||
Assert.IsType<ActionResult<PaginatedItemsViewModel<CatalogItem>>>(actionResult);
|
||||
var page = Assert.IsAssignableFrom<PaginatedItemsViewModel<CatalogItem>>(actionResult.Value);
|
||||
Assert.Equal(expectedTotalItems, page.Count);
|
||||
Assert.Equal(pageIndex, page.PageIndex);
|
||||
Assert.Equal(pageSize, page.PageSize);
|
||||
Assert.Equal(expectedItemsInPage, page.Data.Count());
|
||||
}
|
||||
|
||||
private List<CatalogItem> GetFakeCatalog()
|
||||
{
|
||||
return new List<CatalogItem>()
|
||||
{
|
||||
new CatalogItem()
|
||||
{
|
||||
Id = 1,
|
||||
Name = "fakeItemA",
|
||||
CatalogTypeId = 2,
|
||||
CatalogBrandId = 1,
|
||||
PictureFileName = "fakeItemA.png"
|
||||
},
|
||||
new CatalogItem()
|
||||
{
|
||||
Id = 2,
|
||||
Name = "fakeItemB",
|
||||
CatalogTypeId = 2,
|
||||
CatalogBrandId = 1,
|
||||
PictureFileName = "fakeItemB.png"
|
||||
},
|
||||
new CatalogItem()
|
||||
{
|
||||
Id = 3,
|
||||
Name = "fakeItemC",
|
||||
CatalogTypeId = 2,
|
||||
CatalogBrandId = 1,
|
||||
PictureFileName = "fakeItemC.png"
|
||||
},
|
||||
new CatalogItem()
|
||||
{
|
||||
Id = 4,
|
||||
Name = "fakeItemD",
|
||||
CatalogTypeId = 2,
|
||||
CatalogBrandId = 1,
|
||||
PictureFileName = "fakeItemD.png"
|
||||
},
|
||||
new CatalogItem()
|
||||
{
|
||||
Id = 5,
|
||||
Name = "fakeItemE",
|
||||
CatalogTypeId = 2,
|
||||
CatalogBrandId = 1,
|
||||
PictureFileName = "fakeItemE.png"
|
||||
},
|
||||
new CatalogItem()
|
||||
{
|
||||
Id = 6,
|
||||
Name = "fakeItemF",
|
||||
CatalogTypeId = 2,
|
||||
CatalogBrandId = 1,
|
||||
PictureFileName = "fakeItemF.png"
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public class TestCatalogSettings : IOptionsSnapshot<CatalogSettings>
|
||||
{
|
||||
public CatalogSettings Value => new CatalogSettings
|
||||
{
|
||||
PicBaseUrl = "http://image-server.com/",
|
||||
AzureStorageEnabled = true
|
||||
};
|
||||
|
||||
public CatalogSettings Get(string name) => Value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class TestCatalogSettings : IOptionsSnapshot<CatalogSettings>
|
||||
{
|
||||
public CatalogSettings Value => new CatalogSettings
|
||||
{
|
||||
PicBaseUrl = "http://image-server.com/",
|
||||
AzureStorageEnabled = true
|
||||
};
|
||||
|
||||
public CatalogSettings Get(string name) => Value;
|
||||
}
|
||||
|
@ -7,9 +7,9 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.2" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.0-preview.7.21378.4" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
|
||||
<PackageReference Include="Microsoft.NETCore.Platforms" Version="5.0.0" />
|
||||
<PackageReference Include="Microsoft.NETCore.Platforms" Version="6.0.0-preview.7.21377.19" />
|
||||
<PackageReference Include="Moq" Version="4.15.2" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
|
Loading…
x
Reference in New Issue
Block a user