Update to 3.1
- Fix test to work on Catalog.API controller, using in-memory database
This commit is contained in:
parent
b5ab54afbc
commit
60b28055ef
@ -1,7 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.0</TargetFramework>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<DebugType>portable</DebugType>
|
||||
<PreserveCompilationContext>true</PreserveCompilationContext>
|
||||
<AssemblyName>Catalog.API</AssemblyName>
|
||||
@ -42,22 +42,22 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AspNetCore.HealthChecks.AzureServiceBus" Version="2.2.5" />
|
||||
<PackageReference Include="AspNetCore.HealthChecks.AzureStorage" Version="2.2.2" />
|
||||
<PackageReference Include="AspNetCore.HealthChecks.Rabbitmq" Version="2.2.1" />
|
||||
<PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="2.2.1" />
|
||||
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="2.2.4" />
|
||||
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="5.0.0-rc1" />
|
||||
<PackageReference Include="Google.Protobuf" Version="3.10.0-rc1" />
|
||||
<PackageReference Include="Grpc.AspNetCore.Server" Version="0.2.23-pre2" />
|
||||
<PackageReference Include="Grpc.Tools" Version="2.24.0-pre1" PrivateAssets="All" />
|
||||
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.8.0-beta3" />
|
||||
<PackageReference Include="Microsoft.ApplicationInsights.DependencyCollector" Version="2.11.0-beta2" />
|
||||
<PackageReference Include="AspNetCore.HealthChecks.AzureServiceBus" Version="3.0.0" />
|
||||
<PackageReference Include="AspNetCore.HealthChecks.AzureStorage" Version="3.0.1" />
|
||||
<PackageReference Include="AspNetCore.HealthChecks.Rabbitmq" Version="3.0.3" />
|
||||
<PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="3.0.0" />
|
||||
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="3.0.0" />
|
||||
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="5.0.1" />
|
||||
<PackageReference Include="Google.Protobuf" Version="3.11.2" />
|
||||
<PackageReference Include="Grpc.AspNetCore.Server" Version="2.25.0" />
|
||||
<PackageReference Include="Grpc.Tools" Version="2.25.0" PrivateAssets="All" />
|
||||
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.12.0" />
|
||||
<PackageReference Include="Microsoft.ApplicationInsights.DependencyCollector" Version="2.12.0" />
|
||||
<PackageReference Include="Microsoft.ApplicationInsights.Kubernetes" Version="1.1.1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.HealthChecks" Version="1.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.AzureKeyVault" Version="3.0.0-rc1.19456.10" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.AzureAppServices" Version="3.0.0-rc1.19456.10" />
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="3.0.1-dev-00099" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.AzureKeyVault" Version="3.1.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.AzureAppServices" Version="3.1.0" />
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="3.2.0" />
|
||||
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.1.3" />
|
||||
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.1-dev-00209" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.2-dev-00824" />
|
||||
|
@ -1,7 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.0</TargetFramework>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
@ -33,9 +33,9 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.0.0-rc1.19457.4" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="3.0.0-rc1.19457.4" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.3.0-preview-20190828-03" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.1.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="3.1.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
|
||||
|
@ -1,90 +1,132 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.eShopOnContainers.WebMVC.Controllers;
|
||||
using Microsoft.eShopOnContainers.WebMVC.Services;
|
||||
using Microsoft.eShopOnContainers.WebMVC.ViewModels;
|
||||
using Microsoft.eShopOnContainers.WebMVC.ViewModels.CatalogViewModels;
|
||||
using Catalog.API.IntegrationEvents;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.eShopOnContainers.Services.Catalog.API;
|
||||
using Microsoft.eShopOnContainers.Services.Catalog.API.Controllers;
|
||||
using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure;
|
||||
using Microsoft.eShopOnContainers.Services.Catalog.API.Model;
|
||||
using Microsoft.eShopOnContainers.Services.Catalog.API.ViewModel;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Moq;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
using CatalogModel = Microsoft.eShopOnContainers.WebMVC.ViewModels.Catalog;
|
||||
|
||||
namespace UnitTest.Catalog.Application
|
||||
{
|
||||
public class CatalogControllerTest
|
||||
{
|
||||
private readonly Mock<ICatalogService> _catalogServiceMock;
|
||||
private readonly DbContextOptions<CatalogContext> _dbOptions;
|
||||
|
||||
public CatalogControllerTest()
|
||||
{
|
||||
_catalogServiceMock = new Mock<ICatalogService>();
|
||||
_dbOptions = new DbContextOptionsBuilder<CatalogContext>()
|
||||
.UseInMemoryDatabase(databaseName: "in-memory")
|
||||
.Options;
|
||||
|
||||
using (var dbContext = new CatalogContext(_dbOptions))
|
||||
{
|
||||
dbContext.AddRange(GetFakeCatalog());
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Get_catalog_items_success()
|
||||
{
|
||||
//Arrange
|
||||
var fakeBrandFilterApplied = 1;
|
||||
var fakeTypesFilterApplied = 2;
|
||||
var fakePage = 2;
|
||||
var fakeCatalog = GetFakeCatalog();
|
||||
var brandFilterApplied = 1;
|
||||
var typesFilterApplied = 2;
|
||||
var pageSize = 4;
|
||||
var pageIndex = 1;
|
||||
|
||||
var expectedNumberOfPages = 5;
|
||||
var expectedTotalPages = 50;
|
||||
var expectedCurrentPage = 2;
|
||||
var expectedItemsInPage = 2;
|
||||
var expectedTotalItems = 6;
|
||||
|
||||
_catalogServiceMock.Setup(x => x.GetCatalogItems
|
||||
(
|
||||
It.Is<int>(y => y == fakePage),
|
||||
It.IsAny<int>(),
|
||||
It.Is<int?>(y => y == fakeBrandFilterApplied),
|
||||
It.Is<int?>(y => y == fakeTypesFilterApplied)
|
||||
))
|
||||
.Returns(Task.FromResult(fakeCatalog));
|
||||
var catalogContext = new CatalogContext(_dbOptions);
|
||||
var catalogSettings = new TestCatalogSettings();
|
||||
|
||||
var integrationServicesMock = new Mock<ICatalogIntegrationEventService>();
|
||||
|
||||
//Act
|
||||
var orderController = new CatalogController(_catalogServiceMock.Object);
|
||||
var actionResult = await orderController.Index(fakeBrandFilterApplied, fakeTypesFilterApplied, fakePage, null);
|
||||
var orderController = new CatalogController(catalogContext, catalogSettings, integrationServicesMock.Object);
|
||||
var actionResult = await orderController.ItemsByTypeIdAndBrandIdAsync(typesFilterApplied, brandFilterApplied, pageSize, pageIndex);
|
||||
|
||||
//Assert
|
||||
var viewResult = Assert.IsType<ViewResult>(actionResult);
|
||||
var model = Assert.IsAssignableFrom<IndexViewModel>(viewResult.ViewData.Model);
|
||||
Assert.Equal(model.PaginationInfo.TotalPages, expectedNumberOfPages);
|
||||
Assert.Equal(model.PaginationInfo.TotalItems, expectedTotalPages);
|
||||
Assert.Equal(model.PaginationInfo.ActualPage, expectedCurrentPage);
|
||||
Assert.Empty(model.PaginationInfo.Next);
|
||||
Assert.Empty(model.PaginationInfo.Previous);
|
||||
}
|
||||
|
||||
private CatalogModel GetFakeCatalog()
|
||||
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 CatalogModel()
|
||||
return new List<CatalogItem>()
|
||||
{
|
||||
PageSize = 10,
|
||||
Count = 50,
|
||||
PageIndex = 2,
|
||||
Data = new List<CatalogItem>()
|
||||
new CatalogItem()
|
||||
{
|
||||
new CatalogItem()
|
||||
{
|
||||
Id = 1,
|
||||
Name = "fakeItemA",
|
||||
CatalogTypeId = 1
|
||||
},
|
||||
new CatalogItem()
|
||||
{
|
||||
Id = 2,
|
||||
Name = "fakeItemB",
|
||||
CatalogTypeId = 1
|
||||
},
|
||||
new CatalogItem()
|
||||
{
|
||||
Id = 3,
|
||||
Name = "fakeItemC",
|
||||
CatalogTypeId = 1
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,17 +1,16 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.0</TargetFramework>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<GenerateErrorForMissingTargetingPacks>false</GenerateErrorForMissingTargetingPacks>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="3.0.0-preview4-19123-01" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.ViewFeatures" Version="3.0.0-preview4-19123-01" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.3.0-preview-20190828-03" />
|
||||
<PackageReference Include="Microsoft.NETCore.Platforms" Version="3.0.0-rc1.19456.4" />
|
||||
<PackageReference Include="Moq" Version="4.13.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
|
||||
<PackageReference Include="Microsoft.NETCore.Platforms" Version="3.1.0" />
|
||||
<PackageReference Include="Moq" Version="4.13.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
|
||||
@ -20,7 +19,6 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\Web\WebMVC\WebMVC.csproj" />
|
||||
<ProjectReference Include="..\Catalog.API\Catalog.API.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.0</TargetFramework>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
|
||||
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
|
||||
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
|
||||
@ -67,10 +67,10 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="3.0.0-preview8.19405.7" />
|
||||
<PackageReference Include="xunit" Version="2.4.0" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0">
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="3.1.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
|
||||
</PackageReference>
|
||||
|
Loading…
x
Reference in New Issue
Block a user