diff --git a/.appveyor.yml b/.appveyor.yml index b3a269bdb..09ea8d934 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -4,7 +4,7 @@ branches: - master - development - ^version-.*$ -image: Visual Studio 2019 +image: Visual Studio 2022 configuration: Release clone_depth: 1 before_build: diff --git a/.travis.yml b/.travis.yml index 97c60fd8c..ade94073d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,20 +4,20 @@ git: depth: 10 group: travis_latest sudo: required -dotnet: 3.1 +dotnet: 6.0 solution: MyTested.AspNetCore.Mvc.sln env: global: - DOTNET_CLI_TELEMETRY_OPTOUT: 1 - DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1 - - NET_CORE_VERSION: netcoreapp3.1 + - NET_CORE_VERSION: net6.0 matrix: fast_finish: true include: - os: linux dist: xenial - os: osx - dotnet: 3.1.100 + dotnet: 6.0.101 osx_image: xcode11.2 branches: only: diff --git a/MyTested.AspNetCore.Mvc.sln b/MyTested.AspNetCore.Mvc.sln index db7264fe4..1fab90ecd 100644 --- a/MyTested.AspNetCore.Mvc.sln +++ b/MyTested.AspNetCore.Mvc.sln @@ -215,12 +215,16 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApplication", "samples\C EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Test.MultipleEntryPoints", "samples\Configuration\Test.MultipleEntryPoints\Test.MultipleEntryPoints.csproj", "{840C7C04-90CC-4F0F-BDF9-0D97D399BA3F}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Common", "samples\Configuration\Common\Common.csproj", "{AC971EBF-48A1-47C1-B61F-CF97FCE4F48C}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AdditionalEntryPoint", "samples\Configuration\Common\AdditionalEntryPoint.csproj", "{AC971EBF-48A1-47C1-B61F-CF97FCE4F48C}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MyTested.AspNetCore.Mvc.Versioning", "plugins\MyTested.AspNetCore.Mvc.Versioning\MyTested.AspNetCore.Mvc.Versioning.csproj", "{3529E0FC-4D63-4828-A3DC-ACE7B6243A73}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MyTested.AspNetCore.Mvc.Versioning.Test", "test\MyTested.AspNetCore.Mvc.Versioning.Test\MyTested.AspNetCore.Mvc.Versioning.Test.csproj", "{6541FC98-C8B1-48CF-A3A5-7028B19455E8}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation", "plugins\MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation\MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation.csproj", "{A9353C8C-3EF6-4677-B4D3-8BEB350AC0B0}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation.Test", "test\MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation.Test\MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation.Test.csproj", "{A40A442E-E39B-4EA5-839A-3893AD66A700}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -619,6 +623,14 @@ Global {6541FC98-C8B1-48CF-A3A5-7028B19455E8}.Debug|Any CPU.Build.0 = Debug|Any CPU {6541FC98-C8B1-48CF-A3A5-7028B19455E8}.Release|Any CPU.ActiveCfg = Release|Any CPU {6541FC98-C8B1-48CF-A3A5-7028B19455E8}.Release|Any CPU.Build.0 = Release|Any CPU + {A9353C8C-3EF6-4677-B4D3-8BEB350AC0B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A9353C8C-3EF6-4677-B4D3-8BEB350AC0B0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A9353C8C-3EF6-4677-B4D3-8BEB350AC0B0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A9353C8C-3EF6-4677-B4D3-8BEB350AC0B0}.Release|Any CPU.Build.0 = Release|Any CPU + {A40A442E-E39B-4EA5-839A-3893AD66A700}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A40A442E-E39B-4EA5-839A-3893AD66A700}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A40A442E-E39B-4EA5-839A-3893AD66A700}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A40A442E-E39B-4EA5-839A-3893AD66A700}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -729,6 +741,8 @@ Global {AC971EBF-48A1-47C1-B61F-CF97FCE4F48C} = {7BEC9808-8650-4322-BCC6-1D7D91B53678} {3529E0FC-4D63-4828-A3DC-ACE7B6243A73} = {EF386110-3D7B-4BDF-B403-8DC2AB8CF7BB} {6541FC98-C8B1-48CF-A3A5-7028B19455E8} = {D140FA14-A6C2-4279-8A41-35BC55279DA8} + {A9353C8C-3EF6-4677-B4D3-8BEB350AC0B0} = {EF386110-3D7B-4BDF-B403-8DC2AB8CF7BB} + {A40A442E-E39B-4EA5-839A-3893AD66A700} = {D140FA14-A6C2-4279-8A41-35BC55279DA8} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {99A2DEDD-5195-4EE6-A546-B1CA54C5539F} diff --git a/plugins/MyTested.AspNetCore.Mvc.NewtonsoftJson/MyTested.AspNetCore.Mvc.NewtonsoftJson.csproj b/plugins/MyTested.AspNetCore.Mvc.NewtonsoftJson/MyTested.AspNetCore.Mvc.NewtonsoftJson.csproj index 210f625ed..208d8b426 100644 --- a/plugins/MyTested.AspNetCore.Mvc.NewtonsoftJson/MyTested.AspNetCore.Mvc.NewtonsoftJson.csproj +++ b/plugins/MyTested.AspNetCore.Mvc.NewtonsoftJson/MyTested.AspNetCore.Mvc.NewtonsoftJson.csproj @@ -2,11 +2,11 @@ My Tested ASP.NET Core MVC Newtonsoft JSON components. - 2015-2019 Ivaylo Kenov + 2015-2022 Ivaylo Kenov MyTested.AspNetCore.Mvc.NewtonsoftJson - 3.1.3 + 6.0.0 Ivaylo Kenov - netcoreapp3.1 + net6.0 $(NoWarn);CS1591 true true @@ -29,7 +29,7 @@ - + diff --git a/plugins/MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation/Internal/Razor/ChangeTokenMock.cs b/plugins/MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation/Internal/Razor/ChangeTokenMock.cs new file mode 100644 index 000000000..fa68e543f --- /dev/null +++ b/plugins/MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation/Internal/Razor/ChangeTokenMock.cs @@ -0,0 +1,16 @@ +namespace MyTested.AspNetCore.Mvc.Internal.Razor +{ + using Microsoft.Extensions.Primitives; + using System; + + public class ChangeTokenMock : IChangeToken + { + public static ChangeTokenMock Instance => new ChangeTokenMock(); + + public bool ActiveChangeCallbacks => false; + + public bool HasChanged => false; + + public IDisposable RegisterChangeCallback(Action callback, object state) => DisposableMock.Instance; + } +} diff --git a/plugins/MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation/Internal/Razor/TestActionDescriptorChangeProvider.cs b/plugins/MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation/Internal/Razor/TestActionDescriptorChangeProvider.cs new file mode 100644 index 000000000..5d4920eb3 --- /dev/null +++ b/plugins/MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation/Internal/Razor/TestActionDescriptorChangeProvider.cs @@ -0,0 +1,10 @@ +namespace MyTested.AspNetCore.Mvc.Internal.Razor +{ + using Microsoft.AspNetCore.Mvc.Infrastructure; + using Microsoft.Extensions.Primitives; + + public class TestActionDescriptorChangeProvider : IActionDescriptorChangeProvider + { + public IChangeToken GetChangeToken() => ChangeTokenMock.Instance; + } +} diff --git a/plugins/MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation/MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation.csproj b/plugins/MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation/MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation.csproj new file mode 100644 index 000000000..2db7d6949 --- /dev/null +++ b/plugins/MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation/MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation.csproj @@ -0,0 +1,44 @@ + + + + My Tested ASP.NET Core MVC razor runtime compilation components. + 2015-2022 Ivaylo Kenov + MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation + 6.0.0 + Ivaylo Kenov + net6.0 + $(NoWarn);CS1591 + true + true + MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation + ../../tools/Key.snk + true + true + MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation + aspnetcore;aspnetcoremvc;testing;unit;tests;fluent;testing;framework;asp;net;core;mvc;test;mymvc;mytested + nuget-logo.png + https://mytestedasp.net/ + LICENSE + true + git + https://github.com/ivaylokenov/MyTested.AspNetCore.Mvc + false + MyTested.AspNetCore.Mvc + true + snupkg + + + + + + + + + + + + + + + + diff --git a/plugins/MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation/Plugins/RazorRuntimeCompilationTestPlugin.cs b/plugins/MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation/Plugins/RazorRuntimeCompilationTestPlugin.cs new file mode 100644 index 000000000..9dae1dbb8 --- /dev/null +++ b/plugins/MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation/Plugins/RazorRuntimeCompilationTestPlugin.cs @@ -0,0 +1,17 @@ +namespace MyTested.AspNetCore.Mvc.Plugins +{ + using System; + using Microsoft.AspNetCore.Mvc.Infrastructure; + using Microsoft.Extensions.DependencyInjection; + + public class RazorRuntimeCompilationTestPlugin : IServiceRegistrationPlugin + { + private readonly Type defaultActionDescriptorChangeProviderServiceType = typeof(IActionDescriptorChangeProvider); + + public Func ServiceSelectorPredicate + => serviceDescriptor => serviceDescriptor.ServiceType == this.defaultActionDescriptorChangeProviderServiceType; + + public Action ServiceRegistrationDelegate + => serviceCollection => serviceCollection.ReplaceRazorRuntimeCompilation(); + } +} diff --git a/plugins/MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation/ServiceCollectionRazorRuntimeCompilationExtensions.cs b/plugins/MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation/ServiceCollectionRazorRuntimeCompilationExtensions.cs new file mode 100644 index 000000000..384ed7b4d --- /dev/null +++ b/plugins/MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation/ServiceCollectionRazorRuntimeCompilationExtensions.cs @@ -0,0 +1,24 @@ +namespace MyTested.AspNetCore.Mvc +{ + using Microsoft.AspNetCore.Mvc.Abstractions; + using Microsoft.AspNetCore.Mvc.Infrastructure; + using Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure; + using Microsoft.Extensions.DependencyInjection; + using MyTested.AspNetCore.Mvc.Internal.Razor; + + /// + /// Contains Razor runtime compilation extension methods for . + /// + public static class ServiceCollectionRazorRuntimeCompilationExtensions + { + /// + /// Replaces the default with a mocked implementation.. + /// + /// Instance of type. + /// The same . + public static IServiceCollection ReplaceRazorRuntimeCompilation(this IServiceCollection serviceCollection) + => serviceCollection + .Remove() + .ReplaceSingleton(); + } +} diff --git a/plugins/MyTested.AspNetCore.Mvc.Versioning/MyTested.AspNetCore.Mvc.Versioning.csproj b/plugins/MyTested.AspNetCore.Mvc.Versioning/MyTested.AspNetCore.Mvc.Versioning.csproj index 76261d4e1..10e76dbf6 100644 --- a/plugins/MyTested.AspNetCore.Mvc.Versioning/MyTested.AspNetCore.Mvc.Versioning.csproj +++ b/plugins/MyTested.AspNetCore.Mvc.Versioning/MyTested.AspNetCore.Mvc.Versioning.csproj @@ -2,11 +2,11 @@ My Tested ASP.NET Core MVC versioning components. - 2015-2019 Ivaylo Kenov + 2015-2022 Ivaylo Kenov MyTested.AspNetCore.Mvc.Versioning - 3.1.3 + 6.0.0 Ivaylo Kenov - netcoreapp3.1 + net6.0 $(NoWarn);CS1591 true true @@ -29,7 +29,7 @@ - + diff --git a/samples/ApplicationParts/ApplicationParts.Controllers/ApplicationParts.Controllers.csproj b/samples/ApplicationParts/ApplicationParts.Controllers/ApplicationParts.Controllers.csproj index 4eb9f50f7..6db75368d 100644 --- a/samples/ApplicationParts/ApplicationParts.Controllers/ApplicationParts.Controllers.csproj +++ b/samples/ApplicationParts/ApplicationParts.Controllers/ApplicationParts.Controllers.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 ApplicationParts.Controllers ApplicationParts.Controllers false @@ -17,7 +17,7 @@ - + diff --git a/samples/ApplicationParts/ApplicationParts.Models/ApplicationParts.Models.csproj b/samples/ApplicationParts/ApplicationParts.Models/ApplicationParts.Models.csproj index c5a04a6d8..299297dbb 100644 --- a/samples/ApplicationParts/ApplicationParts.Models/ApplicationParts.Models.csproj +++ b/samples/ApplicationParts/ApplicationParts.Models/ApplicationParts.Models.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 ApplicationParts.Models ApplicationParts.Models false @@ -16,7 +16,7 @@ - + diff --git a/samples/ApplicationParts/ApplicationParts.Services/ApplicationParts.Services.csproj b/samples/ApplicationParts/ApplicationParts.Services/ApplicationParts.Services.csproj index ed1e1b7b4..94fb3d792 100644 --- a/samples/ApplicationParts/ApplicationParts.Services/ApplicationParts.Services.csproj +++ b/samples/ApplicationParts/ApplicationParts.Services/ApplicationParts.Services.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 ApplicationParts.Services ApplicationParts.Services false diff --git a/samples/ApplicationParts/ApplicationParts.Test/ApplicationParts.Test.csproj b/samples/ApplicationParts/ApplicationParts.Test/ApplicationParts.Test.csproj index e7618d04d..a43174241 100644 --- a/samples/ApplicationParts/ApplicationParts.Test/ApplicationParts.Test.csproj +++ b/samples/ApplicationParts/ApplicationParts.Test/ApplicationParts.Test.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 true @@ -15,9 +15,9 @@ - - - + + + diff --git a/samples/ApplicationParts/ApplicationParts.Web/ApplicationParts.Web.csproj b/samples/ApplicationParts/ApplicationParts.Web/ApplicationParts.Web.csproj index 921dc05a0..93599f3bf 100644 --- a/samples/ApplicationParts/ApplicationParts.Web/ApplicationParts.Web.csproj +++ b/samples/ApplicationParts/ApplicationParts.Web/ApplicationParts.Web.csproj @@ -1,33 +1,36 @@  - - netcoreapp3.1 - ApplicationParts.Web - aspnet-ApplicationParts.Web-c273a372-79ef-490d-b0e1-a7fb8f2dacc7 - true - + + net6.0 + ApplicationParts.Web + aspnet-ApplicationParts.Web-c273a372-79ef-490d-b0e1-a7fb8f2dacc7 + true + - - - PreserveNewest - - + + + PreserveNewest + + - - - + + + - - - - - - - - - - - - + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + diff --git a/samples/ApplicationParts/ApplicationParts.Web/Startup.cs b/samples/ApplicationParts/ApplicationParts.Web/Startup.cs index b403f35eb..7e2cf534d 100644 --- a/samples/ApplicationParts/ApplicationParts.Web/Startup.cs +++ b/samples/ApplicationParts/ApplicationParts.Web/Startup.cs @@ -48,6 +48,8 @@ public void ConfigureServices(IServiceCollection services) services.AddTransient(); services.AddTransient(); + + services.AddDatabaseDeveloperPageExceptionFilter(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) @@ -55,7 +57,6 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); - app.UseDatabaseErrorPage(); } else { diff --git a/samples/Autofac/Autofac.AssemblyInit.Test/Autofac.AssemblyInit.Test.csproj b/samples/Autofac/Autofac.AssemblyInit.Test/Autofac.AssemblyInit.Test.csproj index db94456e1..021dac762 100644 --- a/samples/Autofac/Autofac.AssemblyInit.Test/Autofac.AssemblyInit.Test.csproj +++ b/samples/Autofac/Autofac.AssemblyInit.Test/Autofac.AssemblyInit.Test.csproj @@ -1,14 +1,14 @@  - netcoreapp3.1 + net6.0 true - - - + + + diff --git a/samples/Autofac/Autofac.NoContainerBuilder.Test/Autofac.NoContainerBuilder.Test.csproj b/samples/Autofac/Autofac.NoContainerBuilder.Test/Autofac.NoContainerBuilder.Test.csproj index 835227f71..82c68eeb4 100644 --- a/samples/Autofac/Autofac.NoContainerBuilder.Test/Autofac.NoContainerBuilder.Test.csproj +++ b/samples/Autofac/Autofac.NoContainerBuilder.Test/Autofac.NoContainerBuilder.Test.csproj @@ -1,14 +1,17 @@  - netcoreapp3.1 + net6.0 true - + - + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/samples/Autofac/Autofac.NoContainerBuilder.Web/Autofac.NoContainerBuilder.Web.csproj b/samples/Autofac/Autofac.NoContainerBuilder.Web/Autofac.NoContainerBuilder.Web.csproj index 4b40493d0..c73f78261 100644 --- a/samples/Autofac/Autofac.NoContainerBuilder.Web/Autofac.NoContainerBuilder.Web.csproj +++ b/samples/Autofac/Autofac.NoContainerBuilder.Web/Autofac.NoContainerBuilder.Web.csproj @@ -1,14 +1,14 @@  - netcoreapp3.1 + net6.0 InProcess true - + diff --git a/samples/Autofac/Autofac.NoContainerBuilder.Web/Properties/launchSettings.json b/samples/Autofac/Autofac.NoContainerBuilder.Web/Properties/launchSettings.json new file mode 100644 index 000000000..6decd48c3 --- /dev/null +++ b/samples/Autofac/Autofac.NoContainerBuilder.Web/Properties/launchSettings.json @@ -0,0 +1,27 @@ +{ + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:56390/", + "sslPort": 44332 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "Autofac.NoContainerBuilder.Web": { + "commandName": "Project", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "applicationUrl": "https://localhost:5001;http://localhost:5000" + } + } +} \ No newline at end of file diff --git a/samples/Autofac/Autofac.NoContainerBuilder.Web/Startup.cs b/samples/Autofac/Autofac.NoContainerBuilder.Web/Startup.cs index 03cdc5a3e..0c9a23534 100644 --- a/samples/Autofac/Autofac.NoContainerBuilder.Web/Startup.cs +++ b/samples/Autofac/Autofac.NoContainerBuilder.Web/Startup.cs @@ -29,7 +29,7 @@ public IServiceProvider ConfigureServices(IServiceCollection services) services.AddSingleton(_ => new DateTimeService()); - services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0); + services.AddMvc(); var builder = this.GetContainerBuilder(services); diff --git a/samples/Autofac/Autofac.Test/Autofac.Test.csproj b/samples/Autofac/Autofac.Test/Autofac.Test.csproj index 5139ebf26..c071d9480 100644 --- a/samples/Autofac/Autofac.Test/Autofac.Test.csproj +++ b/samples/Autofac/Autofac.Test/Autofac.Test.csproj @@ -1,14 +1,17 @@  - netcoreapp3.1 + net6.0 true - + - + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/samples/Autofac/Autofac.Web/Autofac.Web.csproj b/samples/Autofac/Autofac.Web/Autofac.Web.csproj index 3420e4a64..9871917ae 100644 --- a/samples/Autofac/Autofac.Web/Autofac.Web.csproj +++ b/samples/Autofac/Autofac.Web/Autofac.Web.csproj @@ -1,13 +1,13 @@  - netcoreapp3.1 + net6.0 InProcess true - + diff --git a/samples/Autofac/Autofac.Web/Startup.cs b/samples/Autofac/Autofac.Web/Startup.cs index 758ab0efe..7953d5557 100644 --- a/samples/Autofac/Autofac.Web/Startup.cs +++ b/samples/Autofac/Autofac.Web/Startup.cs @@ -27,7 +27,7 @@ public void ConfigureServices(IServiceCollection services) services.AddSingleton(_ => new DateTimeService()); - services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0); + services.AddMvc(); } public void ConfigureContainer(ContainerBuilder builder) diff --git a/samples/Blog/Blog.Controllers/Blog.Controllers.csproj b/samples/Blog/Blog.Controllers/Blog.Controllers.csproj index 3c191a662..4633b44a0 100644 --- a/samples/Blog/Blog.Controllers/Blog.Controllers.csproj +++ b/samples/Blog/Blog.Controllers/Blog.Controllers.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 true false diff --git a/samples/Blog/Blog.Data/Blog.Data.csproj b/samples/Blog/Blog.Data/Blog.Data.csproj index fa21dddaf..edb5bf76c 100644 --- a/samples/Blog/Blog.Data/Blog.Data.csproj +++ b/samples/Blog/Blog.Data/Blog.Data.csproj @@ -1,14 +1,14 @@  - netcoreapp3.1 + net6.0 true false - - + + diff --git a/samples/Blog/Blog.Services/Blog.Services.csproj b/samples/Blog/Blog.Services/Blog.Services.csproj index e090bf6af..02fb375e4 100644 --- a/samples/Blog/Blog.Services/Blog.Services.csproj +++ b/samples/Blog/Blog.Services/Blog.Services.csproj @@ -1,13 +1,13 @@  - netcoreapp3.1 + net6.0 true false - + diff --git a/samples/Blog/Blog.Test/Blog.Test.csproj b/samples/Blog/Blog.Test/Blog.Test.csproj index 0f23d3de8..b1c1b4e0f 100644 --- a/samples/Blog/Blog.Test/Blog.Test.csproj +++ b/samples/Blog/Blog.Test/Blog.Test.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 true @@ -12,11 +12,14 @@ - - - + + + - + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/samples/Blog/Blog.Web/Blog.Web.csproj b/samples/Blog/Blog.Web/Blog.Web.csproj index 5b930bc8d..ec2e4882f 100644 --- a/samples/Blog/Blog.Web/Blog.Web.csproj +++ b/samples/Blog/Blog.Web/Blog.Web.csproj @@ -1,17 +1,18 @@  - netcoreapp3.1 + net6.0 aspnet-Blog.Web-6757ED6F-7F48-4961-917B-ADA8F5DEAFB4 InProcess true - - - - + + + + + diff --git a/samples/Blog/Blog.Web/Startup.cs b/samples/Blog/Blog.Web/Startup.cs index 3ca64c28a..76479f5e1 100644 --- a/samples/Blog/Blog.Web/Startup.cs +++ b/samples/Blog/Blog.Web/Startup.cs @@ -63,9 +63,10 @@ public void ConfigureServices(IServiceCollection services) services .AddControllersWithViews(options => options .AddAutoValidateAntiforgeryToken()) - .SetCompatibilityVersion(CompatibilityVersion.Version_3_0); + .AddRazorRuntimeCompilation(); services.AddRazorPages(); + services.AddDatabaseDeveloperPageExceptionFilter(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) @@ -73,7 +74,6 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); - app.UseDatabaseErrorPage(); } else { diff --git a/samples/Configuration/Common/Common.csproj b/samples/Configuration/Common/AdditionalEntryPoint.csproj similarity index 64% rename from samples/Configuration/Common/Common.csproj rename to samples/Configuration/Common/AdditionalEntryPoint.csproj index f3cc0c8fb..22e7e8d10 100644 --- a/samples/Configuration/Common/Common.csproj +++ b/samples/Configuration/Common/AdditionalEntryPoint.csproj @@ -1,16 +1,19 @@ - netcoreapp3.1 + net6.0 true false false - + - + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/samples/Configuration/Test.DifferentEnvironment/Test.DifferentEnvironment.csproj b/samples/Configuration/Test.DifferentEnvironment/Test.DifferentEnvironment.csproj index d2d111be5..d63fdd552 100644 --- a/samples/Configuration/Test.DifferentEnvironment/Test.DifferentEnvironment.csproj +++ b/samples/Configuration/Test.DifferentEnvironment/Test.DifferentEnvironment.csproj @@ -1,14 +1,17 @@  - netcoreapp3.1 + net6.0 true - + - + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/samples/Configuration/Test.ExplicitNoStartupType/Test.ExplicitNoStartupType.csproj b/samples/Configuration/Test.ExplicitNoStartupType/Test.ExplicitNoStartupType.csproj index 338a21075..88c2e9465 100644 --- a/samples/Configuration/Test.ExplicitNoStartupType/Test.ExplicitNoStartupType.csproj +++ b/samples/Configuration/Test.ExplicitNoStartupType/Test.ExplicitNoStartupType.csproj @@ -1,14 +1,17 @@  - netcoreapp3.1 + net6.0 true - + - + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/samples/Configuration/Test.MissingStartupType/Test.MissingStartupType.csproj b/samples/Configuration/Test.MissingStartupType/Test.MissingStartupType.csproj index 1010142a8..ac1867f74 100644 --- a/samples/Configuration/Test.MissingStartupType/Test.MissingStartupType.csproj +++ b/samples/Configuration/Test.MissingStartupType/Test.MissingStartupType.csproj @@ -1,14 +1,17 @@  - netcoreapp3.1 + net6.0 true - + - + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/samples/Configuration/Test.MultipleEntryPoints/HomeControllerTest.cs b/samples/Configuration/Test.MultipleEntryPoints/HomeControllerTest.cs index 31175f4e8..16c93a05a 100644 --- a/samples/Configuration/Test.MultipleEntryPoints/HomeControllerTest.cs +++ b/samples/Configuration/Test.MultipleEntryPoints/HomeControllerTest.cs @@ -19,7 +19,6 @@ public void MultipleEntryPointsShouldThrowCorrectException() }); Assert.Equal("Web application WebApplication could not be loaded in the application part manager by convention. You need to set it manually by providing your web project's name in the test configuration's 'General:WebAssemblyName' section ('testsettings.json' file by default).", exception.Message); - } } } diff --git a/samples/Configuration/Test.MultipleEntryPoints/Test.MultipleEntryPoints.csproj b/samples/Configuration/Test.MultipleEntryPoints/Test.MultipleEntryPoints.csproj index 7ab1da26b..3202e145a 100644 --- a/samples/Configuration/Test.MultipleEntryPoints/Test.MultipleEntryPoints.csproj +++ b/samples/Configuration/Test.MultipleEntryPoints/Test.MultipleEntryPoints.csproj @@ -1,19 +1,22 @@ - netcoreapp3.1 + net6.0 true - + - + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + - + diff --git a/samples/Configuration/Test.NoAsync/Test.NoAsync.csproj b/samples/Configuration/Test.NoAsync/Test.NoAsync.csproj index a90c6b076..7ef141cbc 100644 --- a/samples/Configuration/Test.NoAsync/Test.NoAsync.csproj +++ b/samples/Configuration/Test.NoAsync/Test.NoAsync.csproj @@ -1,14 +1,17 @@  - netcoreapp3.1 + net6.0 true - + - + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/samples/Configuration/Test.NoStartupType/Test.NoStartupType.csproj b/samples/Configuration/Test.NoStartupType/Test.NoStartupType.csproj index 338a21075..88c2e9465 100644 --- a/samples/Configuration/Test.NoStartupType/Test.NoStartupType.csproj +++ b/samples/Configuration/Test.NoStartupType/Test.NoStartupType.csproj @@ -1,14 +1,17 @@  - netcoreapp3.1 + net6.0 true - + - + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/samples/Configuration/Test.WrongStartupType/Test.WrongStartupType.csproj b/samples/Configuration/Test.WrongStartupType/Test.WrongStartupType.csproj index 338a21075..88c2e9465 100644 --- a/samples/Configuration/Test.WrongStartupType/Test.WrongStartupType.csproj +++ b/samples/Configuration/Test.WrongStartupType/Test.WrongStartupType.csproj @@ -1,14 +1,17 @@  - netcoreapp3.1 + net6.0 true - + - + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/samples/Configuration/Test.WrongTestAssembly/Test.WrongTestAssembly.csproj b/samples/Configuration/Test.WrongTestAssembly/Test.WrongTestAssembly.csproj index 338a21075..88c2e9465 100644 --- a/samples/Configuration/Test.WrongTestAssembly/Test.WrongTestAssembly.csproj +++ b/samples/Configuration/Test.WrongTestAssembly/Test.WrongTestAssembly.csproj @@ -1,14 +1,17 @@  - netcoreapp3.1 + net6.0 true - + - + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/samples/Configuration/Test.WrongWebAssembly/Test.WrongWebAssembly.csproj b/samples/Configuration/Test.WrongWebAssembly/Test.WrongWebAssembly.csproj index 338a21075..88c2e9465 100644 --- a/samples/Configuration/Test.WrongWebAssembly/Test.WrongWebAssembly.csproj +++ b/samples/Configuration/Test.WrongWebAssembly/Test.WrongWebAssembly.csproj @@ -1,14 +1,17 @@  - netcoreapp3.1 + net6.0 true - + - + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/samples/Configuration/WebApplication.Controllers/WebApplication.Controllers.csproj b/samples/Configuration/WebApplication.Controllers/WebApplication.Controllers.csproj index 53d4616c7..c4914f672 100644 --- a/samples/Configuration/WebApplication.Controllers/WebApplication.Controllers.csproj +++ b/samples/Configuration/WebApplication.Controllers/WebApplication.Controllers.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 true false diff --git a/samples/Configuration/WebApplication.Services/WebApplication.Services.csproj b/samples/Configuration/WebApplication.Services/WebApplication.Services.csproj index 4175b493d..edf64a820 100644 --- a/samples/Configuration/WebApplication.Services/WebApplication.Services.csproj +++ b/samples/Configuration/WebApplication.Services/WebApplication.Services.csproj @@ -1,7 +1,7 @@  - netstandard2.0 + net6.0 true false diff --git a/samples/Configuration/WebApplication/Startup.cs b/samples/Configuration/WebApplication/Startup.cs index bfbc9c1d9..e0a60d0da 100644 --- a/samples/Configuration/WebApplication/Startup.cs +++ b/samples/Configuration/WebApplication/Startup.cs @@ -15,7 +15,7 @@ public Startup(IConfiguration configuration) => this.Configuration = configuration; public IConfiguration Configuration { get; } - + public void ConfigureServices(IServiceCollection services) { services.Configure(options => @@ -27,7 +27,7 @@ public void ConfigureServices(IServiceCollection services) services.AddTransient(); services.AddTransient(); - services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0); + services.AddMvc(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) diff --git a/samples/Configuration/WebApplication/WebApplication.csproj b/samples/Configuration/WebApplication/WebApplication.csproj index 672f058b1..f603b9aed 100644 --- a/samples/Configuration/WebApplication/WebApplication.csproj +++ b/samples/Configuration/WebApplication/WebApplication.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 InProcess true diff --git a/samples/MusicStore/MusicStore.Test/MusicStore.Test.csproj b/samples/MusicStore/MusicStore.Test/MusicStore.Test.csproj index 9b081bdcf..c25cbb405 100644 --- a/samples/MusicStore/MusicStore.Test/MusicStore.Test.csproj +++ b/samples/MusicStore/MusicStore.Test/MusicStore.Test.csproj @@ -1,14 +1,17 @@  - netcoreapp3.1 + net6.0 true - + - + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/samples/MusicStore/MusicStore.Web/MusicStore.Web.csproj b/samples/MusicStore/MusicStore.Web/MusicStore.Web.csproj index f518afac5..3653fea1a 100644 --- a/samples/MusicStore/MusicStore.Web/MusicStore.Web.csproj +++ b/samples/MusicStore/MusicStore.Web/MusicStore.Web.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 aspnet-MusicStore.Web-B1796332-CD47-4D99-85BC-7F98EA978F33 InProcess MusicStore @@ -9,15 +9,15 @@ - - - - - - - - - + + + + + + + + + diff --git a/samples/MusicStore/MusicStore.Web/Startup.cs b/samples/MusicStore/MusicStore.Web/Startup.cs index 2f849cafd..8d54a7d9d 100644 --- a/samples/MusicStore/MusicStore.Web/Startup.cs +++ b/samples/MusicStore/MusicStore.Web/Startup.cs @@ -116,6 +116,8 @@ public void ConfigureServices(IServiceCollection services) options.ClientId = "000000004012C08A"; options.ClientSecret = "GaMQ2hCnqAC6EcDLnXsAeBVIJOLmeutL"; }); + + services.AddDatabaseDeveloperPageExceptionFilter(); } //This method is invoked when ASPNETCORE_ENVIRONMENT is 'Development' or is not defined @@ -129,8 +131,6 @@ public void ConfigureDevelopment(IApplicationBuilder app, ILoggerFactory loggerF // During development use the ErrorPage middleware to display error information in the browser app.UseDeveloperExceptionPage(); - app.UseDatabaseErrorPage(); - Configure(app); } diff --git a/samples/NoStartup/NoStartup.Components/NoStartup.Components.csproj b/samples/NoStartup/NoStartup.Components/NoStartup.Components.csproj index 38e122c6b..e80ac369e 100644 --- a/samples/NoStartup/NoStartup.Components/NoStartup.Components.csproj +++ b/samples/NoStartup/NoStartup.Components/NoStartup.Components.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 $(NoWarn);CS1591 true false diff --git a/samples/NoStartup/NoStartup.Controllers/NoStartup.Controllers.csproj b/samples/NoStartup/NoStartup.Controllers/NoStartup.Controllers.csproj index d2312e879..7b947a744 100644 --- a/samples/NoStartup/NoStartup.Controllers/NoStartup.Controllers.csproj +++ b/samples/NoStartup/NoStartup.Controllers/NoStartup.Controllers.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 $(NoWarn);CS1591 true false diff --git a/samples/NoStartup/NoStartup.Services/NoStartup.Services.csproj b/samples/NoStartup/NoStartup.Services/NoStartup.Services.csproj index dc7a038cb..8c72b85bb 100644 --- a/samples/NoStartup/NoStartup.Services/NoStartup.Services.csproj +++ b/samples/NoStartup/NoStartup.Services/NoStartup.Services.csproj @@ -1,7 +1,7 @@  - netstandard2.0 + net6.0 $(NoWarn);CS1591 true false diff --git a/samples/NoStartup/NoStartup.Test/NoStartup.Test.csproj b/samples/NoStartup/NoStartup.Test/NoStartup.Test.csproj index 63778d56d..a81a93c6e 100644 --- a/samples/NoStartup/NoStartup.Test/NoStartup.Test.csproj +++ b/samples/NoStartup/NoStartup.Test/NoStartup.Test.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 true @@ -24,9 +24,9 @@ - - - + + + \ No newline at end of file diff --git a/samples/WebStartup/WebStartup.Test/WebStartup.Test.csproj b/samples/WebStartup/WebStartup.Test/WebStartup.Test.csproj index f971de011..35fb314f3 100644 --- a/samples/WebStartup/WebStartup.Test/WebStartup.Test.csproj +++ b/samples/WebStartup/WebStartup.Test/WebStartup.Test.csproj @@ -1,14 +1,17 @@  - netcoreapp3.1 + net6.0 true - + - + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/samples/WebStartup/WebStartup.Web/Startup.cs b/samples/WebStartup/WebStartup.Web/Startup.cs index fae1329de..31fccf350 100644 --- a/samples/WebStartup/WebStartup.Web/Startup.cs +++ b/samples/WebStartup/WebStartup.Web/Startup.cs @@ -25,7 +25,7 @@ public void ConfigureServices(IServiceCollection services) options.MinimumSameSitePolicy = SameSiteMode.None; }); - services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0); + services.AddMvc(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. diff --git a/samples/WebStartup/WebStartup.Web/WebStartup.Web.csproj b/samples/WebStartup/WebStartup.Web/WebStartup.Web.csproj index 8758685e3..f04ed27dd 100644 --- a/samples/WebStartup/WebStartup.Web/WebStartup.Web.csproj +++ b/samples/WebStartup/WebStartup.Web/WebStartup.Web.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 InProcess true diff --git a/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Base/BaseTestBuilder.cs b/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Base/BaseTestBuilder.cs index 088581899..7775d2b7f 100644 --- a/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Base/BaseTestBuilder.cs +++ b/src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Base/BaseTestBuilder.cs @@ -2,6 +2,7 @@ { using Contracts.Base; using Internal.TestContexts; + // using Licensing; using Microsoft.AspNetCore.Http; using Utilities.Validators; @@ -16,7 +17,11 @@ public abstract class BaseTestBuilder : IBaseTestBuilder /// Initializes a new instance of the class. /// /// containing data about the currently executed assertion chain. - protected BaseTestBuilder(HttpTestContext testContext) => this.TestContext = testContext; + protected BaseTestBuilder(HttpTestContext testContext) + { + // TestCounter.IncrementAndValidate(); + this.TestContext = testContext; + } public HttpContext HttpContext => this.TestContext.HttpContext; diff --git a/src/MyTested.AspNetCore.Mvc.Abstractions/From.cs b/src/MyTested.AspNetCore.Mvc.Abstractions/From.cs index fdb593a3a..441471c17 100644 --- a/src/MyTested.AspNetCore.Mvc.Abstractions/From.cs +++ b/src/MyTested.AspNetCore.Mvc.Abstractions/From.cs @@ -8,10 +8,7 @@ /// public static class From { - static From() - { - TestApplication.TryInitialize(); - } + static From() => TestApplication.TryInitialize(); /// /// Indicates that a argument should be resolved from the application in a method call lambda expression. diff --git a/src/MyTested.AspNetCore.Mvc.Abstractions/Internal/Application/TestApplication.cs b/src/MyTested.AspNetCore.Mvc.Abstractions/Internal/Application/TestApplication.cs index dad689347..7a502a10c 100644 --- a/src/MyTested.AspNetCore.Mvc.Abstractions/Internal/Application/TestApplication.cs +++ b/src/MyTested.AspNetCore.Mvc.Abstractions/Internal/Application/TestApplication.cs @@ -2,6 +2,7 @@ { using System; using Configuration; + // using Licensing; using Microsoft.AspNetCore.Builder; using Server; using Services; @@ -29,7 +30,7 @@ public static void TryInitialize() { return; } - + var generalConfiguration = ServerTestConfiguration.General; if (StartupType == null && generalConfiguration.AutomaticStartup) @@ -65,6 +66,12 @@ private static void Initialize() ValidateStartup(); + //TestCounter.SetLicenseData( + // ServerTestConfiguration.Global.Licenses, + // DateTime.UtcNow, + // DateTime.ParseExact(TestFramework.ReleaseDate, "yyyy-MM-dd", CultureInfo.InvariantCulture), + // TestWebServer.TestAssemblyName); + PluginsContainer.LoadPlugins(TestWebServer.GetDependencyContext()); var serviceCollection = TestWebServer.GetInitialServiceCollection(); @@ -104,6 +111,7 @@ private static void Reset() AdditionalRouting = null; TestServiceProvider.Current = null; TestServiceProvider.ClearServiceLifetimes(); + // LicenseValidator.ClearLicenseDetails(); PluginsContainer.Reset(); } } diff --git a/src/MyTested.AspNetCore.Mvc.Abstractions/Internal/Application/TestApplicationServices.cs b/src/MyTested.AspNetCore.Mvc.Abstractions/Internal/Application/TestApplicationServices.cs index d926695d4..829212dcc 100644 --- a/src/MyTested.AspNetCore.Mvc.Abstractions/Internal/Application/TestApplicationServices.cs +++ b/src/MyTested.AspNetCore.Mvc.Abstractions/Internal/Application/TestApplicationServices.cs @@ -39,40 +39,52 @@ public static IServiceProvider RoutingServices private static void PrepareServices(IServiceCollection serviceCollection) { - var knownServicesReplaced = false; - - if (startupMethods?.ConfigureServicesDelegate != null) + try { - var startupServiceProvider = PrepareStartupServices(serviceCollection); - var hasTestMarkerService = ValidateTestServices(startupServiceProvider); + var knownServicesReplaced = false; - if (hasTestMarkerService) + if (startupMethods?.ConfigureServicesDelegate != null) { - knownServicesReplaced = true; - serviceProvider = startupServiceProvider; + var startupServiceProvider = PrepareStartupServices(serviceCollection); + var hasTestMarkerService = ValidateTestServices(startupServiceProvider); + + if (hasTestMarkerService) + { + knownServicesReplaced = true; + serviceProvider = startupServiceProvider; + } } - } - else - { - // Server additional services delegate is never invoked because Startup is null. - TestWebServer.AdditionalServices?.Invoke(serviceCollection); + else + { + // Server additional services delegate is never invoked because Startup is null. + TestWebServer.AdditionalServices?.Invoke(serviceCollection); - PrepareDefaultServices(serviceCollection); - } + PrepareDefaultServices(serviceCollection); + } - if (!knownServicesReplaced) - { - AdditionalServices?.Invoke(serviceCollection); + if (!knownServicesReplaced) + { + AdditionalServices?.Invoke(serviceCollection); - TryReplaceKnownServices(serviceCollection); - } + TryReplaceKnownServices(serviceCollection); + } + + serviceProvider ??= serviceCollection.BuildServiceProviderFromFactory(); - serviceProvider ??= serviceCollection.BuildServiceProviderFromFactory(); + PrepareRoutingServices(knownServicesReplaced); + EnsureApplicationParts(serviceProvider); - PrepareRoutingServices(knownServicesReplaced); - EnsureApplicationParts(serviceProvider); + PluginsContainer.InitializationPlugins.ForEach(plugin => plugin.InitializationDelegate(serviceProvider)); + } + catch (Exception exception) + { + if (exception is InvalidOperationException) + { + throw; + } - PluginsContainer.InitializationPlugins.ForEach(plugin => plugin.InitializationDelegate(serviceProvider)); + throw new InvalidOperationException($"Test application could not be initialized. You may need to create a custom mock for one of your registered services. If you are having difficulties debugging this error, open an issue at https://github.com/ivaylokenov/MyTested.AspNetCore.Mvc/issues. Provide your Startup classes and this exception message: '{exception.Message}'."); + } } private static bool ValidateTestServices(IServiceProvider startupServiceProvider) @@ -113,7 +125,7 @@ private static IServiceProvider PrepareStartupServices(IServiceCollection servic } catch (InvalidOperationException exception) { - throw new InvalidOperationException($"{exception.Message} Services could not be configured. If your web project is registering services outside of the Startup class (during the WebHost configuration in the Program.cs file for example), you should provide them to the test framework too by calling 'IsRunningOn(server => server.WithServices(servicesAction))'. Since this method should be called only once per test project, you may invoke it in the static constructor of your {TestWebServer.Environment.EnvironmentName}Startup class or if your test runner supports it - in the test assembly initialization."); + throw new InvalidOperationException($"An exception with the following message was thrown during initialization: '{exception.Message}'. Services could not be configured. If your web project is registering services outside of the Startup class (during the WebHost configuration in the Program.cs file for example), you should provide them to the test framework too by calling 'IsRunningOn(server => server.WithServices(servicesAction))'. Since this method should be called only once per test project, you may invoke it in the static constructor of your {TestWebServer.Environment.EnvironmentName}Startup class or if your test runner supports it - in the test assembly initialization."); } } diff --git a/src/MyTested.AspNetCore.Mvc.Abstractions/Internal/Application/TestApplicationStartupLoader.cs b/src/MyTested.AspNetCore.Mvc.Abstractions/Internal/Application/TestApplicationStartupLoader.cs index 72ece3df5..0352457bb 100644 --- a/src/MyTested.AspNetCore.Mvc.Abstractions/Internal/Application/TestApplicationStartupLoader.cs +++ b/src/MyTested.AspNetCore.Mvc.Abstractions/Internal/Application/TestApplicationStartupLoader.cs @@ -116,7 +116,8 @@ private static void PrepareStartup(IServiceCollection serviceCollection) startupMethods = startupLoader.LoadMethods( serviceCollection.BuildServiceProviderFromFactory(), StartupType, - TestWebServer.Environment.EnvironmentName); + TestWebServer.Environment.EnvironmentName, + null); if (typeof(IStartup).GetTypeInfo().IsAssignableFrom(StartupType.GetTypeInfo())) { diff --git a/src/MyTested.AspNetCore.Mvc.Abstractions/Internal/PluginsContainer.cs b/src/MyTested.AspNetCore.Mvc.Abstractions/Internal/PluginsContainer.cs index 7db0f908c..f60fb9a97 100644 --- a/src/MyTested.AspNetCore.Mvc.Abstractions/Internal/PluginsContainer.cs +++ b/src/MyTested.AspNetCore.Mvc.Abstractions/Internal/PluginsContainer.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; + // using Licensing; using Microsoft.DotNet.PlatformAbstractions; using Microsoft.Extensions.DependencyModel; using Plugins; @@ -36,6 +37,11 @@ public static void LoadPlugins(DependencyContext dependencyContext) .Where(l => l.Name.StartsWith(testFrameworkName)) .ToArray(); + //if (testFrameworkAssemblies.Length == 7 && testFrameworkAssemblies.Any(t => t.Name == $"{testFrameworkName}.Lite")) + //{ + // TestCounter.SkipValidation = true; + //} + var plugins = testFrameworkAssemblies .Select(l => Assembly .Load(new AssemblyName(l.Name)) diff --git a/src/MyTested.AspNetCore.Mvc.Abstractions/Internal/Server/TestWebServer.cs b/src/MyTested.AspNetCore.Mvc.Abstractions/Internal/Server/TestWebServer.cs index 472cbdd3f..bf208797a 100644 --- a/src/MyTested.AspNetCore.Mvc.Abstractions/Internal/Server/TestWebServer.cs +++ b/src/MyTested.AspNetCore.Mvc.Abstractions/Internal/Server/TestWebServer.cs @@ -20,7 +20,8 @@ private static IWebHostEnvironment PrepareEnvironment() ApplicationName = ApplicationName, EnvironmentName = ServerTestConfiguration.General.EnvironmentName, ContentRootPath = AppContext.BaseDirectory, - WebRootFileProvider = new NullFileProvider() + WebRootFileProvider = new NullFileProvider(), + ContentRootFileProvider = new NullFileProvider() }; internal static void ResetConfigurationAndAssemblies() diff --git a/src/MyTested.AspNetCore.Mvc.Abstractions/Internal/Services/TestServiceProvider.cs b/src/MyTested.AspNetCore.Mvc.Abstractions/Internal/Services/TestServiceProvider.cs index a97ea3060..e63a391b6 100644 --- a/src/MyTested.AspNetCore.Mvc.Abstractions/Internal/Services/TestServiceProvider.cs +++ b/src/MyTested.AspNetCore.Mvc.Abstractions/Internal/Services/TestServiceProvider.cs @@ -87,6 +87,9 @@ public static TInstance TryGetService() public static void SaveServiceLifetime(Type serviceType, ServiceLifetime lifetime) => ServiceLifetimes[serviceType] = lifetime; + public static void SaveServiceLifetime(ServiceLifetime lifetime) + => SaveServiceLifetime(typeof(TService), lifetime); + public static ServiceLifetime GetServiceLifetime(Type serviceType) => ServiceLifetimes.ContainsKey(serviceType) ? ServiceLifetimes[serviceType] diff --git a/src/MyTested.AspNetCore.Mvc.Abstractions/Internal/TestFramework.cs b/src/MyTested.AspNetCore.Mvc.Abstractions/Internal/TestFramework.cs index 72d5a88da..4f194b224 100644 --- a/src/MyTested.AspNetCore.Mvc.Abstractions/Internal/TestFramework.cs +++ b/src/MyTested.AspNetCore.Mvc.Abstractions/Internal/TestFramework.cs @@ -7,8 +7,8 @@ public static class TestFramework { public const string TestFrameworkName = "MyTested.AspNetCore.Mvc"; - public const string ReleaseDate = "2019-12-12"; - public const string VersionPrefix = "3.1"; + public const string ReleaseDate = "2022-12-28"; + public const string VersionPrefix = "6.0"; internal static void EnsureCorrectVersion(DependencyContext dependencyContext) { diff --git a/src/MyTested.AspNetCore.Mvc.Abstractions/MyTested.AspNetCore.Mvc.Abstractions.csproj b/src/MyTested.AspNetCore.Mvc.Abstractions/MyTested.AspNetCore.Mvc.Abstractions.csproj index acbb2cfb8..92c8e133e 100644 --- a/src/MyTested.AspNetCore.Mvc.Abstractions/MyTested.AspNetCore.Mvc.Abstractions.csproj +++ b/src/MyTested.AspNetCore.Mvc.Abstractions/MyTested.AspNetCore.Mvc.Abstractions.csproj @@ -2,11 +2,11 @@ My Tested ASP.NET Core MVC common abstractions and interfaces. - 2015-2019 Ivaylo Kenov + 2015-2022 Ivaylo Kenov MyTested.AspNetCore.Mvc.Abstractions - 3.1.3 + 6.0.0 Ivaylo Kenov - netcoreapp3.1 + net6.0 $(NoWarn);CS1591 true true @@ -34,10 +34,10 @@ - - - - + + + + diff --git a/src/MyTested.AspNetCore.Mvc.Abstractions/Utilities/ExposedObject.cs b/src/MyTested.AspNetCore.Mvc.Abstractions/Utilities/ExposedObject.cs index acb7fcf06..6cc42bd8a 100644 --- a/src/MyTested.AspNetCore.Mvc.Abstractions/Utilities/ExposedObject.cs +++ b/src/MyTested.AspNetCore.Mvc.Abstractions/Utilities/ExposedObject.cs @@ -79,18 +79,22 @@ public override bool TryGetMember(GetMemberBinder binder, out object result) return base.TryGetMember(binder, out result); } - public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) + public override bool TryInvokeMember(InvokeMemberBinder binder, object[] arguments, out object result) { - args = args + arguments = arguments .Select(a => Unwrap(a)) .ToArray(); - var method = this.type.GetMethod(binder.Name, args.Select(a => a.GetType()).ToArray()); + var argumentsTypes = arguments + .Select(a => a?.GetType() ?? typeof(object)) + .ToArray(); + + var method = this.type.GetMethod(binder.Name, argumentsTypes); try { result = method - .Invoke(this.instance, args) + .Invoke(this.instance, arguments) .Exposed(); } catch (Exception ex) @@ -100,7 +104,7 @@ public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, o throw ex.InnerException; } - throw ex; + throw; } return true; diff --git a/src/MyTested.AspNetCore.Mvc.Abstractions/Utilities/ExpressionParser.cs b/src/MyTested.AspNetCore.Mvc.Abstractions/Utilities/ExpressionParser.cs index 751137734..4715e602d 100644 --- a/src/MyTested.AspNetCore.Mvc.Abstractions/Utilities/ExpressionParser.cs +++ b/src/MyTested.AspNetCore.Mvc.Abstractions/Utilities/ExpressionParser.cs @@ -83,6 +83,8 @@ public static object ResolveExpressionValue(Expression expression) // These expressions types should be ignored and can be skipped: // - c => c.Action(With.No()) // - c => c.Action(With.Any()) + // - c => c.Action(With.Value(value)) + // - c => c.Action(With.IgnoredRouteValue(value)) // - c => c.Action(From.Services()) var expressionArgumentAsMethodCall = (MethodCallExpression)expression; var expressionMethodDeclaringType = expressionArgumentAsMethodCall.Method.DeclaringType; @@ -92,7 +94,9 @@ public static object ResolveExpressionValue(Expression expression) var expressionArgumentMethodName = expressionArgumentAsMethodCall.Method.Name; if (expressionMethodDeclaringType == TypeOfWith - && expressionArgumentMethodName == nameof(With.Any)) + && (expressionArgumentMethodName == nameof(With.Any) + || expressionArgumentMethodName == nameof(With.Value) + || expressionArgumentMethodName == nameof(With.IgnoredRouteValue))) { return IgnoredExpressionArgument; } diff --git a/src/MyTested.AspNetCore.Mvc.Abstractions/With.cs b/src/MyTested.AspNetCore.Mvc.Abstractions/With.cs index e3355f422..269874f9f 100644 --- a/src/MyTested.AspNetCore.Mvc.Abstractions/With.cs +++ b/src/MyTested.AspNetCore.Mvc.Abstractions/With.cs @@ -24,7 +24,7 @@ public static TParameter Empty() => new TParameter(); /// - /// Indicates that a argument should not be considered in method call lambda expression. + /// Indicates that an argument should not be considered in method call lambda expression. /// /// Type of parameter. /// Default value of the parameter. @@ -32,12 +32,30 @@ public static TParameter No() => default(TParameter); /// - /// Indicates that a argument should not be considered in method call lambda expression. + /// Indicates that an argument should not be considered in method call lambda expression. /// /// Type of parameter. /// Default value of the parameter. /// Using this method in route testing will indicate that the route value should be ignored during the test. public static TParameter Any() => default(TParameter); + + /// + /// Indicates that a argument should not be considered during a route test but used in the actual action call. This method is the same as + /// + /// Type of parameter. + /// The provided value. + /// Using this method in pipeline testing will indicate that the route value should be ignored during the route test but used during the action execution. + public static TParameter Value(TParameter value) + => value; + + /// + /// Indicates that a argument should not be considered during a route test but used in the actual action call. This method is the same as . + /// + /// Type of parameter. + /// The provided value. + /// Using this method in pipeline testing will indicate that the route value should be ignored during the route test but used during the action execution. + public static TParameter IgnoredRouteValue(TParameter value) + => value; } } diff --git a/src/MyTested.AspNetCore.Mvc.Authentication/MyTested.AspNetCore.Mvc.Authentication.csproj b/src/MyTested.AspNetCore.Mvc.Authentication/MyTested.AspNetCore.Mvc.Authentication.csproj index 83a78f4f1..19261d383 100644 --- a/src/MyTested.AspNetCore.Mvc.Authentication/MyTested.AspNetCore.Mvc.Authentication.csproj +++ b/src/MyTested.AspNetCore.Mvc.Authentication/MyTested.AspNetCore.Mvc.Authentication.csproj @@ -2,11 +2,11 @@ My Tested ASP.NET Core MVC authentication components. - 2015-2019 Ivaylo Kenov + 2015-2022 Ivaylo Kenov MyTested.AspNetCore.Mvc.Authentication - 3.1.3 + 6.0.0 Ivaylo Kenov - netcoreapp3.1 + net6.0 $(NoWarn);CS1591 true true diff --git a/src/MyTested.AspNetCore.Mvc.Caching/MyTested.AspNetCore.Mvc.Caching.csproj b/src/MyTested.AspNetCore.Mvc.Caching/MyTested.AspNetCore.Mvc.Caching.csproj index 5715100aa..4f2fdce96 100644 --- a/src/MyTested.AspNetCore.Mvc.Caching/MyTested.AspNetCore.Mvc.Caching.csproj +++ b/src/MyTested.AspNetCore.Mvc.Caching/MyTested.AspNetCore.Mvc.Caching.csproj @@ -2,11 +2,11 @@ My Tested ASP.NET Core MVC caching components. - 2015-2019 Ivaylo Kenov + 2015-2022 Ivaylo Kenov MyTested.AspNetCore.Mvc.Caching - 3.1.3 + 6.0.0 Ivaylo Kenov - netcoreapp3.1 + net6.0 $(NoWarn);CS1591 true true @@ -33,7 +33,7 @@ - + diff --git a/src/MyTested.AspNetCore.Mvc.Configuration/ConfigurationBuilderExtensions.cs b/src/MyTested.AspNetCore.Mvc.Configuration/ConfigurationBuilderExtensions.cs index eb1e42b79..e0355c46a 100644 --- a/src/MyTested.AspNetCore.Mvc.Configuration/ConfigurationBuilderExtensions.cs +++ b/src/MyTested.AspNetCore.Mvc.Configuration/ConfigurationBuilderExtensions.cs @@ -1,24 +1,24 @@ -namespace MyTested.AspNetCore.Mvc -{ - using System.Collections.Generic; - using Microsoft.Extensions.Configuration; - - public static class ConfigurationBuilderExtensions - { - /// - /// Adds the provided key-value pair to the configuration builder. - /// - /// The to add to. - /// The configuration key to add. - /// The configuration value to add. - /// The same . - public static IConfigurationBuilder Add( - this IConfigurationBuilder configurationBuilder, - string key, - string value) - => configurationBuilder.AddInMemoryCollection(new[] - { - new KeyValuePair(key, value) - }); - } -} +namespace MyTested.AspNetCore.Mvc +{ + using System.Collections.Generic; + using Microsoft.Extensions.Configuration; + + public static class ConfigurationBuilderExtensions + { + /// + /// Adds the provided key-value pair to the configuration builder. + /// + /// The to add to. + /// The configuration key to add. + /// The configuration value to add. + /// The same . + public static IConfigurationBuilder Add( + this IConfigurationBuilder configurationBuilder, + string key, + string value) + => configurationBuilder.AddInMemoryCollection(new[] + { + new KeyValuePair(key, value) + }); + } +} diff --git a/src/MyTested.AspNetCore.Mvc.Configuration/MyTested.AspNetCore.Mvc.Configuration.csproj b/src/MyTested.AspNetCore.Mvc.Configuration/MyTested.AspNetCore.Mvc.Configuration.csproj index 01e7a4f02..fbc120481 100644 --- a/src/MyTested.AspNetCore.Mvc.Configuration/MyTested.AspNetCore.Mvc.Configuration.csproj +++ b/src/MyTested.AspNetCore.Mvc.Configuration/MyTested.AspNetCore.Mvc.Configuration.csproj @@ -1,40 +1,40 @@  - - My Tested ASP.NET Core MVC configuration components. - 2015-2019 Ivaylo Kenov - MyTested.AspNetCore.Mvc.Configuration - 3.1.3 - Ivaylo Kenov - netcoreapp3.1 - $(NoWarn);CS1591 - true - true - MyTested.AspNetCore.Mvc.Configuration - ../../tools/Key.snk - true - true - MyTested.AspNetCore.Mvc.Configuration - aspnetcore;aspnetcoremvc;testing;unit;tests;fluent;testing;framework;asp;net;core;mvc;test;mymvc;mytested - nuget-logo.png - https://docs.mytestedasp.net/ - LICENSE - true - git - https://github.com/ivaylokenov/MyTested.AspNetCore.Mvc - false - MyTested.AspNetCore.Mvc - true - snupkg - + + My Tested ASP.NET Core MVC configuration components. + 2015-2022 Ivaylo Kenov + MyTested.AspNetCore.Mvc.Configuration + 6.0.0 + Ivaylo Kenov + net6.0 + $(NoWarn);CS1591 + true + true + MyTested.AspNetCore.Mvc.Configuration + ../../tools/Key.snk + true + true + MyTested.AspNetCore.Mvc.Configuration + aspnetcore;aspnetcoremvc;testing;unit;tests;fluent;testing;framework;asp;net;core;mvc;test;mymvc;mytested + nuget-logo.png + https://mytestedasp.net/ + LICENSE + true + git + https://github.com/ivaylokenov/MyTested.AspNetCore.Mvc + false + MyTested.AspNetCore.Mvc + true + snupkg + - - - - - - - - + + + + + + + + diff --git a/src/MyTested.AspNetCore.Mvc.Controllers.ActionResults/MyTested.AspNetCore.Mvc.Controllers.ActionResults.csproj b/src/MyTested.AspNetCore.Mvc.Controllers.ActionResults/MyTested.AspNetCore.Mvc.Controllers.ActionResults.csproj index e5a848c23..844d0f42c 100644 --- a/src/MyTested.AspNetCore.Mvc.Controllers.ActionResults/MyTested.AspNetCore.Mvc.Controllers.ActionResults.csproj +++ b/src/MyTested.AspNetCore.Mvc.Controllers.ActionResults/MyTested.AspNetCore.Mvc.Controllers.ActionResults.csproj @@ -2,11 +2,11 @@ My Tested ASP.NET Core MVC controller action result components. - 2015-2019 Ivaylo Kenov + 2015-2022 Ivaylo Kenov MyTested.AspNetCore.Mvc.Controllers.ActionResults - 3.1.3 + 6.0.0 Ivaylo Kenov - netcoreapp3.1 + net6.0 $(NoWarn);CS1591 true true diff --git a/src/MyTested.AspNetCore.Mvc.Controllers.Attributes/MyTested.AspNetCore.Mvc.Controllers.Attributes.csproj b/src/MyTested.AspNetCore.Mvc.Controllers.Attributes/MyTested.AspNetCore.Mvc.Controllers.Attributes.csproj index 3afe6bdb6..23ba66a61 100644 --- a/src/MyTested.AspNetCore.Mvc.Controllers.Attributes/MyTested.AspNetCore.Mvc.Controllers.Attributes.csproj +++ b/src/MyTested.AspNetCore.Mvc.Controllers.Attributes/MyTested.AspNetCore.Mvc.Controllers.Attributes.csproj @@ -2,11 +2,11 @@ My Tested ASP.NET Core MVC controller attribute components. - 2015-2019 Ivaylo Kenov + 2015-2022 Ivaylo Kenov MyTested.AspNetCore.Mvc.Controllers.Attributes - 3.1.3 + 6.0.0 Ivaylo Kenov - netcoreapp3.1 + net6.0 $(NoWarn);CS1591 true true diff --git a/src/MyTested.AspNetCore.Mvc.Controllers.Views.ActionResults/MyTested.AspNetCore.Mvc.Controllers.Views.ActionResults.csproj b/src/MyTested.AspNetCore.Mvc.Controllers.Views.ActionResults/MyTested.AspNetCore.Mvc.Controllers.Views.ActionResults.csproj index 1a5a679c6..ba8664dd1 100644 --- a/src/MyTested.AspNetCore.Mvc.Controllers.Views.ActionResults/MyTested.AspNetCore.Mvc.Controllers.Views.ActionResults.csproj +++ b/src/MyTested.AspNetCore.Mvc.Controllers.Views.ActionResults/MyTested.AspNetCore.Mvc.Controllers.Views.ActionResults.csproj @@ -2,11 +2,11 @@ My Tested ASP.NET Core MVC controller view action result components. - 2015-2019 Ivaylo Kenov + 2015-2022 Ivaylo Kenov MyTested.AspNetCore.Mvc.Controllers.Views.ActionResults - 3.1.3 + 6.0.0 Ivaylo Kenov - netcoreapp3.1 + net6.0 $(NoWarn);CS1591 true true diff --git a/src/MyTested.AspNetCore.Mvc.Controllers.Views/MyTested.AspNetCore.Mvc.Controllers.Views.csproj b/src/MyTested.AspNetCore.Mvc.Controllers.Views/MyTested.AspNetCore.Mvc.Controllers.Views.csproj index bcedf2ce9..af123dd28 100644 --- a/src/MyTested.AspNetCore.Mvc.Controllers.Views/MyTested.AspNetCore.Mvc.Controllers.Views.csproj +++ b/src/MyTested.AspNetCore.Mvc.Controllers.Views/MyTested.AspNetCore.Mvc.Controllers.Views.csproj @@ -2,11 +2,11 @@ My Tested ASP.NET Core MVC controller view assertion methods. - 2015-2019 Ivaylo Kenov + 2015-2022 Ivaylo Kenov MyTested.AspNetCore.Mvc.Controllers.Views - 3.1.3 + 6.0.0 Ivaylo Kenov - netcoreapp3.1 + net6.0 $(NoWarn);CS1591 true true diff --git a/src/MyTested.AspNetCore.Mvc.Controllers/MyTested.AspNetCore.Mvc.Controllers.csproj b/src/MyTested.AspNetCore.Mvc.Controllers/MyTested.AspNetCore.Mvc.Controllers.csproj index da18411ea..e26d4f128 100644 --- a/src/MyTested.AspNetCore.Mvc.Controllers/MyTested.AspNetCore.Mvc.Controllers.csproj +++ b/src/MyTested.AspNetCore.Mvc.Controllers/MyTested.AspNetCore.Mvc.Controllers.csproj @@ -2,11 +2,11 @@ My Tested ASP.NET Core MVC controller components. - 2015-2019 Ivaylo Kenov + 2015-2022 Ivaylo Kenov MyTested.AspNetCore.Mvc.Controllers - 3.1.3 + 6.0.0 Ivaylo Kenov - netcoreapp3.1 + net6.0 $(NoWarn);CS1591 true true diff --git a/src/MyTested.AspNetCore.Mvc.Core/MyTested.AspNetCore.Mvc.Core.csproj b/src/MyTested.AspNetCore.Mvc.Core/MyTested.AspNetCore.Mvc.Core.csproj index d9778c823..213c21244 100644 --- a/src/MyTested.AspNetCore.Mvc.Core/MyTested.AspNetCore.Mvc.Core.csproj +++ b/src/MyTested.AspNetCore.Mvc.Core/MyTested.AspNetCore.Mvc.Core.csproj @@ -2,11 +2,11 @@ My Tested ASP.NET Core MVC core components. - 2015-2019 Ivaylo Kenov + 2015-2022 Ivaylo Kenov MyTested.AspNetCore.Mvc.Core - 3.1.3 + 6.0.0 Ivaylo Kenov - netcoreapp3.1 + net6.0 $(NoWarn);CS1591 true true diff --git a/src/MyTested.AspNetCore.Mvc.DataAnnotations/MyTested.AspNetCore.Mvc.DataAnnotations.csproj b/src/MyTested.AspNetCore.Mvc.DataAnnotations/MyTested.AspNetCore.Mvc.DataAnnotations.csproj index 56f87b054..265b236bb 100644 --- a/src/MyTested.AspNetCore.Mvc.DataAnnotations/MyTested.AspNetCore.Mvc.DataAnnotations.csproj +++ b/src/MyTested.AspNetCore.Mvc.DataAnnotations/MyTested.AspNetCore.Mvc.DataAnnotations.csproj @@ -2,11 +2,11 @@ My Tested ASP.NET Core MVC data annotations components. - 2015-2019 Ivaylo Kenov + 2015-2022 Ivaylo Kenov MyTested.AspNetCore.Mvc.DataAnnotations - 3.1.3 + 6.0.0 Ivaylo Kenov - netcoreapp3.1 + net6.0 $(NoWarn);CS1591 true true diff --git a/src/MyTested.AspNetCore.Mvc.DependencyInjection/MyTested.AspNetCore.Mvc.DependencyInjection.csproj b/src/MyTested.AspNetCore.Mvc.DependencyInjection/MyTested.AspNetCore.Mvc.DependencyInjection.csproj index e9cceb141..e3457c186 100644 --- a/src/MyTested.AspNetCore.Mvc.DependencyInjection/MyTested.AspNetCore.Mvc.DependencyInjection.csproj +++ b/src/MyTested.AspNetCore.Mvc.DependencyInjection/MyTested.AspNetCore.Mvc.DependencyInjection.csproj @@ -2,11 +2,11 @@ My Tested ASP.NET Core MVC dependency injection components. - 2015-2019 Ivaylo Kenov + 2015-2022 Ivaylo Kenov MyTested.AspNetCore.Mvc.DependencyInjection - 3.1.3 + 6.0.0 Ivaylo Kenov - netcoreapp3.1 + net6.0 $(NoWarn);CS1591 true true diff --git a/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Builders/Contracts/Data/IWithDbContextBuilder.cs b/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Builders/Contracts/Data/IWithDbContextBuilder.cs index 7eeb2e150..b1155a4fc 100644 --- a/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Builders/Contracts/Data/IWithDbContextBuilder.cs +++ b/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Builders/Contracts/Data/IWithDbContextBuilder.cs @@ -23,7 +23,7 @@ public interface IWithDbContextBuilder /// Initial values to add to the provided . /// The same builder. IAndWithDbContextBuilder WithEntities(IEnumerable entities) - where TDbContext : DbContext; + where TDbContext : class; /// /// Sets initial values to the registered . @@ -39,7 +39,7 @@ IAndWithDbContextBuilder WithEntities(IEnumerable entities) /// Initial values to add to the provided . /// The same builder. IAndWithDbContextBuilder WithEntities(params object[] entities) - where TDbContext : DbContext; + where TDbContext : class; /// /// Sets initial values to the registered . @@ -55,7 +55,7 @@ IAndWithDbContextBuilder WithEntities(params object[] entities) /// Action setting the . /// The same builder. IAndWithDbContextBuilder WithEntities(Action dbContextSetup) - where TDbContext : DbContext; + where TDbContext : class; /// /// Sets initial values to the provided entity. @@ -74,7 +74,7 @@ IAndWithDbContextBuilder WithSet(Action> entitySetup) /// Action setting the entity. /// The same builder. IAndWithDbContextBuilder WithSet(Action> entitySetup) - where TDbContext : DbContext + where TDbContext : class where TEntity : class; } } diff --git a/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Builders/Contracts/Data/IWithDbContextTestBuilder.cs b/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Builders/Contracts/Data/IWithDbContextTestBuilder.cs index 924dda8f3..542e7a066 100644 --- a/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Builders/Contracts/Data/IWithDbContextTestBuilder.cs +++ b/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Builders/Contracts/Data/IWithDbContextTestBuilder.cs @@ -28,7 +28,7 @@ public interface IWithDbContextTestBuilder /// Type of . /// Action containing all assertions for the entities. IAndWithDbContextTestBuilder WithEntities(Action assertions) - where TDbContext : DbContext; + where TDbContext : class; /// /// Tests whether entities pass the given predicate. @@ -36,7 +36,7 @@ IAndWithDbContextTestBuilder WithEntities(Action asserti /// Type of . /// Predicate testing the entities. IAndWithDbContextTestBuilder WithEntities(Func predicate) - where TDbContext : DbContext; + where TDbContext : class; /// /// Tests whether entity passes the given assertions. @@ -61,7 +61,7 @@ IAndWithDbContextTestBuilder WithSet(Func, bool> predica /// Type of entity set. /// Action containing all assertions for the entity set. IAndWithDbContextTestBuilder WithSet(Action> assertions) - where TDbContext : DbContext + where TDbContext : class where TEntity : class; /// @@ -71,7 +71,7 @@ IAndWithDbContextTestBuilder WithSet(Action> /// Type of entity set. /// Predicate testing the entity set. IAndWithDbContextTestBuilder WithSet(Func, bool> predicate) - where TDbContext : DbContext + where TDbContext : class where TEntity : class; } } diff --git a/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Builders/Contracts/Data/IWithoutDbContextBuilder.cs b/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Builders/Contracts/Data/IWithoutDbContextBuilder.cs index 0163ae3bc..d0dde77d9 100644 --- a/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Builders/Contracts/Data/IWithoutDbContextBuilder.cs +++ b/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Builders/Contracts/Data/IWithoutDbContextBuilder.cs @@ -23,7 +23,7 @@ IAndWithoutDbContextBuilder WithoutEntityByKey(object key) /// Primary key of entity to remove from the registered . /// The same builder. IAndWithoutDbContextBuilder WithoutEntityByKey(object key) - where TDbContext : DbContext; + where TDbContext : class; /// /// Remove entities by providing their primary keys from the registered . @@ -42,7 +42,7 @@ IAndWithoutDbContextBuilder WithoutEntitiesByKeys(IEnumerable k /// Primary keys for entities to remove from the registered . /// The same builder. IAndWithoutDbContextBuilder WithoutEntitiesByKeys(IEnumerable keys) - where TDbContext : DbContext; + where TDbContext : class; /// /// Remove entity from the registered . @@ -58,7 +58,7 @@ IAndWithoutDbContextBuilder WithoutEntitiesByKeys(IEnumerab /// Entity to remove from the registered . /// The same builder. IAndWithoutDbContextBuilder WithoutEntity(object entity) - where TDbContext : DbContext; + where TDbContext : class; /// /// Remove values from the registered . @@ -74,7 +74,7 @@ IAndWithoutDbContextBuilder WithoutEntity(object entity) /// Values to remove from the registered . /// The same builder. IAndWithoutDbContextBuilder WithoutEntities(IEnumerable entities) - where TDbContext : DbContext; + where TDbContext : class; /// /// Remove values from the registered . @@ -90,7 +90,7 @@ IAndWithoutDbContextBuilder WithoutEntities(IEnumerable enti /// Values to remove from the provided . /// The same builder. IAndWithoutDbContextBuilder WithoutEntities(params object[] entities) - where TDbContext : DbContext; + where TDbContext : class; /// /// Remove values from the registered . @@ -106,7 +106,7 @@ IAndWithoutDbContextBuilder WithoutEntities(params object[] entities /// Action setting the . /// The same builder. IAndWithoutDbContextBuilder WithoutEntities(Action dbContextSetup) - where TDbContext : DbContext; + where TDbContext : class; /// /// Remove values from the provided entity. @@ -125,7 +125,7 @@ IAndWithoutDbContextBuilder WithoutSet(Action> entitySet /// Action setting the entity. /// The same builder. IAndWithoutDbContextBuilder WithoutSet(Action> entitySetup) - where TDbContext : DbContext + where TDbContext : class where TEntity : class; /// diff --git a/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Builders/Data/WithDbContextBuilder.cs b/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Builders/Data/WithDbContextBuilder.cs index fc03124a8..efaaf204c 100644 --- a/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Builders/Data/WithDbContextBuilder.cs +++ b/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Builders/Data/WithDbContextBuilder.cs @@ -29,8 +29,8 @@ public IAndWithDbContextBuilder WithEntities(IEnumerable entities) /// public IAndWithDbContextBuilder WithEntities(IEnumerable entities) - where TDbContext : DbContext - => this.WithEntities(dbContext => dbContext.AddRange(entities)); + where TDbContext : class + => this.WithEntities(dbContext => (dbContext as DbContext).AddRange(entities)); /// public IAndWithDbContextBuilder WithEntities(params object[] entities) @@ -38,7 +38,7 @@ public IAndWithDbContextBuilder WithEntities(params object[] entities) /// public IAndWithDbContextBuilder WithEntities(params object[] entities) - where TDbContext : DbContext + where TDbContext : class => this.WithEntities(entities.AsEnumerable()); /// @@ -47,14 +47,14 @@ public IAndWithDbContextBuilder WithEntities(Action dbContextSetup) /// public IAndWithDbContextBuilder WithEntities(Action dbContextSetup) - where TDbContext : DbContext + where TDbContext : class { CommonValidator.CheckForNullReference(dbContextSetup, nameof(dbContextSetup)); var dbContext = this.TestContext.GetDbContext(); dbContextSetup(dbContext); - this.SaveChangesAndCleanUp(dbContext); + this.SaveChangesAndCleanUp(dbContext as DbContext); return this; } @@ -66,12 +66,12 @@ public IAndWithDbContextBuilder WithSet(Action> entitySe /// public IAndWithDbContextBuilder WithSet(Action> entitySetup) - where TDbContext : DbContext + where TDbContext : class where TEntity : class { CommonValidator.CheckForNullReference(entitySetup, nameof(entitySetup)); - var dbContext = this.TestContext.GetDbContext(); + var dbContext = this.TestContext.GetBaseDbContext(); entitySetup(dbContext.Set()); this.SaveChangesAndCleanUp(dbContext); diff --git a/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Builders/Data/WithDbContextTestBuilder.cs b/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Builders/Data/WithDbContextTestBuilder.cs index 7d3b23205..7c1742ecf 100644 --- a/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Builders/Data/WithDbContextTestBuilder.cs +++ b/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Builders/Data/WithDbContextTestBuilder.cs @@ -24,19 +24,15 @@ public WithDbContextTestBuilder(ComponentTestContext testContext) } /// - public IAndWithDbContextTestBuilder WithEntities(Action assertions) - { - return this.WithEntities(assertions); - } + public IAndWithDbContextTestBuilder WithEntities(Action assertions) + => this.WithEntities(assertions); /// - public IAndWithDbContextTestBuilder WithEntities(Func predicate) - { - return this.WithEntities(predicate); - } + public IAndWithDbContextTestBuilder WithEntities(Func predicate) + => this.WithEntities(predicate); /// - public IAndWithDbContextTestBuilder WithEntities(Func predicate) where TDbContext : DbContext + public IAndWithDbContextTestBuilder WithEntities(Func predicate) where TDbContext : class { CommonValidator.CheckForNullReference(predicate, nameof(predicate)); @@ -52,7 +48,7 @@ public IAndWithDbContextTestBuilder WithEntities(Func - public IAndWithDbContextTestBuilder WithEntities(Action assertions) where TDbContext : DbContext + public IAndWithDbContextTestBuilder WithEntities(Action assertions) where TDbContext : class { CommonValidator.CheckForNullReference(assertions, nameof(assertions)); @@ -63,38 +59,34 @@ public IAndWithDbContextTestBuilder WithEntities(Action /// public IAndWithDbContextTestBuilder WithSet(Action> assertions) - where TEntity : class - { - return this.WithSet(assertions); - } + where TEntity : class + => this.WithSet(assertions); /// public IAndWithDbContextTestBuilder WithSet(Func, bool> predicate) - where TEntity : class - { - return this.WithSet(predicate); - } + where TEntity : class + => this.WithSet(predicate); /// public IAndWithDbContextTestBuilder WithSet(Action> assertions) - where TDbContext : DbContext + where TDbContext : class where TEntity : class { CommonValidator.CheckForNullReference(assertions, nameof(assertions)); - assertions(this.TestContext.GetDbContext().Set()); + assertions(this.TestContext.GetBaseDbContext().Set()); return this; } /// public IAndWithDbContextTestBuilder WithSet(Func, bool> predicate) - where TDbContext : DbContext + where TDbContext : class where TEntity : class { CommonValidator.CheckForNullReference(predicate, nameof(predicate)); - if (!predicate(this.TestContext.GetDbContext().Set())) + if (!predicate(this.TestContext.GetBaseDbContext().Set())) { throw new DataProviderAssertionException(string.Format( "{0} the {1} set of {2} to pass the given predicate, but it failed.", diff --git a/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Builders/Data/WithoutDbContextBuilder.cs b/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Builders/Data/WithoutDbContextBuilder.cs index 1330d1043..f0ccce730 100644 --- a/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Builders/Data/WithoutDbContextBuilder.cs +++ b/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Builders/Data/WithoutDbContextBuilder.cs @@ -30,13 +30,15 @@ public IAndWithoutDbContextBuilder WithoutEntityByKey(object key) /// public IAndWithoutDbContextBuilder WithoutEntityByKey(object key) - where TDbContext : DbContext + where TDbContext : class { - var dbContext = this.TestContext.GetDbContext(); + var dbContext = this.TestContext.GetBaseDbContext(); var entity = dbContext.Find(typeof(TEntity), key); if (entity == null) + { return this; + } dbContext.Remove(entity); dbContext.SaveChanges(); @@ -51,17 +53,18 @@ public IAndWithoutDbContextBuilder WithoutEntitiesByKeys(IEnumerable public IAndWithoutDbContextBuilder WithoutEntitiesByKeys(IEnumerable keys) - where TDbContext : DbContext + where TDbContext : class { - var dbContext = this.TestContext.GetDbContext(); + var dbContext = this.TestContext.GetBaseDbContext(); - var entityType = typeof(TEntity); var entities = keys - .Select(key => dbContext.Find(entityType, key)) + .Select(key => dbContext.Find(typeof(TEntity), key)) .Where(entity => entity != null); if (entities.Any() == false) + { return this; + } dbContext.RemoveRange(entities); dbContext.SaveChanges(); @@ -75,7 +78,7 @@ public IAndWithoutDbContextBuilder WithoutEntity(object entity) /// public IAndWithoutDbContextBuilder WithoutEntity(object entity) - where TDbContext : DbContext + where TDbContext : class => this.WithoutEntities(entity); /// @@ -84,8 +87,8 @@ public IAndWithoutDbContextBuilder WithoutEntities(IEnumerable entities) /// public IAndWithoutDbContextBuilder WithoutEntities(IEnumerable entities) - where TDbContext : DbContext - => this.WithoutEntities(dbContext => dbContext.RemoveRange(entities)); + where TDbContext : class + => this.WithoutEntities(dbContext => (dbContext as DbContext).RemoveRange(entities)); /// public IAndWithoutDbContextBuilder WithoutEntities(params object[] entities) @@ -93,7 +96,7 @@ public IAndWithoutDbContextBuilder WithoutEntities(params object[] entities) /// public IAndWithoutDbContextBuilder WithoutEntities(params object[] entities) - where TDbContext : DbContext + where TDbContext : class => this.WithoutEntities(entities.AsEnumerable()); /// @@ -105,20 +108,21 @@ public IAndWithoutDbContextBuilder WithoutAllEntities() => this.WithoutEntities(dbContext => dbContext.Database.EnsureDeleted()); /// - public IAndWithoutDbContextBuilder WithoutEntities(Action dbContextSetup) where TDbContext : DbContext + public IAndWithoutDbContextBuilder WithoutEntities(Action dbContextSetup) where TDbContext : class { CommonValidator.CheckForNullReference(dbContextSetup, nameof(dbContextSetup)); var dbContext = this.TestContext.GetDbContext(); + dbContextSetup(dbContext); try { - dbContext.SaveChanges(); + (dbContext as DbContext).SaveChanges(); } catch (DbUpdateConcurrencyException) { - // Intentional silent fail, when deleting entities that does not exist in the database or have been already deleted. + // Intentional silent fail, when deleting entities that do not exist in the database or have been already deleted. } return this; @@ -131,12 +135,13 @@ public IAndWithoutDbContextBuilder WithoutSet(Action> en /// public IAndWithoutDbContextBuilder WithoutSet(Action> entitySetup) - where TDbContext : DbContext + where TDbContext : class where TEntity : class { CommonValidator.CheckForNullReference(entitySetup, nameof(entitySetup)); - var dbContext = this.TestContext.GetDbContext(); + var dbContext = this.TestContext.GetBaseDbContext(); + entitySetup(dbContext.Set()); try @@ -145,7 +150,7 @@ public IAndWithoutDbContextBuilder WithoutSet(Action string.Empty; - + { + public string GenerateScript( + string fromMigration = null, + string toMigration = null, + MigrationsSqlGenerationOptions options = + MigrationsSqlGenerationOptions.Default) + => string.Empty; + public void Migrate(string targetMigration = null) { // intentionally left empty diff --git a/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Internal/EntityFrameworkCore/ScopedInMemoryOptionsExtension.cs b/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Internal/EntityFrameworkCore/ScopedInMemoryOptionsExtension.cs index 12a885408..b1db9d52f 100644 --- a/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Internal/EntityFrameworkCore/ScopedInMemoryOptionsExtension.cs +++ b/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Internal/EntityFrameworkCore/ScopedInMemoryOptionsExtension.cs @@ -6,8 +6,7 @@ using Microsoft.Extensions.DependencyInjection; #pragma warning disable EF1001 // Internal EF Core API usage. - public class ScopedInMemoryOptionsExtension : InMemoryOptionsExtension -#pragma warning restore EF1001 // Internal EF Core API usage. + public class ScopedInMemoryOptionsExtension : InMemoryOptionsExtension { public override void ApplyServices(IServiceCollection services) => services @@ -15,5 +14,6 @@ public override void ApplyServices(IServiceCollection services) .ReplaceLifetime(ServiceLifetime.Scoped) .ReplaceLifetime(ServiceLifetime.Scoped) .ReplaceLifetime(ServiceLifetime.Scoped); - } + } +#pragma warning restore EF1001 // Internal EF Core API usage. } diff --git a/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Internal/TestContexts/HttpTestContextEntityFrameworkExtensions.cs b/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Internal/TestContexts/HttpTestContextEntityFrameworkExtensions.cs index 61ff3f3f2..4cf3b20c2 100644 --- a/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Internal/TestContexts/HttpTestContextEntityFrameworkExtensions.cs +++ b/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Internal/TestContexts/HttpTestContextEntityFrameworkExtensions.cs @@ -2,6 +2,7 @@ { using System; using System.Linq; + using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Utilities; using Utilities.Validators; @@ -24,12 +25,23 @@ public static TDbContext GetDbContext(this HttpTestContext httpTestC if (dbContextServices.Length > 1) { - throw new InvalidOperationException($"Multiple services of type {typeof(TDbContext).ToFriendlyTypeName()} are registered in the test service provider. You should specify the DbContext class explicitly by calling '.WithData(data => data.WithEntities(dbContextSetupAction)'."); + throw new InvalidOperationException($"Multiple services of type {typeof(TDbContext).ToFriendlyTypeName()} are registered in the test service provider. You should specify the {nameof(DbContext)} class explicitly by calling '.WithData(data => data.WithEntities(dbContextSetupAction)'."); } ServiceValidator.ValidateScopedServiceLifetime(nameof(GetDbContext)); - return dbContextServices.First(); + var dbContext = dbContextServices.First(); + + if (dbContext is not DbContext) + { + throw new InvalidOperationException($"The provided service {typeof(TDbContext).ToFriendlyTypeName()} is not an instance of {nameof(DbContext)}. The resolved implementation is {dbContext.GetType().ToFriendlyTypeName()}."); + } + + return dbContext; } + + public static DbContext GetBaseDbContext(this HttpTestContext httpTestContext) + where TDbContext : class + => httpTestContext.GetDbContext() as DbContext; } } diff --git a/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/MyTested.AspNetCore.Mvc.EntityFrameworkCore.csproj b/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/MyTested.AspNetCore.Mvc.EntityFrameworkCore.csproj index 21dc97374..620972f56 100644 --- a/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/MyTested.AspNetCore.Mvc.EntityFrameworkCore.csproj +++ b/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/MyTested.AspNetCore.Mvc.EntityFrameworkCore.csproj @@ -2,11 +2,11 @@ My Tested ASP.NET Core MVC Entity Framework Core components. - 2015-2019 Ivaylo Kenov + 2015-2022 Ivaylo Kenov MyTested.AspNetCore.Mvc.EntityFrameworkCore - 3.1.3 + 6.0.0 Ivaylo Kenov - netcoreapp3.1 + net6.0 $(NoWarn);CS1591 true true @@ -33,8 +33,8 @@ - - + + diff --git a/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/ServiceCollectionEntityFrameworkCoreExtensions.cs b/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/ServiceCollectionEntityFrameworkCoreExtensions.cs index 55d94fda6..446bc97ab 100644 --- a/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/ServiceCollectionEntityFrameworkCoreExtensions.cs +++ b/src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/ServiceCollectionEntityFrameworkCoreExtensions.cs @@ -8,6 +8,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.Extensions.DependencyInjection; + using Microsoft.Extensions.DependencyInjection.Extensions; using Utilities.Extensions; /// @@ -59,20 +60,28 @@ public static IServiceCollection ReplaceDbContext(this IServiceCollection servic } AddScopedDatabaseMethodInfo - .MakeGenericMethod(existingDbContextService.ImplementationType) + .MakeGenericMethod(existingDbContextService.ServiceType, existingDbContextService.ImplementationType) .Invoke(null, new object[] { serviceCollection }); }); - + TestServiceProvider.SaveServiceLifetime(BaseDbContextType, ServiceLifetime.Scoped); return serviceCollection; } - private static void AddScopedDatabase(IServiceCollection serviceCollection) - where TDbContext : DbContext + private static void AddScopedDatabase(IServiceCollection serviceCollection) + where TDbContextImplementation : DbContext, TDbContextService { - serviceCollection.AddScoped(s => s.GetRequiredService()); - serviceCollection.AddDbContext(opts => + serviceCollection.AddScoped(s => s.GetRequiredService() as DbContext); + + if (typeof(TDbContextService) != typeof(TDbContextImplementation)) + { + serviceCollection.TryAddScoped(s => s.GetRequiredService() as TDbContextImplementation); + + TestServiceProvider.SaveServiceLifetime(ServiceLifetime.Scoped); + } + + serviceCollection.AddDbContext(opts => { opts.UseInMemoryDatabase(Guid.NewGuid().ToString()); diff --git a/src/MyTested.AspNetCore.Mvc.Helpers/MyTested.AspNetCore.Mvc.Helpers.csproj b/src/MyTested.AspNetCore.Mvc.Helpers/MyTested.AspNetCore.Mvc.Helpers.csproj index f7abad19f..2b2520d3f 100644 --- a/src/MyTested.AspNetCore.Mvc.Helpers/MyTested.AspNetCore.Mvc.Helpers.csproj +++ b/src/MyTested.AspNetCore.Mvc.Helpers/MyTested.AspNetCore.Mvc.Helpers.csproj @@ -2,11 +2,11 @@ My Tested ASP.NET Core MVC helper components. - 2015-2019 Ivaylo Kenov + 2015-2022 Ivaylo Kenov MyTested.AspNetCore.Mvc.Helpers - 3.1.3 + 6.0.0 Ivaylo Kenov - netcoreapp3.1 + net6.0 $(NoWarn);CS1591 true true diff --git a/src/MyTested.AspNetCore.Mvc.Http/MyTested.AspNetCore.Mvc.Http.csproj b/src/MyTested.AspNetCore.Mvc.Http/MyTested.AspNetCore.Mvc.Http.csproj index ce9f0c614..0e1ecdf85 100644 --- a/src/MyTested.AspNetCore.Mvc.Http/MyTested.AspNetCore.Mvc.Http.csproj +++ b/src/MyTested.AspNetCore.Mvc.Http/MyTested.AspNetCore.Mvc.Http.csproj @@ -2,11 +2,11 @@ My Tested ASP.NET Core MVC HTTP components. - 2015-2019 Ivaylo Kenov + 2015-2022 Ivaylo Kenov MyTested.AspNetCore.Mvc.Http - 3.1.3 + 6.0.0 Ivaylo Kenov - netcoreapp3.1 + net6.0 $(NoWarn);CS1591 true true diff --git a/src/MyTested.AspNetCore.Mvc.ModelState/MyTested.AspNetCore.Mvc.ModelState.csproj b/src/MyTested.AspNetCore.Mvc.ModelState/MyTested.AspNetCore.Mvc.ModelState.csproj index 0c0236192..c9e9ac77e 100644 --- a/src/MyTested.AspNetCore.Mvc.ModelState/MyTested.AspNetCore.Mvc.ModelState.csproj +++ b/src/MyTested.AspNetCore.Mvc.ModelState/MyTested.AspNetCore.Mvc.ModelState.csproj @@ -2,11 +2,11 @@ My Tested ASP.NET Core MVC model state components. - 2015-2019 Ivaylo Kenov + 2015-2022 Ivaylo Kenov MyTested.AspNetCore.Mvc.ModelState - 3.1.3 + 6.0.0 Ivaylo Kenov - netcoreapp3.1 + net6.0 $(NoWarn);CS1591 true true diff --git a/src/MyTested.AspNetCore.Mvc.Models/MyTested.AspNetCore.Mvc.Models.csproj b/src/MyTested.AspNetCore.Mvc.Models/MyTested.AspNetCore.Mvc.Models.csproj index 9447e3423..9bc39bdeb 100644 --- a/src/MyTested.AspNetCore.Mvc.Models/MyTested.AspNetCore.Mvc.Models.csproj +++ b/src/MyTested.AspNetCore.Mvc.Models/MyTested.AspNetCore.Mvc.Models.csproj @@ -2,11 +2,11 @@ My Tested ASP.NET Core MVC model components. - 2015-2019 Ivaylo Kenov + 2015-2022 Ivaylo Kenov MyTested.AspNetCore.Mvc.Models - 3.1.3 + 6.0.0 Ivaylo Kenov - netcoreapp3.1 + net6.0 $(NoWarn);CS1591 true true diff --git a/src/MyTested.AspNetCore.Mvc.Options/MyTested.AspNetCore.Mvc.Options.csproj b/src/MyTested.AspNetCore.Mvc.Options/MyTested.AspNetCore.Mvc.Options.csproj index fa08125dd..e42b8b4ca 100644 --- a/src/MyTested.AspNetCore.Mvc.Options/MyTested.AspNetCore.Mvc.Options.csproj +++ b/src/MyTested.AspNetCore.Mvc.Options/MyTested.AspNetCore.Mvc.Options.csproj @@ -2,11 +2,11 @@ My Tested ASP.NET Core MVC configuration options components. - 2015-2019 Ivaylo Kenov + 2015-2022 Ivaylo Kenov MyTested.AspNetCore.Mvc.Options - 3.1.3 + 6.0.0 Ivaylo Kenov - netcoreapp3.1 + net6.0 $(NoWarn);CS1591 true true diff --git a/src/MyTested.AspNetCore.Mvc.Options/Plugins/OptionsTestPlugin.cs b/src/MyTested.AspNetCore.Mvc.Options/Plugins/OptionsTestPlugin.cs index a48f6b4d9..bfd85e93e 100644 --- a/src/MyTested.AspNetCore.Mvc.Options/Plugins/OptionsTestPlugin.cs +++ b/src/MyTested.AspNetCore.Mvc.Options/Plugins/OptionsTestPlugin.cs @@ -6,15 +6,16 @@ public class OptionsTestPlugin : IServiceRegistrationPlugin { + private const string DefaultOptionsImplementationTypeFullName = "Microsoft.Extensions.Options.UnnamedOptionsManager`1"; + private readonly Type defaultOptionsServiceType = typeof(IOptions<>); - private readonly Type defaultOptionsImplementationType = typeof(OptionsManager<>); - + public Func ServiceSelectorPredicate => serviceDescriptor => serviceDescriptor.ServiceType == this.defaultOptionsServiceType && - serviceDescriptor.ImplementationType == this.defaultOptionsImplementationType; + serviceDescriptor.ImplementationType?.FullName == DefaultOptionsImplementationTypeFullName; - public Action ServiceRegistrationDelegate + public Action ServiceRegistrationDelegate => serviceCollection => serviceCollection.ReplaceOptions(); } } diff --git a/src/MyTested.AspNetCore.Mvc.Pipeline/MyTested.AspNetCore.Mvc.Pipeline.csproj b/src/MyTested.AspNetCore.Mvc.Pipeline/MyTested.AspNetCore.Mvc.Pipeline.csproj index ae3fdd02f..1a5129f30 100644 --- a/src/MyTested.AspNetCore.Mvc.Pipeline/MyTested.AspNetCore.Mvc.Pipeline.csproj +++ b/src/MyTested.AspNetCore.Mvc.Pipeline/MyTested.AspNetCore.Mvc.Pipeline.csproj @@ -2,11 +2,11 @@ My Tested ASP.NET Core MVC pipeline components. - 2015-2019 Ivaylo Kenov + 2015-2022 Ivaylo Kenov MyTested.AspNetCore.Mvc.Pipeline - 3.1.3 + 6.0.0 Ivaylo Kenov - netcoreapp3.1 + net6.0 $(NoWarn);CS1591 true true diff --git a/src/MyTested.AspNetCore.Mvc.Routing/MyTested.AspNetCore.Mvc.Routing.csproj b/src/MyTested.AspNetCore.Mvc.Routing/MyTested.AspNetCore.Mvc.Routing.csproj index 9716a98e4..61e8a53e8 100644 --- a/src/MyTested.AspNetCore.Mvc.Routing/MyTested.AspNetCore.Mvc.Routing.csproj +++ b/src/MyTested.AspNetCore.Mvc.Routing/MyTested.AspNetCore.Mvc.Routing.csproj @@ -2,11 +2,11 @@ My Tested ASP.NET Core MVC routing components. - 2015-2019 Ivaylo Kenov + 2015-2022 Ivaylo Kenov MyTested.AspNetCore.Mvc.Routing - 3.1.3 + 6.0.0 Ivaylo Kenov - netcoreapp3.1 + net6.0 $(NoWarn);CS1591 true true diff --git a/src/MyTested.AspNetCore.Mvc.Session/MyTested.AspNetCore.Mvc.Session.csproj b/src/MyTested.AspNetCore.Mvc.Session/MyTested.AspNetCore.Mvc.Session.csproj index fed24d9cc..2f28d1655 100644 --- a/src/MyTested.AspNetCore.Mvc.Session/MyTested.AspNetCore.Mvc.Session.csproj +++ b/src/MyTested.AspNetCore.Mvc.Session/MyTested.AspNetCore.Mvc.Session.csproj @@ -2,11 +2,11 @@ My Tested ASP.NET Core MVC session middleware components. - 2015-2019 Ivaylo Kenov + 2015-2022 Ivaylo Kenov MyTested.AspNetCore.Mvc.Session - 3.1.3 + 6.0.0 Ivaylo Kenov - netcoreapp3.1 + net6.0 $(NoWarn);CS1591 true true diff --git a/src/MyTested.AspNetCore.Mvc.TempData/MyTested.AspNetCore.Mvc.TempData.csproj b/src/MyTested.AspNetCore.Mvc.TempData/MyTested.AspNetCore.Mvc.TempData.csproj index a7dcf9e13..79e22b745 100644 --- a/src/MyTested.AspNetCore.Mvc.TempData/MyTested.AspNetCore.Mvc.TempData.csproj +++ b/src/MyTested.AspNetCore.Mvc.TempData/MyTested.AspNetCore.Mvc.TempData.csproj @@ -2,11 +2,11 @@ My Tested ASP.NET Core MVC temporary data components. - 2015-2019 Ivaylo Kenov + 2015-2022 Ivaylo Kenov MyTested.AspNetCore.Mvc.TempData - 3.1.3 + 6.0.0 Ivaylo Kenov - netcoreapp3.1 + net6.0 $(NoWarn);CS1591 true true diff --git a/src/MyTested.AspNetCore.Mvc.Universe/MyTested.AspNetCore.Mvc.Universe.csproj b/src/MyTested.AspNetCore.Mvc.Universe/MyTested.AspNetCore.Mvc.Universe.csproj index f0019753d..4e8fa6350 100644 --- a/src/MyTested.AspNetCore.Mvc.Universe/MyTested.AspNetCore.Mvc.Universe.csproj +++ b/src/MyTested.AspNetCore.Mvc.Universe/MyTested.AspNetCore.Mvc.Universe.csproj @@ -2,11 +2,11 @@ My Tested ASP.NET Core MVC is a powerful testing library providing easy fluent interface to test the ASP.NET Core MVC framework. - 2015-2019 Ivaylo Kenov + 2015-2022 Ivaylo Kenov MyTested.AspNetCore.Mvc.Universe - 3.1.3 + 6.0.0 Ivaylo Kenov - netcoreapp3.1 + net6.0 $(NoWarn);CS1591 true true @@ -34,6 +34,8 @@ + + diff --git a/src/MyTested.AspNetCore.Mvc.ViewComponents.Attributes/MyTested.AspNetCore.Mvc.ViewComponents.Attributes.csproj b/src/MyTested.AspNetCore.Mvc.ViewComponents.Attributes/MyTested.AspNetCore.Mvc.ViewComponents.Attributes.csproj index e9fe5e7cb..15c45832f 100644 --- a/src/MyTested.AspNetCore.Mvc.ViewComponents.Attributes/MyTested.AspNetCore.Mvc.ViewComponents.Attributes.csproj +++ b/src/MyTested.AspNetCore.Mvc.ViewComponents.Attributes/MyTested.AspNetCore.Mvc.ViewComponents.Attributes.csproj @@ -2,11 +2,11 @@ My Tested ASP.NET Core MVC view component attribute assertion methods. - 2015-2019 Ivaylo Kenov + 2015-2022 Ivaylo Kenov MyTested.AspNetCore.Mvc.ViewComponents.Attributes - 3.1.3 + 6.0.0 Ivaylo Kenov - netcoreapp3.1 + net6.0 $(NoWarn);CS1591 true true diff --git a/src/MyTested.AspNetCore.Mvc.ViewComponents.Results/MyTested.AspNetCore.Mvc.ViewComponents.Results.csproj b/src/MyTested.AspNetCore.Mvc.ViewComponents.Results/MyTested.AspNetCore.Mvc.ViewComponents.Results.csproj index d93c00a3d..4216c3749 100644 --- a/src/MyTested.AspNetCore.Mvc.ViewComponents.Results/MyTested.AspNetCore.Mvc.ViewComponents.Results.csproj +++ b/src/MyTested.AspNetCore.Mvc.ViewComponents.Results/MyTested.AspNetCore.Mvc.ViewComponents.Results.csproj @@ -2,11 +2,11 @@ My Tested ASP.NET Core MVC view component result assertion methods. - 2015-2019 Ivaylo Kenov + 2015-2022 Ivaylo Kenov MyTested.AspNetCore.Mvc.ViewComponents.Results - 3.1.3 + 6.0.0 Ivaylo Kenov - netcoreapp3.1 + net6.0 $(NoWarn);CS1591 true true diff --git a/src/MyTested.AspNetCore.Mvc.ViewComponents/MyTested.AspNetCore.Mvc.ViewComponents.csproj b/src/MyTested.AspNetCore.Mvc.ViewComponents/MyTested.AspNetCore.Mvc.ViewComponents.csproj index 31eebab6f..86968f32f 100644 --- a/src/MyTested.AspNetCore.Mvc.ViewComponents/MyTested.AspNetCore.Mvc.ViewComponents.csproj +++ b/src/MyTested.AspNetCore.Mvc.ViewComponents/MyTested.AspNetCore.Mvc.ViewComponents.csproj @@ -2,11 +2,11 @@ My Tested ASP.NET Core MVC view components assertion methods. - 2015-2019 Ivaylo Kenov + 2015-2022 Ivaylo Kenov MyTested.AspNetCore.Mvc.ViewComponents - 3.1.3 + 6.0.0 Ivaylo Kenov - netcoreapp3.1 + net6.0 $(NoWarn);CS1591 true true diff --git a/src/MyTested.AspNetCore.Mvc.ViewData/MyTested.AspNetCore.Mvc.ViewData.csproj b/src/MyTested.AspNetCore.Mvc.ViewData/MyTested.AspNetCore.Mvc.ViewData.csproj index 877da47f7..0ac93eb5c 100644 --- a/src/MyTested.AspNetCore.Mvc.ViewData/MyTested.AspNetCore.Mvc.ViewData.csproj +++ b/src/MyTested.AspNetCore.Mvc.ViewData/MyTested.AspNetCore.Mvc.ViewData.csproj @@ -2,11 +2,11 @@ My Tested ASP.NET Core MVC view data components. - 2015-2019 Ivaylo Kenov + 2015-2022 Ivaylo Kenov MyTested.AspNetCore.Mvc.ViewData - 3.1.3 + 6.0.0 Ivaylo Kenov - netcoreapp3.1 + net6.0 $(NoWarn);CS1591 true true diff --git a/src/MyTested.AspNetCore.Mvc.ViewFeatures/MyTested.AspNetCore.Mvc.ViewFeatures.csproj b/src/MyTested.AspNetCore.Mvc.ViewFeatures/MyTested.AspNetCore.Mvc.ViewFeatures.csproj index d819ac72d..45366f9b6 100644 --- a/src/MyTested.AspNetCore.Mvc.ViewFeatures/MyTested.AspNetCore.Mvc.ViewFeatures.csproj +++ b/src/MyTested.AspNetCore.Mvc.ViewFeatures/MyTested.AspNetCore.Mvc.ViewFeatures.csproj @@ -2,11 +2,11 @@ My Tested ASP.NET Core MVC view features components. - 2015-2019 Ivaylo Kenov + 2015-2022 Ivaylo Kenov MyTested.AspNetCore.Mvc.ViewFeatures - 3.1.3 + 6.0.0 Ivaylo Kenov - netcoreapp3.1 + net6.0 $(NoWarn);CS1591 true true diff --git a/src/MyTested.AspNetCore.Mvc/MyTested.AspNetCore.Mvc.csproj b/src/MyTested.AspNetCore.Mvc/MyTested.AspNetCore.Mvc.csproj index d8c777731..218ebbe4c 100644 --- a/src/MyTested.AspNetCore.Mvc/MyTested.AspNetCore.Mvc.csproj +++ b/src/MyTested.AspNetCore.Mvc/MyTested.AspNetCore.Mvc.csproj @@ -2,11 +2,11 @@ My Tested ASP.NET Core MVC is a powerful testing library providing easy fluent interface to test the ASP.NET Core MVC framework. - 2015-2019 Ivaylo Kenov + 2015-2022 Ivaylo Kenov MyTested.AspNetCore.Mvc - 3.1.3 + 6.0.0 Ivaylo Kenov - netcoreapp3.1 + net6.0 $(NoWarn);CS1591 true true diff --git a/test/MyTested.AspNetCore.Mvc.Abstractions.Test/MyTested.AspNetCore.Mvc.Abstractions.Test.csproj b/test/MyTested.AspNetCore.Mvc.Abstractions.Test/MyTested.AspNetCore.Mvc.Abstractions.Test.csproj index a3bd4fc18..fc27b9a39 100644 --- a/test/MyTested.AspNetCore.Mvc.Abstractions.Test/MyTested.AspNetCore.Mvc.Abstractions.Test.csproj +++ b/test/MyTested.AspNetCore.Mvc.Abstractions.Test/MyTested.AspNetCore.Mvc.Abstractions.Test.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 true MyTested.AspNetCore.Mvc.Test ../../tools/Key.snk @@ -26,8 +26,11 @@ - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/test/MyTested.AspNetCore.Mvc.Abstractions.Test/ServicesTests.cs b/test/MyTested.AspNetCore.Mvc.Abstractions.Test/ServicesTests.cs index c332e6b21..04c3464d7 100644 --- a/test/MyTested.AspNetCore.Mvc.Abstractions.Test/ServicesTests.cs +++ b/test/MyTested.AspNetCore.Mvc.Abstractions.Test/ServicesTests.cs @@ -34,6 +34,21 @@ public class ServicesTests { + [Fact] + public void ExceptionDuringStartupServiceRegistrationShouldThrowProperErrorMessage() + { + Test.AssertException( + () => + { + MyApplication.StartsFrom(); + + TestServiceProvider.GetService(WebFramework.Internals.MvcMarkerService); + }, + "Test application could not be initialized. You may need to create a custom mock for one of your registered services. If you are having difficulties debugging this error, open an issue at https://github.com/ivaylokenov/MyTested.AspNetCore.Mvc/issues. Provide your Startup classes and this exception message: 'Exception during service registration.'."); + + MyApplication.StartsFrom(); + } + [Fact] public void UsesDefaultServicesShouldPopulateDefaultServices() { @@ -235,7 +250,7 @@ public void IsUsingShouldRecreateServicesEveryTimeItIsInvoked() } [Fact] - public void DefaultConfigShouldSetMvc() + public void DefaultConfigurationShouldSetMvc() { MyApplication.StartsFrom(); @@ -245,7 +260,7 @@ public void DefaultConfigShouldSetMvc() } [Fact] - public void DefaultConfigShouldSetDefaultRoutes() + public void DefaultConfigurationShouldSetDefaultRoutes() { MyApplication.StartsFrom(); @@ -256,7 +271,7 @@ public void DefaultConfigShouldSetDefaultRoutes() } [Fact] - public void DefaultConfigAndAdditionalServicesShouldWorkCorrectly() + public void DefaultConfigurationAndAdditionalServicesShouldWorkCorrectly() { MyApplication .StartsFrom() @@ -273,7 +288,7 @@ public void DefaultConfigAndAdditionalServicesShouldWorkCorrectly() } [Fact] - public void DefaultConfigAndAdditionalApplicationShouldWorkCorrectly() + public void DefaultConfigurationAndAdditionalApplicationShouldWorkCorrectly() { var set = false; @@ -289,7 +304,7 @@ public void DefaultConfigAndAdditionalApplicationShouldWorkCorrectly() } [Fact] - public void DefaultConfigAndAdditionalRoutesShouldSetOnlyThem() + public void DefaultConfigurationAndAdditionalRoutesShouldSetOnlyThem() { MyApplication .StartsFrom() @@ -307,7 +322,7 @@ public void DefaultConfigAndAdditionalRoutesShouldSetOnlyThem() } [Fact] - public void DefaultConfigAndAdditionalRoutesShouldSetOnlyThemWithoutEndpoints() + public void DefaultConfigurationAndAdditionalRoutesShouldSetOnlyThemWithoutEndpoints() { MyApplication .StartsFrom() @@ -707,7 +722,7 @@ public void ConfigureContainerWithNoServerServicesShouldThrowCorrectExceptionMes // This call ensures services are loaded (uses lazy loading). var setupServices = TestApplication.Services; }, - "No service for type 'Microsoft.Extensions.DependencyInjection.IServiceProviderFactory`1[MyTested.AspNetCore.Mvc.Test.Setups.Common.CustomContainer]' has been registered. Services could not be configured. If your web project is registering services outside of the Startup class (during the WebHost configuration in the Program.cs file for example), you should provide them to the test framework too by calling 'IsRunningOn(server => server.WithServices(servicesAction))'. Since this method should be called only once per test project, you may invoke it in the static constructor of your TestStartup class or if your test runner supports it - in the test assembly initialization."); + "An exception with the following message was thrown during initialization: 'No service for type 'Microsoft.Extensions.DependencyInjection.IServiceProviderFactory`1[MyTested.AspNetCore.Mvc.Test.Setups.Common.CustomContainer]' has been registered.'. Services could not be configured. If your web project is registering services outside of the Startup class (during the WebHost configuration in the Program.cs file for example), you should provide them to the test framework too by calling 'IsRunningOn(server => server.WithServices(servicesAction))'. Since this method should be called only once per test project, you may invoke it in the static constructor of your TestStartup class or if your test runner supports it - in the test assembly initialization."); MyApplication.StartsFrom(); } @@ -857,6 +872,6 @@ public void StartupFiltersShouldBeRegisteredAndConsidered() Assert.True(sameStartupFilter.Registered); MyApplication.StartsFrom(); - } + } } } diff --git a/test/MyTested.AspNetCore.Mvc.Abstractions.Test/UtilitiesTests/ReflectionTests.cs b/test/MyTested.AspNetCore.Mvc.Abstractions.Test/UtilitiesTests/ReflectionTests.cs index 26e929357..6f9ae53d1 100644 --- a/test/MyTested.AspNetCore.Mvc.Abstractions.Test/UtilitiesTests/ReflectionTests.cs +++ b/test/MyTested.AspNetCore.Mvc.Abstractions.Test/UtilitiesTests/ReflectionTests.cs @@ -1025,7 +1025,7 @@ public void AreDeeplyEqualShouldReportCorrectlyWithPrimitiveAndStructTypes() Assert.False(Reflection.AreDeeplyEqual(1, "1", out result)); Assert.Equal("Expected a value of Int32 type, but in fact it was String", result.ToString()); Assert.False(Reflection.AreDeeplyEqual(new DateTime(2015, 10, 19), new DateTime(2015, 10, 20), out result)); - Assert.Equal("Difference occurs at 'DateTime.== (Equality Operator)'. Expected a value of '10/19/2015 12:00:00 AM', but in fact it was '10/20/2015 12:00:00 AM'", result.ToString()); + Assert.Equal($"Difference occurs at 'DateTime.== (Equality Operator)'. Expected a value of '{new DateTime(2015, 10, 19)}', but in fact it was '{new DateTime(2015, 10, 20)}'", result.ToString()); } [Fact] diff --git a/test/MyTested.AspNetCore.Mvc.Authentication.Test/MyTested.AspNetCore.Mvc.Authentication.Test.csproj b/test/MyTested.AspNetCore.Mvc.Authentication.Test/MyTested.AspNetCore.Mvc.Authentication.Test.csproj index 797b4a2f5..d325354d0 100644 --- a/test/MyTested.AspNetCore.Mvc.Authentication.Test/MyTested.AspNetCore.Mvc.Authentication.Test.csproj +++ b/test/MyTested.AspNetCore.Mvc.Authentication.Test/MyTested.AspNetCore.Mvc.Authentication.Test.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 true MyTested.AspNetCore.Mvc.Test ../../tools/Key.snk @@ -28,8 +28,11 @@ - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/test/MyTested.AspNetCore.Mvc.Caching.Test/BuildersTests/ActionsTests/ShouldHaveTests/ShouldHaveMemoryCacheTests.cs b/test/MyTested.AspNetCore.Mvc.Caching.Test/BuildersTests/ActionsTests/ShouldHaveTests/ShouldHaveMemoryCacheTests.cs index d1c0d9973..bfb5175b9 100644 --- a/test/MyTested.AspNetCore.Mvc.Caching.Test/BuildersTests/ActionsTests/ShouldHaveTests/ShouldHaveMemoryCacheTests.cs +++ b/test/MyTested.AspNetCore.Mvc.Caching.Test/BuildersTests/ActionsTests/ShouldHaveTests/ShouldHaveMemoryCacheTests.cs @@ -268,7 +268,8 @@ public void MemoryCacheWithBuilderShouldThrowWithMemoryCacheEntryOptionsAndIncor [Fact] public void MemoryCacheWithBuilderShouldThrowWithMemoryCacheEntryOptionsWithInvalidAbsoluteExpiration() { - var invalidExpirationDate = new DateTime(2017, 1, 1, 1, 1, 1, DateTimeKind.Utc); + var invalidExpirationDate = new DateTimeOffset(new DateTime(2017, 1, 1, 1, 1, 1, DateTimeKind.Utc)); + var actualExpirationDate = new DateTimeOffset(new DateTime(2016, 1, 1, 1, 1, 1, DateTimeKind.Utc)); Test.AssertException( () => @@ -280,7 +281,7 @@ public void MemoryCacheWithBuilderShouldThrowWithMemoryCacheEntryOptionsWithInva .MemoryCache(cache => cache .ContainingEntry("test", "value", new MemoryCacheEntryOptions { - AbsoluteExpiration = new DateTimeOffset(invalidExpirationDate), + AbsoluteExpiration = invalidExpirationDate, AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(1), Priority = CacheItemPriority.High, SlidingExpiration = TimeSpan.FromMinutes(5) @@ -289,7 +290,7 @@ public void MemoryCacheWithBuilderShouldThrowWithMemoryCacheEntryOptionsWithInva .ShouldReturn() .Ok(); }, - "When calling AddMemoryCacheAction action in MvcController expected memory cache to have entry with the given options, but in fact they were different. Difference occurs at 'MemoryCacheEntryOptions.AbsoluteExpiration.== (Equality Operator)'. Expected a value of '1/1/2017 1:01:01 AM +00:00', but in fact it was '1/1/2016 1:01:01 AM +00:00'."); + $"When calling AddMemoryCacheAction action in MvcController expected memory cache to have entry with the given options, but in fact they were different. Difference occurs at 'MemoryCacheEntryOptions.AbsoluteExpiration.== (Equality Operator)'. Expected a value of '{invalidExpirationDate}', but in fact it was '{actualExpirationDate}'."); } [Fact] diff --git a/test/MyTested.AspNetCore.Mvc.Caching.Test/BuildersTests/InvocationsTests/ShouldHaveTests/ShouldHaveMemoryCacheTests.cs b/test/MyTested.AspNetCore.Mvc.Caching.Test/BuildersTests/InvocationsTests/ShouldHaveTests/ShouldHaveMemoryCacheTests.cs index 539c21906..0f79f5959 100644 --- a/test/MyTested.AspNetCore.Mvc.Caching.Test/BuildersTests/InvocationsTests/ShouldHaveTests/ShouldHaveMemoryCacheTests.cs +++ b/test/MyTested.AspNetCore.Mvc.Caching.Test/BuildersTests/InvocationsTests/ShouldHaveTests/ShouldHaveMemoryCacheTests.cs @@ -242,7 +242,8 @@ public void MemoryCacheWithBuilderShouldThrowWithMemoryCacheEntryOptionsAndIncor [Fact] public void MemoryCacheWithBuilderShouldThrowWithMemoryCacheEntryOptionsWithInvalidAbsoluteExpiration() { - var invalidExpirationDate = new DateTime(2017, 1, 1, 1, 1, 1, DateTimeKind.Utc); + var invalidExpirationDate = new DateTimeOffset(new DateTime(2017, 1, 1, 1, 1, 1, DateTimeKind.Utc)); + var actualExpirationDate = new DateTimeOffset(new DateTime(2016, 1, 1, 1, 1, 1, DateTimeKind.Utc)); Test.AssertException( () => @@ -253,7 +254,7 @@ public void MemoryCacheWithBuilderShouldThrowWithMemoryCacheEntryOptionsWithInva .MemoryCache(cache => cache .ContainingEntry("test", "value", new MemoryCacheEntryOptions { - AbsoluteExpiration = new DateTimeOffset(invalidExpirationDate), + AbsoluteExpiration = invalidExpirationDate, AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(1), Priority = CacheItemPriority.High, SlidingExpiration = TimeSpan.FromMinutes(5) @@ -262,7 +263,7 @@ public void MemoryCacheWithBuilderShouldThrowWithMemoryCacheEntryOptionsWithInva .ShouldReturn() .View(); }, - "When invoking MemoryCacheValuesComponent expected memory cache to have entry with the given options, but in fact they were different. Difference occurs at 'MemoryCacheEntryOptions.AbsoluteExpiration.== (Equality Operator)'. Expected a value of '1/1/2017 1:01:01 AM +00:00', but in fact it was '1/1/2016 1:01:01 AM +00:00'."); + $"When invoking MemoryCacheValuesComponent expected memory cache to have entry with the given options, but in fact they were different. Difference occurs at 'MemoryCacheEntryOptions.AbsoluteExpiration.== (Equality Operator)'. Expected a value of '{invalidExpirationDate}', but in fact it was '{actualExpirationDate}'."); } [Fact] diff --git a/test/MyTested.AspNetCore.Mvc.Caching.Test/MyTested.AspNetCore.Mvc.Caching.Test.csproj b/test/MyTested.AspNetCore.Mvc.Caching.Test/MyTested.AspNetCore.Mvc.Caching.Test.csproj index ee5d27b4b..5fa17c92c 100644 --- a/test/MyTested.AspNetCore.Mvc.Caching.Test/MyTested.AspNetCore.Mvc.Caching.Test.csproj +++ b/test/MyTested.AspNetCore.Mvc.Caching.Test/MyTested.AspNetCore.Mvc.Caching.Test.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 true MyTested.AspNetCore.Mvc.Test ../../tools/Key.snk @@ -29,8 +29,11 @@ - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/test/MyTested.AspNetCore.Mvc.Configuration.Test/MyTested.AspNetCore.Mvc.Configuration.Test.csproj b/test/MyTested.AspNetCore.Mvc.Configuration.Test/MyTested.AspNetCore.Mvc.Configuration.Test.csproj index e524c0ba2..8b1f20afc 100644 --- a/test/MyTested.AspNetCore.Mvc.Configuration.Test/MyTested.AspNetCore.Mvc.Configuration.Test.csproj +++ b/test/MyTested.AspNetCore.Mvc.Configuration.Test/MyTested.AspNetCore.Mvc.Configuration.Test.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 true MyTested.AspNetCore.Mvc.Test ../../tools/Key.snk @@ -19,9 +19,12 @@ - + - + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + \ No newline at end of file diff --git a/test/MyTested.AspNetCore.Mvc.Controllers.ActionResults.Test/MyTested.AspNetCore.Mvc.Controllers.ActionResults.Test.csproj b/test/MyTested.AspNetCore.Mvc.Controllers.ActionResults.Test/MyTested.AspNetCore.Mvc.Controllers.ActionResults.Test.csproj index 8b2e1d90c..40d82015b 100644 --- a/test/MyTested.AspNetCore.Mvc.Controllers.ActionResults.Test/MyTested.AspNetCore.Mvc.Controllers.ActionResults.Test.csproj +++ b/test/MyTested.AspNetCore.Mvc.Controllers.ActionResults.Test/MyTested.AspNetCore.Mvc.Controllers.ActionResults.Test.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 true MyTested.AspNetCore.Mvc.Test ../../tools/Key.snk @@ -27,8 +27,11 @@ - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/test/MyTested.AspNetCore.Mvc.Controllers.Attributes.Test/MyTested.AspNetCore.Mvc.Controllers.Attributes.Test.csproj b/test/MyTested.AspNetCore.Mvc.Controllers.Attributes.Test/MyTested.AspNetCore.Mvc.Controllers.Attributes.Test.csproj index ca29a74fd..fee32ba7f 100644 --- a/test/MyTested.AspNetCore.Mvc.Controllers.Attributes.Test/MyTested.AspNetCore.Mvc.Controllers.Attributes.Test.csproj +++ b/test/MyTested.AspNetCore.Mvc.Controllers.Attributes.Test/MyTested.AspNetCore.Mvc.Controllers.Attributes.Test.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 true MyTested.AspNetCore.Mvc.Test ../../tools/Key.snk @@ -15,9 +15,12 @@ - + - + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/test/MyTested.AspNetCore.Mvc.Controllers.Test/MyTested.AspNetCore.Mvc.Controllers.Test.csproj b/test/MyTested.AspNetCore.Mvc.Controllers.Test/MyTested.AspNetCore.Mvc.Controllers.Test.csproj index b4cfb0dc3..8be96d3fb 100644 --- a/test/MyTested.AspNetCore.Mvc.Controllers.Test/MyTested.AspNetCore.Mvc.Controllers.Test.csproj +++ b/test/MyTested.AspNetCore.Mvc.Controllers.Test/MyTested.AspNetCore.Mvc.Controllers.Test.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 true MyTested.AspNetCore.Mvc.Test ../../tools/Key.snk @@ -26,8 +26,11 @@ - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/test/MyTested.AspNetCore.Mvc.Controllers.Views.ActionResults.Test/MyTested.AspNetCore.Mvc.Controllers.Views.ActionResults.Test.csproj b/test/MyTested.AspNetCore.Mvc.Controllers.Views.ActionResults.Test/MyTested.AspNetCore.Mvc.Controllers.Views.ActionResults.Test.csproj index 4a7dac27f..b6090421b 100644 --- a/test/MyTested.AspNetCore.Mvc.Controllers.Views.ActionResults.Test/MyTested.AspNetCore.Mvc.Controllers.Views.ActionResults.Test.csproj +++ b/test/MyTested.AspNetCore.Mvc.Controllers.Views.ActionResults.Test/MyTested.AspNetCore.Mvc.Controllers.Views.ActionResults.Test.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 true MyTested.AspNetCore.Mvc.Test ../../tools/Key.snk @@ -27,8 +27,11 @@ - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/test/MyTested.AspNetCore.Mvc.Controllers.Views.Test/MyTested.AspNetCore.Mvc.Controllers.Views.Test.csproj b/test/MyTested.AspNetCore.Mvc.Controllers.Views.Test/MyTested.AspNetCore.Mvc.Controllers.Views.Test.csproj index 90c9fab5a..c806a2f7e 100644 --- a/test/MyTested.AspNetCore.Mvc.Controllers.Views.Test/MyTested.AspNetCore.Mvc.Controllers.Views.Test.csproj +++ b/test/MyTested.AspNetCore.Mvc.Controllers.Views.Test/MyTested.AspNetCore.Mvc.Controllers.Views.Test.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 MyTested.AspNetCore.Mvc.Test ../../tools/Key.snk true @@ -26,8 +26,11 @@ - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/test/MyTested.AspNetCore.Mvc.DataAnnotations.Test/MyTested.AspNetCore.Mvc.DataAnnotations.Test.csproj b/test/MyTested.AspNetCore.Mvc.DataAnnotations.Test/MyTested.AspNetCore.Mvc.DataAnnotations.Test.csproj index 0acfb0b16..774567b0f 100644 --- a/test/MyTested.AspNetCore.Mvc.DataAnnotations.Test/MyTested.AspNetCore.Mvc.DataAnnotations.Test.csproj +++ b/test/MyTested.AspNetCore.Mvc.DataAnnotations.Test/MyTested.AspNetCore.Mvc.DataAnnotations.Test.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 true MyTested.AspNetCore.Mvc.Test ../../tools/Key.snk @@ -26,8 +26,11 @@ - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/test/MyTested.AspNetCore.Mvc.DataAnnotations.Test/PluginsTests/DataAnnotationsTestPluginTests.cs b/test/MyTested.AspNetCore.Mvc.DataAnnotations.Test/PluginsTests/DataAnnotationsTestPluginTests.cs index be672e3c9..b7d657852 100644 --- a/test/MyTested.AspNetCore.Mvc.DataAnnotations.Test/PluginsTests/DataAnnotationsTestPluginTests.cs +++ b/test/MyTested.AspNetCore.Mvc.DataAnnotations.Test/PluginsTests/DataAnnotationsTestPluginTests.cs @@ -34,9 +34,6 @@ public void ShouldInvokeMethodOfTypeVoidWithValidServiceCollection() testPlugin.DefaultServiceRegistrationDelegate(serviceCollection); - var methodReturnType = testPlugin.DefaultServiceRegistrationDelegate.Method.ReturnType.Name; - - Assert.True(methodReturnType == "Void"); Assert.Contains(serviceCollection, s => s.ServiceType == typeof(IValidationAttributeAdapterProvider)); } } diff --git a/test/MyTested.AspNetCore.Mvc.DependencyInjection.Test/MyTested.AspNetCore.Mvc.DependencyInjection.Test.csproj b/test/MyTested.AspNetCore.Mvc.DependencyInjection.Test/MyTested.AspNetCore.Mvc.DependencyInjection.Test.csproj index 978e1868f..92e277ca3 100644 --- a/test/MyTested.AspNetCore.Mvc.DependencyInjection.Test/MyTested.AspNetCore.Mvc.DependencyInjection.Test.csproj +++ b/test/MyTested.AspNetCore.Mvc.DependencyInjection.Test/MyTested.AspNetCore.Mvc.DependencyInjection.Test.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 true MyTested.AspNetCore.Mvc.Test ../../tools/Key.snk @@ -29,8 +29,11 @@ - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/BuildersTests/ActionsTests/ShouldHaveTests/ShouldHaveDbContextTests.cs b/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/BuildersTests/ActionsTests/ShouldHaveTests/ShouldHaveDbContextTests.cs index 4aad5b0fa..6f536d940 100644 --- a/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/BuildersTests/ActionsTests/ShouldHaveTests/ShouldHaveDbContextTests.cs +++ b/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/BuildersTests/ActionsTests/ShouldHaveTests/ShouldHaveDbContextTests.cs @@ -1,5 +1,6 @@ namespace MyTested.AspNetCore.Mvc.Test.BuildersTests.ActionsTests.ShouldHaveTests { + using System; using Exceptions; using Microsoft.Extensions.DependencyInjection; using Microsoft.EntityFrameworkCore; @@ -15,19 +16,50 @@ public void DbContextShouldNotThrowExceptionWithCorrectAssertions() { MyApplication .StartsFrom() - .WithServices(services => - { - services.AddDbContext(options => options.UseInMemoryDatabase(TestObjectFactory.TestDatabaseName)); - }); + .WithServices(services => services + .AddDbContext(options => options.UseInMemoryDatabase(TestObjectFactory.TestDatabaseName))); + + MyController + .Instance() + .Calling(c => c.Create(new CustomModel { Id = 1, Name = "Test" })) + .ShouldHave() + .Data(dbContext => dbContext + .WithEntities(db => + { + Assert.NotNull(db.Models.FirstOrDefaultAsync(m => m.Id == 1)); + })) + .AndAlso() + .ShouldReturn() + .Ok(); + + MyApplication.StartsFrom(); + } + + [Fact] + public void DbContextShouldNotThrowExceptionWithCorrectAssertionsThroughInterface() + { + MyApplication + .StartsFrom() + .WithServices(services => services + .AddDbContext(options => options + .UseInMemoryDatabase(TestObjectFactory.TestDatabaseName))); MyController .Instance() .Calling(c => c.Create(new CustomModel { Id = 1, Name = "Test" })) .ShouldHave() .Data(dbContext => dbContext + .WithEntities(db => + { + Assert.NotNull(db.Models.FirstOrDefaultAsync(m => m.Id == 1)); + }) .WithEntities(db => { Assert.NotNull(db.Models.FirstOrDefaultAsync(m => m.Id == 1)); + }) + .WithEntities(db => + { + Assert.NotNull(db.Set().FirstOrDefaultAsync(m => m.Id == 1)); })) .AndAlso() .ShouldReturn() @@ -35,16 +67,15 @@ public void DbContextShouldNotThrowExceptionWithCorrectAssertions() MyApplication.StartsFrom(); } - + [Fact] public void DbContextShouldNotThrowExceptionWithCorrectPredicate() { MyApplication .StartsFrom() - .WithServices(services => - { - services.AddDbContext(options => options.UseInMemoryDatabase(TestObjectFactory.TestDatabaseName)); - }); + .WithServices(services => services + .AddDbContext(options => options + .UseInMemoryDatabase(TestObjectFactory.TestDatabaseName))); MyController .Instance() @@ -60,15 +91,72 @@ public void DbContextShouldNotThrowExceptionWithCorrectPredicate() } [Fact] - public void DbContextShouldThrowExceptionWithIncorrectPredicate() + public void DbContextShouldNotThrowExceptionWithCorrectPredicateThroughInterface() + { + MyApplication + .StartsFrom() + .WithServices(services => services + .AddDbContext(options => options + .UseInMemoryDatabase(TestObjectFactory.TestDatabaseName))); + + MyController + .Instance() + .Calling(c => c.Create(new CustomModel { Id = 1, Name = "Test" })) + .ShouldHave() + .Data(dbContext => dbContext + .WithEntities(db => db.Models.FirstOrDefaultAsync(m => m.Id == 1) != null) + .WithEntities(db => db.Models.FirstOrDefaultAsync(m => m.Id == 1) != null) + .WithEntities(db => db.Set().FirstOrDefaultAsync(m => m.Id == 1) != null)) + .AndAlso() + .ShouldReturn() + .Ok(); + + MyApplication.StartsFrom(); + } + + [Fact] + public void DbContextShouldThrowExceptionWithCorrectPredicateThroughInvalidInterface() { MyApplication .StartsFrom() .WithServices(services => { - services.AddDbContext(options => options.UseInMemoryDatabase(TestObjectFactory.TestDatabaseName)); + services + .AddDbContext(options => options + .UseInMemoryDatabase(TestObjectFactory.TestDatabaseName)); + + services.AddScoped(); }); + Test.AssertException(() => + { + MyController + .Instance() + .Calling(c => c.Create(new CustomModel { Id = 1, Name = "Test" })) + .ShouldHave() + .Data(dbContext => dbContext + .WithEntities(db => db.Models.FirstOrDefaultAsync(m => m.Id == 1) != null) + .WithEntities(db => db.Models.FirstOrDefaultAsync(m => m.Id == 1) != null) + .WithEntities(db => db.Set().FirstOrDefaultAsync(m => m.Id == 1) != null) + .WithEntities(db => db.Models.FirstOrDefaultAsync(m => m.Id == 1) != null)) + .AndAlso() + .ShouldReturn() + .Ok(); + }, + "The provided service IInvalidDbContext is not an instance of DbContext. The resolved implementation is InvalidDbContext."); + + MyApplication.StartsFrom(); + } + + [Fact] + public void DbContextShouldThrowExceptionWithIncorrectPredicate() + { + MyApplication + .StartsFrom() + .WithServices(services => services + .AddDbContext(options => options + .UseInMemoryDatabase(TestObjectFactory.TestDatabaseName))); + MyController .Instance() .Calling(c => c.Create(new CustomModel { Id = 1, Name = "Test" })) @@ -101,10 +189,9 @@ public void DbContextWithSetShouldNotThrowExceptionWithCorrectAssertions() { MyApplication .StartsFrom() - .WithServices(services => - { - services.AddDbContext(options => options.UseInMemoryDatabase(TestObjectFactory.TestDatabaseName)); - }); + .WithServices(services => services + .AddDbContext(options => options + .UseInMemoryDatabase(TestObjectFactory.TestDatabaseName))); MyController .Instance() @@ -122,15 +209,39 @@ public void DbContextWithSetShouldNotThrowExceptionWithCorrectAssertions() MyApplication.StartsFrom(); } + [Fact] + public void DbContextWithSetShouldNotThrowExceptionWithCorrectAssertionsThroughInterface() + { + MyApplication + .StartsFrom() + .WithServices(services => services + .AddDbContext(options => options + .UseInMemoryDatabase(TestObjectFactory.TestDatabaseName))); + + MyController + .Instance() + .Calling(c => c.Create(new CustomModel { Id = 1, Name = "Test" })) + .ShouldHave() + .Data(dbContext => dbContext + .WithSet(set => + { + Assert.NotNull(set.FirstOrDefaultAsync(m => m.Id == 1)); + })) + .AndAlso() + .ShouldReturn() + .Ok(); + + MyApplication.StartsFrom(); + } + [Fact] public void DbContextWithSetShouldNotThrowExceptionWithCorrectPredicate() { MyApplication .StartsFrom() - .WithServices(services => - { - services.AddDbContext(options => options.UseInMemoryDatabase(TestObjectFactory.TestDatabaseName)); - }); + .WithServices(services => services + .AddDbContext(options => options + .UseInMemoryDatabase(TestObjectFactory.TestDatabaseName))); MyController .Instance() @@ -145,15 +256,36 @@ public void DbContextWithSetShouldNotThrowExceptionWithCorrectPredicate() MyApplication.StartsFrom(); } + [Fact] + public void DbContextWithSetShouldNotThrowExceptionWithCorrectPredicateThroughInterface() + { + MyApplication + .StartsFrom() + .WithServices(services => services + .AddDbContext(options => options + .UseInMemoryDatabase(TestObjectFactory.TestDatabaseName))); + + MyController + .Instance() + .Calling(c => c.Create(new CustomModel { Id = 1, Name = "Test" })) + .ShouldHave() + .Data(dbContext => dbContext + .WithSet(set => set.FirstOrDefaultAsync(m => m.Id == 1) != null)) + .AndAlso() + .ShouldReturn() + .Ok(); + + MyApplication.StartsFrom(); + } + [Fact] public void DbContextWithSetShouldThrowExceptionWithIncorrectPredicate() { MyApplication .StartsFrom() - .WithServices(services => - { - services.AddDbContext(options => options.UseInMemoryDatabase(TestObjectFactory.TestDatabaseName)); - }); + .WithServices(services => services + .AddDbContext(options => options + .UseInMemoryDatabase(TestObjectFactory.TestDatabaseName))); MyController .Instance() diff --git a/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/BuildersTests/ControllersTests/ControllerBuilderTests.cs b/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/BuildersTests/ControllersTests/ControllerBuilderTests.cs index 93af3bfa3..31305758f 100644 --- a/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/BuildersTests/ControllersTests/ControllerBuilderTests.cs +++ b/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/BuildersTests/ControllersTests/ControllerBuilderTests.cs @@ -12,6 +12,9 @@ public class ControllerBuilderTests { + private const string TestConnectionString = "Server=(localdb)\\MSSQLLocalDB;Database=TestDb;Trusted_Connection=True;MultipleActiveResultSets=true;Connect Timeout=30;"; + private const string AnotherConnectionString = "Server=(localdb)\\MSSQLLocalDB;Database=AnotherTestDb;Trusted_Connection=True;MultipleActiveResultSets=true;Connect Timeout=30;"; + [Fact] public void WithDataShouldSetupDbContext() { @@ -19,8 +22,9 @@ public void WithDataShouldSetupDbContext() .StartsFrom() .WithServices(services => { - services.AddDbContext(options => - options.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=TestDb;Trusted_Connection=True;MultipleActiveResultSets=true;Connect Timeout=30;")); + services + .AddDbContext(options => options + .UseSqlServer(TestConnectionString)); }); MyController @@ -46,8 +50,9 @@ public void WithEntitiesShouldSetupDbContext() .StartsFrom() .WithServices(services => { - services.AddDbContext(options => - options.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=TestDb;Trusted_Connection=True;MultipleActiveResultSets=true;Connect Timeout=30;")); + services + .AddDbContext(options => options + .UseSqlServer(TestConnectionString)); }); MyController @@ -139,6 +144,107 @@ public void WithEntitiesShouldSetupDbContext() MyApplication.StartsFrom(); } + [Fact] + public void WithEntitiesShouldSetupDbContextThroughInterface() + { + MyApplication + .StartsFrom() + .WithServices(services => + { + services + .AddDbContext(options => options + .UseSqlServer(TestConnectionString)); + }); + + MyController + .Instance() + .WithData(data => data + .WithEntities(db => db + .Models.Add(new CustomModel + { + Id = 1, + Name = "Test" + }))) + .Calling(c => c.Get(1)) + .ShouldReturn() + .Ok(ok => ok + .WithModelOfType() + .Passing(m => m.Name == "Test")); + + MyController + .Instance() + .WithData(data => data + .WithEntities(db => db.Add(new CustomModel + { + Id = 1, + Name = "Test" + }))) + .Calling(c => c.Get(1)) + .ShouldReturn() + .Ok(ok => ok + .WithModelOfType() + .Passing(m => m.Name == "Test")); + + MyController + .Instance() + .WithData(data => data + .WithEntities( + new CustomModel + { + Id = 1, + Name = "Test 1" + }, + new CustomModel + { + Id = 2, + Name = "Test 2" + })) + .Calling(c => c.Get(1)) + .ShouldReturn() + .Ok(ok => ok + .WithModelOfType() + .Passing(m => m.Name == "Test 1")); + + MyController + .Instance() + .WithData(data => data + .WithEntities(db => db + .Models.Add(new CustomModel + { + Id = 2, + Name = "Test" + }))) + .Calling(c => c.Get(1)) + .ShouldReturn() + .NotFound(); + + MyController + .Instance() + .WithData(data => data + .WithEntities( + new CustomModel + { + Id = 2, + Name = "Test 2" + }, + new CustomModel + { + Id = 3, + Name = "Test 3" + })) + .Calling(c => c.Get(1)) + .ShouldReturn() + .NotFound(); + + MyController + .Instance() + .Calling(c => c.Get(1)) + .ShouldReturn() + .NotFound(); + + MyApplication.StartsFrom(); + } + [Fact] public void WithSetShouldSetupDbContext() { @@ -146,8 +252,9 @@ public void WithSetShouldSetupDbContext() .StartsFrom() .WithServices(services => { - services.AddDbContext(options => - options.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=TestDb;Trusted_Connection=True;MultipleActiveResultSets=true;Connect Timeout=30;")); + services + .AddDbContext(options => options + .UseSqlServer(TestConnectionString)); }); MyController @@ -187,6 +294,55 @@ public void WithSetShouldSetupDbContext() MyApplication.StartsFrom(); } + [Fact] + public void WithSetShouldSetupDbContextThroughInterface() + { + MyApplication + .StartsFrom() + .WithServices(services => + { + services + .AddDbContext(options => options + .UseSqlServer(TestConnectionString)); + }); + + MyController + .Instance() + .WithData(data => data + .WithSet(set => set + .Add(new CustomModel + { + Id = 1, + Name = "Test" + }))) + .Calling(c => c.Get(1)) + .ShouldReturn() + .Ok(ok => ok + .WithModelOfType() + .Passing(m => m.Name == "Test")); + + MyController + .Instance() + .WithData(data => data + .WithSet(set => set + .Add(new CustomModel + { + Id = 2, + Name = "Test" + }))) + .Calling(c => c.Get(1)) + .ShouldReturn() + .NotFound(); + + MyController + .Instance() + .Calling(c => c.Get(1)) + .ShouldReturn() + .NotFound(); + + MyApplication.StartsFrom(); + } + [Fact] public void WithEntitiesShouldSetupMultipleDbContext() { @@ -194,11 +350,13 @@ public void WithEntitiesShouldSetupMultipleDbContext() .StartsFrom() .WithServices(services => { - services.AddDbContext(options => - options.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=TestDb;Trusted_Connection=True;MultipleActiveResultSets=true;Connect Timeout=30;")); + services + .AddDbContext(options => options + .UseSqlServer(TestConnectionString)); - services.AddDbContext(options => - options.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=AnotherTestDb;Trusted_Connection=True;MultipleActiveResultSets=true;Connect Timeout=30;")); + services + .AddDbContext(options => options + .UseSqlServer(AnotherConnectionString)); }); var modelName = "Test"; @@ -297,11 +455,13 @@ public void WithSetShouldSetupMultipleDbContext() .StartsFrom() .WithServices(services => { - services.AddDbContext(options => - options.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=TestDb;Trusted_Connection=True;MultipleActiveResultSets=true;Connect Timeout=30;")); + services + .AddDbContext(options => options + .UseSqlServer(TestConnectionString)); - services.AddDbContext(options => - options.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=AnotherTestDb;Trusted_Connection=True;MultipleActiveResultSets=true;Connect Timeout=30;")); + services + .AddDbContext(options => options + .UseSqlServer(AnotherConnectionString)); }); var modelName = "Test"; @@ -413,11 +573,13 @@ public void WithDataThrowCorrectExceptionWhenMultipleDbContextsAreRegisteredAndD .StartsFrom() .WithServices(services => { - services.AddDbContext(options => - options.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=TestDb;Trusted_Connection=True;MultipleActiveResultSets=true;Connect Timeout=30;")); + services + .AddDbContext(options => options + .UseSqlServer(TestConnectionString)); - services.AddDbContext(options => - options.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=AnotherTestDb;Trusted_Connection=True;MultipleActiveResultSets=true;Connect Timeout=30;")); + services + .AddDbContext(options => options + .UseSqlServer(AnotherConnectionString)); }); Test.AssertException(() => @@ -534,8 +696,8 @@ public void WithoutDataThrowsExceptionWhenNullIsProvided() () => { MyApplication - .StartsFrom() - .WithServices(services => services.AddDbContext()); + .StartsFrom() + .WithServices(services => services.AddDbContext()); var model = new CustomModel { diff --git a/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/BuildersTests/InvocationsTests/ShouldHaveTests/ShouldHaveDbContextTests.cs b/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/BuildersTests/InvocationsTests/ShouldHaveTests/ShouldHaveDbContextTests.cs index 289f9b444..adf465f51 100644 --- a/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/BuildersTests/InvocationsTests/ShouldHaveTests/ShouldHaveDbContextTests.cs +++ b/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/BuildersTests/InvocationsTests/ShouldHaveTests/ShouldHaveDbContextTests.cs @@ -17,7 +17,9 @@ public void DbContextShouldNotThrowExceptionWithCorrectAssertions() .StartsFrom() .WithServices(services => { - services.AddDbContext(options => options.UseInMemoryDatabase(TestObjectFactory.TestDatabaseName)); + services + .AddDbContext(options => options + .UseInMemoryDatabase(TestObjectFactory.TestDatabaseName)); }); MyViewComponent @@ -35,6 +37,41 @@ public void DbContextShouldNotThrowExceptionWithCorrectAssertions() MyApplication.StartsFrom(); } + [Fact] + public void DbContextShouldNotThrowExceptionWithCorrectAssertionsThroughInterface() + { + MyApplication + .StartsFrom() + .WithServices(services => + { + services + .AddDbContext(options => options + .UseInMemoryDatabase(TestObjectFactory.TestDatabaseName)); + }); + + MyViewComponent + .InvokedWith(c => c.Invoke(new CustomModel { Id = 1, Name = "Test" })) + .ShouldHave() + .Data(dbContext => dbContext + .WithEntities(db => + { + Assert.NotNull(db.Models.FirstOrDefaultAsync(m => m.Id == 1)); + }) + .WithEntities(db => + { + Assert.NotNull(db.Models.FirstOrDefaultAsync(m => m.Id == 1)); + }) + .WithEntities(db => + { + Assert.NotNull(db.Set().FirstOrDefaultAsync(m => m.Id == 1)); + })) + .AndAlso() + .ShouldReturn() + .View(); + + MyApplication.StartsFrom(); + } + [Fact] public void DbContextShouldNotThrowExceptionWithCorrectPredicate() { @@ -42,7 +79,9 @@ public void DbContextShouldNotThrowExceptionWithCorrectPredicate() .StartsFrom() .WithServices(services => { - services.AddDbContext(options => options.UseInMemoryDatabase(TestObjectFactory.TestDatabaseName)); + services + .AddDbContext(options => options + .UseInMemoryDatabase(TestObjectFactory.TestDatabaseName)); }); MyViewComponent @@ -57,6 +96,32 @@ public void DbContextShouldNotThrowExceptionWithCorrectPredicate() MyApplication.StartsFrom(); } + [Fact] + public void DbContextShouldNotThrowExceptionWithCorrectPredicateThroughInterface() + { + MyApplication + .StartsFrom() + .WithServices(services => + { + services + .AddDbContext(options => options + .UseInMemoryDatabase(TestObjectFactory.TestDatabaseName)); + }); + + MyViewComponent + .InvokedWith(c => c.Invoke(new CustomModel { Id = 1, Name = "Test" })) + .ShouldHave() + .Data(dbContext => dbContext + .WithEntities(db => db.Models.FirstOrDefaultAsync(m => m.Id == 1) != null) + .WithEntities(db => db.Models.FirstOrDefaultAsync(m => m.Id == 1) != null) + .WithEntities(db => db.Set().FirstOrDefaultAsync(m => m.Id == 1) != null)) + .AndAlso() + .ShouldReturn() + .View(); + + MyApplication.StartsFrom(); + } + [Fact] public void DbContextShouldThrowExceptionWithIncorrectPredicate() { @@ -64,7 +129,9 @@ public void DbContextShouldThrowExceptionWithIncorrectPredicate() .StartsFrom() .WithServices(services => { - services.AddDbContext(options => options.UseInMemoryDatabase(TestObjectFactory.TestDatabaseName)); + services + .AddDbContext(options => options + .UseInMemoryDatabase(TestObjectFactory.TestDatabaseName)); }); MyViewComponent @@ -91,5 +158,46 @@ public void DbContextShouldThrowExceptionWithIncorrectPredicate() MyApplication.StartsFrom(); } + + [Fact] + public void DbContextShouldThrowExceptionWithIncorrectPredicateThroughInterface() + { + MyApplication + .StartsFrom() + .WithServices(services => + { + services + .AddDbContext(options => options + .UseInMemoryDatabase(TestObjectFactory.TestDatabaseName)); + }); + + MyViewComponent + .InvokedWith(c => c.Invoke(new CustomModel { Id = 1, Name = "Test" })) + .ShouldHave() + .Data(dbContext => dbContext + .WithEntities(db => db.Models.FirstOrDefaultAsync(m => m.Id == 1) != null) + .WithEntities(db => db.Models.FirstOrDefaultAsync(m => m.Id == 1) != null) + .WithEntities(db => db.Set().FirstOrDefaultAsync(m => m.Id == 1) != null)) + .AndAlso() + .ShouldReturn() + .View(); + + Test.AssertException(() => + { + MyViewComponent + .InvokedWith(c => c.Invoke(new CustomModel { Id = 2, Name = "Test" })) + .ShouldHave() + .Data(dbContext => dbContext + .WithEntities(db => db.Models.FirstOrDefaultAsync(m => m.Id == 1) == null) + .WithEntities(db => db.Models.FirstOrDefaultAsync(m => m.Id == 1) == null) + .WithEntities(db => db.Set().FirstOrDefaultAsync(m => m.Id == 1) == null)) + .AndAlso() + .ShouldReturn() + .View(); + }, + "When invoking CreateDataComponent expected the ICustomDbContext entities to pass the given predicate, but it failed."); + + MyApplication.StartsFrom(); + } } } diff --git a/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/BuildersTests/ViewComponentsTests/ViewComponentBuilderTests.cs b/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/BuildersTests/ViewComponentsTests/ViewComponentBuilderTests.cs index 3cb8d708b..ec1ae579e 100644 --- a/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/BuildersTests/ViewComponentsTests/ViewComponentBuilderTests.cs +++ b/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/BuildersTests/ViewComponentsTests/ViewComponentBuilderTests.cs @@ -70,6 +70,67 @@ public void WithEntitiesShouldSetupDbContext() MyApplication.StartsFrom(); } + [Fact] + public void WithEntitiesShouldSetupDbContextThroughInterface() + { + MyApplication + .StartsFrom() + .WithServices(services => + { + services.AddDbContext(options => + options.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=TestDb;Trusted_Connection=True;MultipleActiveResultSets=true;Connect Timeout=30;")); + }); + + MyViewComponent + .Instance() + .WithData(data => data + .WithEntities(db => db + .Models.Add(new CustomModel + { + Id = 1, + Name = "Test" + }))) + .InvokedWith(c => c.Invoke(1)) + .ShouldReturn() + .View(view => view + .WithModelOfType() + .Passing(m => m.Name == "Test")); + + MyViewComponent + .Instance() + .WithData(data => data + .WithEntities(db => db.Add(new CustomModel + { + Id = 1, + Name = "Test" + }))) + .InvokedWith(c => c.Invoke(1)) + .ShouldReturn() + .View(view => view + .WithModelOfType() + .Passing(m => m.Name == "Test")); + + MyViewComponent + .Instance() + .WithData(data => data + .WithEntities(db => db + .Models.Add(new CustomModel + { + Id = 2, + Name = "Test" + }))) + .InvokedWith(c => c.Invoke(1)) + .ShouldReturn() + .Content("Invalid"); + + MyViewComponent + .InvokedWith(c => c.Invoke(1)) + .ShouldReturn() + .Content("Invalid"); + + MyApplication.StartsFrom(); + } + [Fact] public void WithSetShouldSetupDbContext() { @@ -116,5 +177,52 @@ public void WithSetShouldSetupDbContext() MyApplication.StartsFrom(); } + + [Fact] + public void WithSetShouldSetupDbContextThroughInterface() + { + MyApplication + .StartsFrom() + .WithServices(services => + { + services.AddDbContext(options => + options.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=TestDb;Trusted_Connection=True;MultipleActiveResultSets=true;Connect Timeout=30;")); + }); + + MyViewComponent + .Instance() + .WithData(data => data + .WithSet(set => set + .Add(new CustomModel + { + Id = 1, + Name = "Test" + }))) + .InvokedWith(c => c.Invoke(1)) + .ShouldReturn() + .View(view => view + .WithModelOfType() + .Passing(m => m.Name == "Test")); + + MyViewComponent + .Instance() + .WithData(data => data + .WithSet(set => set + .Add(new CustomModel + { + Id = 2, + Name = "Test" + }))) + .InvokedWith(c => c.Invoke(1)) + .ShouldReturn() + .Content("Invalid"); + + MyViewComponent + .InvokedWith(c => c.Invoke(1)) + .ShouldReturn() + .Content("Invalid"); + + MyApplication.StartsFrom(); + } } } diff --git a/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test.csproj b/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test.csproj index 36c60ec0d..9e28b2ab7 100644 --- a/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test.csproj +++ b/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 true MyTested.AspNetCore.Mvc.Test ../../tools/Key.snk @@ -29,10 +29,13 @@ - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + - + diff --git a/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/PluginsTests/EntityFrameworkCoreTestPluginTests.cs b/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/PluginsTests/EntityFrameworkCoreTestPluginTests.cs index 09291b927..f2498aa86 100644 --- a/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/PluginsTests/EntityFrameworkCoreTestPluginTests.cs +++ b/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/PluginsTests/EntityFrameworkCoreTestPluginTests.cs @@ -27,9 +27,6 @@ public void ShouldInvokeMethodOfTypeVoidWithValidServiceCollection() testPlugin.ServiceRegistrationDelegate(serviceCollection); - var methodReturnType = testPlugin.ServiceRegistrationDelegate.Method.ReturnType.Name; - - Assert.True(methodReturnType == "Void"); Assert.Contains(serviceCollection, s => s.ServiceType == typeof(DbContextOptions)); } } diff --git a/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/ServicesTests.cs b/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/ServicesTests.cs index f83d1fb80..44472c0a5 100644 --- a/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/ServicesTests.cs +++ b/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/ServicesTests.cs @@ -1,116 +1,162 @@ -namespace MyTested.AspNetCore.Mvc.Test -{ - using System.Linq; - using Internal.EntityFrameworkCore; - using Microsoft.EntityFrameworkCore; - using Microsoft.EntityFrameworkCore.Infrastructure; - using Microsoft.EntityFrameworkCore.InMemory.Infrastructure.Internal; - using Microsoft.Extensions.DependencyInjection; - using Setups; - using Setups.Common; - using Xunit; - - public class ServicesTests - { - [Fact] - public void ReplaceDbContextShouldReplaceNonInMemoryDatabaseWithInMemoryScopedOne() - { - var services = new ServiceCollection(); - - this.AddDbContextWithSqlServer(services); - - services.ReplaceDbContext(); - - this.AssertCorrectDbContextAndOptions(services); - } - - [Fact] - public void ReplaceDbContextShouldReplaceInMemoryDatabaseWithInMemoryScopedOne() - { - var services = new ServiceCollection(); - - services.AddDbContext(options => options.UseInMemoryDatabase(TestObjectFactory.TestDatabaseName)); - - services.ReplaceDbContext(); - - this.AssertCorrectDbContextAndOptions(services); - } - - [Fact] - public void ReplaceDbContextShouldNotAddDbContextIfMissing() - { - var services = new ServiceCollection(); - - services.ReplaceDbContext(); - - var serviceProvider = services.BuildServiceProvider(); - - Assert.Null(serviceProvider.GetService()); - } - - [Fact] - public void ReplaceDbContextShouldReplaceMultipleDbContextTypes() - { - var services = new ServiceCollection(); - - this.AddDbContextWithSqlServer(services); - this.AddDbContextWithSqlServer(services); - - services.ReplaceDbContext(); - - this.AssertCorrectDbContextAndOptions(services); - this.AssertCorrectDbContextAndOptions(services); - } - - [Fact] - public void CallingMigrateShouldNotThrowExceptionWithInMemoryDatabase() - { - var services = new ServiceCollection(); - - this.AddDbContextWithSqlServer(services); - - services.ReplaceDbContext(); - - services.BuildServiceProvider().GetRequiredService().Database.Migrate(); - } - - private void AddDbContextWithSqlServer(IServiceCollection services) - => this.AddDbContextWithSqlServer(services); - - private void AddDbContextWithSqlServer(IServiceCollection services) - where TDbContext : DbContext - => services.AddDbContext(options => - options.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=TestDb;Trusted_Connection=True;MultipleActiveResultSets=true;Connect Timeout=30;")); - - private void AssertCorrectDbContextAndOptions(IServiceCollection services) - => this.AssertCorrectDbContextAndOptions(services); - - private void AssertCorrectDbContextAndOptions(IServiceCollection services) - where TDbContext : DbContext - { - var serviceProvider = services.BuildServiceProvider(); - - var dbContextService = services.FirstOrDefault(s => s.ServiceType == typeof(TDbContext)); - - Assert.NotNull(dbContextService); - Assert.Equal(ServiceLifetime.Scoped, dbContextService.Lifetime); - - var customDbContext = serviceProvider.GetService(); - - Assert.NotNull(customDbContext); - - var dbContextOptions = serviceProvider.GetService>(); - - Assert.NotNull(dbContextOptions); - Assert.Equal(3, dbContextOptions.Extensions.Count()); - - var coreOptionsExtension = dbContextOptions.FindExtension(); - var inMemoryOptionsExtension = dbContextOptions.FindExtension(); - var scopedInMemoryOptionsExtension = dbContextOptions.FindExtension(); - - Assert.NotNull(coreOptionsExtension); - Assert.NotNull(inMemoryOptionsExtension); - Assert.NotNull(scopedInMemoryOptionsExtension); - } - } -} +namespace MyTested.AspNetCore.Mvc.Test +{ + using System.Linq; + using Internal.EntityFrameworkCore; + using Microsoft.EntityFrameworkCore; + using Microsoft.EntityFrameworkCore.Infrastructure; + using Microsoft.EntityFrameworkCore.InMemory.Infrastructure.Internal; + using Microsoft.Extensions.DependencyInjection; + using Setups; + using Setups.Common; + using Xunit; + + public class ServicesTests + { + private const string TestConnectionString = "Server=(localdb)\\MSSQLLocalDB;Database=TestDb;Trusted_Connection=True;MultipleActiveResultSets=true;Connect Timeout=30;"; + + [Fact] + public void ReplaceDbContextShouldReplaceNonInMemoryDatabaseWithInMemoryScopedOne() + { + var services = new ServiceCollection(); + + this.AddDbContextWithSqlServer(services); + + services.ReplaceDbContext(); + + this.AssertCorrectDbContextAndOptions(services); + } + + [Fact] + public void ReplaceDbContextShouldReplaceInMemoryDatabaseWithInMemoryScopedOne() + { + var services = new ServiceCollection(); + + services.AddDbContext(options => options.UseInMemoryDatabase(TestObjectFactory.TestDatabaseName)); + + services.ReplaceDbContext(); + + this.AssertCorrectDbContextAndOptions(services); + } + + [Fact] + public void ReplaceDbContextShouldNotAddDbContextIfMissing() + { + var services = new ServiceCollection(); + + services.ReplaceDbContext(); + + var serviceProvider = services.BuildServiceProvider(); + + Assert.Null(serviceProvider.GetService()); + } + + [Fact] + public void ReplaceDbContextShouldReplaceMultipleDbContextTypes() + { + var services = new ServiceCollection(); + + this.AddDbContextWithSqlServer(services); + this.AddDbContextWithSqlServer(services); + + services.ReplaceDbContext(); + + this.AssertCorrectDbContextAndOptions(services); + this.AssertCorrectDbContextAndOptions(services); + } + + [Fact] + public void ReplaceDbContextWithInterfaceShouldReplaceDbContextCorrectly() + { + var services = new ServiceCollection(); + + services.AddDbContext(options => options.UseSqlServer(TestConnectionString)); + + services.ReplaceDbContext(); + + var baseDbContextService = services.FirstOrDefault(s => s.ServiceType == typeof(DbContext)); + var classDbContextService = services.FirstOrDefault(s => s.ServiceType == typeof(CustomDbContext)); + var interfaceDbContextService = services.FirstOrDefault(s => s.ServiceType == typeof(ICustomDbContext)); + + Assert.NotNull(baseDbContextService); + Assert.NotNull(classDbContextService); + Assert.NotNull(interfaceDbContextService); + + Assert.Equal(ServiceLifetime.Scoped, baseDbContextService.Lifetime); + Assert.Equal(ServiceLifetime.Scoped, classDbContextService.Lifetime); + Assert.Equal(ServiceLifetime.Scoped, interfaceDbContextService.Lifetime); + + var serviceProvider = services.BuildServiceProvider(); + + var baseDbContext = serviceProvider.GetService(); + var classDbContext = serviceProvider.GetService(); + var interfaceDbContext = serviceProvider.GetService(); + + Assert.NotNull(baseDbContext); + Assert.NotNull(classDbContext); + Assert.NotNull(interfaceDbContext); + + baseDbContext.Add(new CustomModel()); + baseDbContext.SaveChanges(); + + baseDbContext = serviceProvider.GetService(); + classDbContext = serviceProvider.GetService(); + interfaceDbContext = serviceProvider.GetService(); + + Assert.Single(baseDbContext.Set()); + Assert.Single(classDbContext.Models); + Assert.Single(interfaceDbContext.Models); + } + + [Fact] + public void CallingMigrateShouldNotThrowExceptionWithInMemoryDatabase() + { + var services = new ServiceCollection(); + + this.AddDbContextWithSqlServer(services); + + services.ReplaceDbContext(); + + services.BuildServiceProvider().GetRequiredService().Database.Migrate(); + } + + private void AddDbContextWithSqlServer(IServiceCollection services) + => this.AddDbContextWithSqlServer(services); + + private void AddDbContextWithSqlServer(IServiceCollection services) + where TDbContext : DbContext + => services.AddDbContext(options => options.UseSqlServer(TestConnectionString)); + + private void AssertCorrectDbContextAndOptions(IServiceCollection services) + => this.AssertCorrectDbContextAndOptions(services); + + private void AssertCorrectDbContextAndOptions(IServiceCollection services) + where TDbContext : DbContext + { + var serviceProvider = services.BuildServiceProvider(); + + var dbContextService = services.FirstOrDefault(s => s.ServiceType == typeof(TDbContext)); + + Assert.NotNull(dbContextService); + Assert.Equal(ServiceLifetime.Scoped, dbContextService.Lifetime); + + var customDbContext = serviceProvider.GetService(); + + Assert.NotNull(customDbContext); + + var dbContextOptions = serviceProvider.GetService>(); + + Assert.NotNull(dbContextOptions); + Assert.Equal(3, dbContextOptions.Extensions.Count()); + +#pragma warning disable EF1001 // Internal EF Core API usage. + var coreOptionsExtension = dbContextOptions.FindExtension(); + var inMemoryOptionsExtension = dbContextOptions.FindExtension(); + var scopedInMemoryOptionsExtension = dbContextOptions.FindExtension(); +#pragma warning restore EF1001 // Internal EF Core API usage. + + Assert.NotNull(coreOptionsExtension); + Assert.NotNull(inMemoryOptionsExtension); + Assert.NotNull(scopedInMemoryOptionsExtension); + } + } +} diff --git a/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/Setups/Common/CustomDbContext.cs b/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/Setups/Common/CustomDbContext.cs index a28084244..28dd0ab2c 100644 --- a/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/Setups/Common/CustomDbContext.cs +++ b/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/Setups/Common/CustomDbContext.cs @@ -2,7 +2,7 @@ { using Microsoft.EntityFrameworkCore; - public class CustomDbContext : DbContext + public class CustomDbContext : DbContext, ICustomDbContext { public CustomDbContext(DbContextOptions options) : base(options) diff --git a/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/Setups/Common/ICustomDbContext.cs b/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/Setups/Common/ICustomDbContext.cs new file mode 100644 index 000000000..08cf5a185 --- /dev/null +++ b/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/Setups/Common/ICustomDbContext.cs @@ -0,0 +1,9 @@ +namespace MyTested.AspNetCore.Mvc.Test.Setups.Common +{ + using Microsoft.EntityFrameworkCore; + + public interface ICustomDbContext + { + DbSet Models { get; set; } + } +} diff --git a/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/Setups/Common/IInvalidDbContext.cs b/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/Setups/Common/IInvalidDbContext.cs new file mode 100644 index 000000000..8b738fb5f --- /dev/null +++ b/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/Setups/Common/IInvalidDbContext.cs @@ -0,0 +1,9 @@ +namespace MyTested.AspNetCore.Mvc.Test.Setups.Common +{ + using Microsoft.EntityFrameworkCore; + + public interface IInvalidDbContext + { + DbSet Models { get; set; } + } +} diff --git a/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/Setups/Common/InvalidDbContext.cs b/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/Setups/Common/InvalidDbContext.cs new file mode 100644 index 000000000..6776b0195 --- /dev/null +++ b/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/Setups/Common/InvalidDbContext.cs @@ -0,0 +1,9 @@ +namespace MyTested.AspNetCore.Mvc.Test.Setups.Common +{ + using Microsoft.EntityFrameworkCore; + + public class InvalidDbContext : IInvalidDbContext + { + public DbSet Models { get; set; } + } +} diff --git a/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/Setups/Controllers/DbContextController.cs b/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/Setups/Controllers/DbContextController.cs index d5307e52e..989fa9329 100644 --- a/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/Setups/Controllers/DbContextController.cs +++ b/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/Setups/Controllers/DbContextController.cs @@ -8,10 +8,7 @@ public class DbContextController : Controller { private readonly CustomDbContext data; - public DbContextController(CustomDbContext data) - { - this.data = data; - } + public DbContextController(CustomDbContext data) => this.data = data; public IActionResult Create(CustomModel model) { diff --git a/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/Setups/Controllers/InterfaceDbContextController.cs b/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/Setups/Controllers/InterfaceDbContextController.cs new file mode 100644 index 000000000..183a76fde --- /dev/null +++ b/test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/Setups/Controllers/InterfaceDbContextController.cs @@ -0,0 +1,41 @@ +namespace MyTested.AspNetCore.Mvc.Test.Setups.Controllers +{ + using System.Linq; + using Common; + using Microsoft.AspNetCore.Mvc; + + public class InterfaceDbContextController : Controller + { + private readonly ICustomDbContext data; + + public InterfaceDbContextController(ICustomDbContext data) => this.data = data; + + public IActionResult Create(CustomModel model) + { + this.data.Models.Add(model); + return this.Ok(); + } + + public IActionResult Get(int id) + { + var model = this.data.Models.FirstOrDefault(m => m.Id == id); + if (model == null) + { + return this.NotFound(); + } + + return this.Ok(model); + } + + public IActionResult GetAll() + { + var models = this.data.Models; + if (models == null || !models.Any()) + { + return this.NotFound(); + } + + return this.Ok(models.ToList()); + } + } +} diff --git a/test/MyTested.AspNetCore.Mvc.Helpers.Test/MyTested.AspNetCore.Mvc.Helpers.Test.csproj b/test/MyTested.AspNetCore.Mvc.Helpers.Test/MyTested.AspNetCore.Mvc.Helpers.Test.csproj index d465955fd..17bf9a929 100644 --- a/test/MyTested.AspNetCore.Mvc.Helpers.Test/MyTested.AspNetCore.Mvc.Helpers.Test.csproj +++ b/test/MyTested.AspNetCore.Mvc.Helpers.Test/MyTested.AspNetCore.Mvc.Helpers.Test.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 true MyTested.AspNetCore.Mvc.Test ../../tools/Key.snk @@ -26,8 +26,11 @@ - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/test/MyTested.AspNetCore.Mvc.Http.Test/MyTested.AspNetCore.Mvc.Http.Test.csproj b/test/MyTested.AspNetCore.Mvc.Http.Test/MyTested.AspNetCore.Mvc.Http.Test.csproj index 5c7a1109d..f573b8c7a 100644 --- a/test/MyTested.AspNetCore.Mvc.Http.Test/MyTested.AspNetCore.Mvc.Http.Test.csproj +++ b/test/MyTested.AspNetCore.Mvc.Http.Test/MyTested.AspNetCore.Mvc.Http.Test.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 true MyTested.AspNetCore.Mvc.Test ../../tools/Key.snk @@ -28,8 +28,11 @@ - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/test/MyTested.AspNetCore.Mvc.ModelState.Test/MyTested.AspNetCore.Mvc.ModelState.Test.csproj b/test/MyTested.AspNetCore.Mvc.ModelState.Test/MyTested.AspNetCore.Mvc.ModelState.Test.csproj index 6fef10719..c9f5f4e46 100644 --- a/test/MyTested.AspNetCore.Mvc.ModelState.Test/MyTested.AspNetCore.Mvc.ModelState.Test.csproj +++ b/test/MyTested.AspNetCore.Mvc.ModelState.Test/MyTested.AspNetCore.Mvc.ModelState.Test.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 true MyTested.AspNetCore.Mvc.Test ../../tools/Key.snk @@ -29,8 +29,11 @@ - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/test/MyTested.AspNetCore.Mvc.ModelState.Test/PluginsTests/ModelStateTestPluginTests.cs b/test/MyTested.AspNetCore.Mvc.ModelState.Test/PluginsTests/ModelStateTestPluginTests.cs index e718351a2..b945dad6a 100644 --- a/test/MyTested.AspNetCore.Mvc.ModelState.Test/PluginsTests/ModelStateTestPluginTests.cs +++ b/test/MyTested.AspNetCore.Mvc.ModelState.Test/PluginsTests/ModelStateTestPluginTests.cs @@ -34,9 +34,6 @@ public void ShouldInvokeMethodOfTypeVoidWithValidServiceCollection() testPlugin.DefaultServiceRegistrationDelegate(serviceCollection); - var methodReturnType = testPlugin.DefaultServiceRegistrationDelegate.Method.ReturnType.Name; - - Assert.True(methodReturnType == "Void"); Assert.Contains(serviceCollection, s => s.ServiceType == typeof(IValidationAttributeAdapterProvider)); } } diff --git a/test/MyTested.AspNetCore.Mvc.Models.Test/MyTested.AspNetCore.Mvc.Models.Test.csproj b/test/MyTested.AspNetCore.Mvc.Models.Test/MyTested.AspNetCore.Mvc.Models.Test.csproj index d689a686a..a32b249be 100644 --- a/test/MyTested.AspNetCore.Mvc.Models.Test/MyTested.AspNetCore.Mvc.Models.Test.csproj +++ b/test/MyTested.AspNetCore.Mvc.Models.Test/MyTested.AspNetCore.Mvc.Models.Test.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 true MyTested.AspNetCore.Mvc.Test ../../tools/Key.snk @@ -27,8 +27,11 @@ - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/test/MyTested.AspNetCore.Mvc.NewtonsoftJson.Test/BuildersTests/ActionResultsTests/JsonTests/JsonSerializerTestBuilderTests.cs b/test/MyTested.AspNetCore.Mvc.NewtonsoftJson.Test/BuildersTests/ActionResultsTests/JsonTests/JsonSerializerTestBuilderTests.cs index 4dd73c15a..ca3d89a1f 100644 --- a/test/MyTested.AspNetCore.Mvc.NewtonsoftJson.Test/BuildersTests/ActionResultsTests/JsonTests/JsonSerializerTestBuilderTests.cs +++ b/test/MyTested.AspNetCore.Mvc.NewtonsoftJson.Test/BuildersTests/ActionResultsTests/JsonTests/JsonSerializerTestBuilderTests.cs @@ -109,7 +109,7 @@ public void WithCultureShouldThrowExceptionWithIncorrectValue() .WithJsonSerializerSettings(s => s.WithCulture(CultureInfo.GetCultureInfo("en-US")))); }, - "When calling JsonWithSettingsAction action in MvcController expected JSON result serializer settings to have 'English (United States)' culture, but in fact found 'Invariant Language (Invariant Country)'."); + $"When calling JsonWithSettingsAction action in MvcController expected JSON result serializer settings to have '{CultureInfo.GetCultureInfo("en-US").DisplayName}' culture, but in fact found '{CultureInfo.InvariantCulture.DisplayName}'."); } [Fact] diff --git a/test/MyTested.AspNetCore.Mvc.NewtonsoftJson.Test/MyTested.AspNetCore.Mvc.NewtonsoftJson.Test.csproj b/test/MyTested.AspNetCore.Mvc.NewtonsoftJson.Test/MyTested.AspNetCore.Mvc.NewtonsoftJson.Test.csproj index cd6225393..c531ad3a9 100644 --- a/test/MyTested.AspNetCore.Mvc.NewtonsoftJson.Test/MyTested.AspNetCore.Mvc.NewtonsoftJson.Test.csproj +++ b/test/MyTested.AspNetCore.Mvc.NewtonsoftJson.Test/MyTested.AspNetCore.Mvc.NewtonsoftJson.Test.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 true MyTested.AspNetCore.Mvc.Test ../../tools/Key.snk @@ -27,8 +27,11 @@ - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/test/MyTested.AspNetCore.Mvc.Options.Test/MyTested.AspNetCore.Mvc.Options.Test.csproj b/test/MyTested.AspNetCore.Mvc.Options.Test/MyTested.AspNetCore.Mvc.Options.Test.csproj index 2f2c9e609..7da00def4 100644 --- a/test/MyTested.AspNetCore.Mvc.Options.Test/MyTested.AspNetCore.Mvc.Options.Test.csproj +++ b/test/MyTested.AspNetCore.Mvc.Options.Test/MyTested.AspNetCore.Mvc.Options.Test.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 true MyTested.AspNetCore.Mvc.Test ../../tools/Key.snk @@ -28,10 +28,13 @@ - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + - + diff --git a/test/MyTested.AspNetCore.Mvc.Options.Test/PluginsTests/OptionsTestPluginTests.cs b/test/MyTested.AspNetCore.Mvc.Options.Test/PluginsTests/OptionsTestPluginTests.cs index cf0d515f9..a1d8c3bad 100644 --- a/test/MyTested.AspNetCore.Mvc.Options.Test/PluginsTests/OptionsTestPluginTests.cs +++ b/test/MyTested.AspNetCore.Mvc.Options.Test/PluginsTests/OptionsTestPluginTests.cs @@ -24,9 +24,6 @@ public void ShouldInvokeMethodOfTypeVoidWithValidServiceCollection() testPlugin.ServiceRegistrationDelegate(serviceCollection); - var methodReturnType = testPlugin.ServiceRegistrationDelegate.Method.ReturnType.Name; - - Assert.True(methodReturnType == "Void"); Assert.Contains(serviceCollection, s => s.ServiceType == typeof(IOptions<>)); } } diff --git a/test/MyTested.AspNetCore.Mvc.Pipeline.Test/BuildersTests/PipelineTests/WhichControllerInstanceBuilderTests.cs b/test/MyTested.AspNetCore.Mvc.Pipeline.Test/BuildersTests/PipelineTests/WhichControllerInstanceBuilderTests.cs index 41f7fdc36..fd722cc57 100644 --- a/test/MyTested.AspNetCore.Mvc.Pipeline.Test/BuildersTests/PipelineTests/WhichControllerInstanceBuilderTests.cs +++ b/test/MyTested.AspNetCore.Mvc.Pipeline.Test/BuildersTests/PipelineTests/WhichControllerInstanceBuilderTests.cs @@ -1,5 +1,6 @@ namespace MyTested.AspNetCore.Mvc.Test.BuildersTests.PipelineTests { + using System.Threading; using Exceptions; using Microsoft.Extensions.DependencyInjection; using Setups; @@ -62,6 +63,136 @@ public void WhichShouldResolveCorrectEmptyAsyncAction() .ShouldReturnEmpty(); } + [Fact] + public void WhichShouldResolveCorrectActionWithIgnoredRouteValue() + { + MyPipeline + .Configuration() + .ShouldMap("/Home/Contact/1") + .To(c => c.Contact(With.Value(2))) + .Which() + .ShouldReturn() + .Ok(ok => ok + .Passing(result => result + .Value + .Equals(2))); + } + + [Fact] + public void WhichShouldResolveCorrectActionWithIgnoredRouteValueLongName() + { + MyPipeline + .Configuration() + .ShouldMap("/Home/Contact/1") + .To(c => c.Contact(With.IgnoredRouteValue(2))) + .Which() + .ShouldReturn() + .Ok(ok => ok + .Passing(result => result + .Value + .Equals(2))); + } + + [Fact] + public void WhichShouldResolveCorrectActionWithIgnoredCancellationTokenCancelled() + { + MyPipeline + .Configuration() + .ShouldMap("/Home/CancelledTask/1") + .To(c => c.CancelledTask(1, With.Value(new CancellationToken(true)))) + .Which() + .ShouldReturn() + .Ok(ok => ok + .Passing(result => result + .Value + .Equals("Cancelled with id: 1"))); + } + + [Fact] + public void WhichShouldResolveCorrectActionWithIgnoredCancellationTokenLongNameCancelled() + { + MyPipeline + .Configuration() + .ShouldMap("/Home/CancelledTask/1") + .To(c => c.CancelledTask(1, With.IgnoredRouteValue(new CancellationToken(true)))) + .Which() + .ShouldReturn() + .Ok(ok => ok + .Passing(result => result + .Value + .Equals("Cancelled with id: 1"))); + } + + [Fact] + public void WhichShouldResolveCorrectActionWithIgnoredCancellationToken() + { + MyPipeline + .Configuration() + .ShouldMap("/Home/CancelledTask/1") + .To(c => c.CancelledTask(1, With.Value(new CancellationToken(false)))) + .Which() + .ShouldReturn() + .Ok(ok => ok + .Passing(result => result + .Value + .Equals(1))); + } + + [Fact] + public void WhichShouldResolveCorrectActionWithIgnoredCancellationTokenLongName() + { + MyPipeline + .Configuration() + .ShouldMap("/Home/CancelledTask/1") + .To(c => c.CancelledTask(1, With.IgnoredRouteValue(new CancellationToken(false)))) + .Which() + .ShouldReturn() + .Ok(ok => ok + .Passing(result => result + .Value + .Equals(1))); + } + + [Fact] + public void WhichShouldResolveCorrectActionValuesWithIgnoredCancellationToken() + { + Test.AssertException( + () => + { + MyPipeline + .Configuration() + .ShouldMap("/Home/CancelledTask/1") + .To(c => c.CancelledTask(2, With.Value(new CancellationToken(false)))) + .Which() + .ShouldReturn() + .Ok(ok => ok + .Passing(result => result + .Value + .Equals(2))); + }, + "Expected route '/Home/CancelledTask/1' to contain route value with 'id' key and the provided value but the value was different. Expected a value of '2', but in fact it was '1'."); + } + + [Fact] + public void WhichShouldResolveCorrectActionValuesWithIgnoredCancellationTokenLongName() + { + Test.AssertException( + () => + { + MyPipeline + .Configuration() + .ShouldMap("/Home/CancelledTask/1") + .To(c => c.CancelledTask(2, With.IgnoredRouteValue(new CancellationToken(false)))) + .Which() + .ShouldReturn() + .Ok(ok => ok + .Passing(result => result + .Value + .Equals(2))); + }, + "Expected route '/Home/CancelledTask/1' to contain route value with 'id' key and the provided value but the value was different. Expected a value of '2', but in fact it was '1'."); + } + [Fact] public void WhichShouldResolveCorrectAsyncActionWithSetup() { diff --git a/test/MyTested.AspNetCore.Mvc.Pipeline.Test/MyTested.AspNetCore.Mvc.Pipeline.Test.csproj b/test/MyTested.AspNetCore.Mvc.Pipeline.Test/MyTested.AspNetCore.Mvc.Pipeline.Test.csproj index 26a7b6984..309ec0c19 100644 --- a/test/MyTested.AspNetCore.Mvc.Pipeline.Test/MyTested.AspNetCore.Mvc.Pipeline.Test.csproj +++ b/test/MyTested.AspNetCore.Mvc.Pipeline.Test/MyTested.AspNetCore.Mvc.Pipeline.Test.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 true MyTested.AspNetCore.Mvc.Test ../../tools/Key.snk @@ -29,8 +29,11 @@ - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/test/MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation.Test/BuildersTests/ControllersTests/ControllerTestBuilderTests.cs b/test/MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation.Test/BuildersTests/ControllersTests/ControllerTestBuilderTests.cs new file mode 100644 index 000000000..939e7c737 --- /dev/null +++ b/test/MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation.Test/BuildersTests/ControllersTests/ControllerTestBuilderTests.cs @@ -0,0 +1,17 @@ +namespace MyTested.AspNetCore.Mvc.Test.BuildersTests.ControllersTests +{ + using Setups.Controllers; + using Xunit; + + public class ControllerTestBuilderTests + { + [Fact] + public void ControllerAssertionShouldWorkCorrectlyWithRazorRuntimeCompilation() + { + MyController + .Calling(c => c.DefaultView()) + .ShouldReturn() + .View(); + } + } +} diff --git a/test/MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation.Test/MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation.Test.csproj b/test/MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation.Test/MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation.Test.csproj new file mode 100644 index 000000000..02a9d323b --- /dev/null +++ b/test/MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation.Test/MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation.Test.csproj @@ -0,0 +1,33 @@ + + + + net6.0 + true + MyTested.AspNetCore.Mvc.Test + ../../tools/Key.snk + true + true + MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation.Test + true + false + true + MyTested.AspNetCore.Mvc.Test + + + + + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + diff --git a/test/MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation.Test/Properties/AssemblyInfo.cs b/test/MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation.Test/Properties/AssemblyInfo.cs new file mode 100644 index 000000000..30543f164 --- /dev/null +++ b/test/MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation.Test/Properties/AssemblyInfo.cs @@ -0,0 +1,11 @@ +using System.Reflection; +using System.Runtime.InteropServices; +using Microsoft.AspNetCore.Mvc.ApplicationParts; +using Xunit; + +[assembly: ApplicationPart("MyTested.AspNetCore.Mvc.Test.Setups")] + +[assembly: AssemblyProduct("MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation.Test")] +[assembly: ComVisible(false)] + +[assembly: CollectionBehavior(DisableTestParallelization = true)] diff --git a/test/MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation.Test/TestStartup.cs b/test/MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation.Test/TestStartup.cs new file mode 100644 index 000000000..3b3eb3ad1 --- /dev/null +++ b/test/MyTested.AspNetCore.Mvc.Razor.RuntimeCompilation.Test/TestStartup.cs @@ -0,0 +1,18 @@ +namespace MyTested.AspNetCore.Mvc.Test +{ + using Microsoft.Extensions.DependencyInjection; + using Setups; + + public class TestStartup : DefaultStartup + { + public override void ConfigureServices(IServiceCollection services) + { + services + .AddControllersWithViews() + .AddRazorRuntimeCompilation(); + + services + .AddRazorPages(); + } + } +} diff --git a/test/MyTested.AspNetCore.Mvc.Routing.Test/BuildersTests/RoutingTests/RouteTestBuilderTests.cs b/test/MyTested.AspNetCore.Mvc.Routing.Test/BuildersTests/RoutingTests/RouteTestBuilderTests.cs index a12fceb3f..5241d29c7 100644 --- a/test/MyTested.AspNetCore.Mvc.Routing.Test/BuildersTests/RoutingTests/RouteTestBuilderTests.cs +++ b/test/MyTested.AspNetCore.Mvc.Routing.Test/BuildersTests/RoutingTests/RouteTestBuilderTests.cs @@ -431,6 +431,24 @@ public void ToShouldResolveCorrectlyWithIgnoredParameter() .To(c => c.Contact(With.Any())); } + [Fact] + public void ToShouldResolveCorrectlyWithIgnoredParameterButActualValue() + { + MyRouting + .Configuration() + .ShouldMap("/Home/Contact/1") + .To(c => c.Contact(With.Value(2))); + } + + [Fact] + public void ToShouldResolveCorrectlyWithIgnoredParameterButActualValueLongName() + { + MyRouting + .Configuration() + .ShouldMap("/Home/Contact/1") + .To(c => c.Contact(With.IgnoredRouteValue(2))); + } + [Fact] public void ToShouldResolveCorrectControllerAndActionWithNoModel() { diff --git a/test/MyTested.AspNetCore.Mvc.Routing.Test/MyTested.AspNetCore.Mvc.Routing.Test.csproj b/test/MyTested.AspNetCore.Mvc.Routing.Test/MyTested.AspNetCore.Mvc.Routing.Test.csproj index ebab264fc..611eb5e23 100644 --- a/test/MyTested.AspNetCore.Mvc.Routing.Test/MyTested.AspNetCore.Mvc.Routing.Test.csproj +++ b/test/MyTested.AspNetCore.Mvc.Routing.Test/MyTested.AspNetCore.Mvc.Routing.Test.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 true MyTested.AspNetCore.Mvc.Test ../../tools/Key.snk @@ -29,8 +29,11 @@ - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/test/MyTested.AspNetCore.Mvc.Session.Test/MyTested.AspNetCore.Mvc.Session.Test.csproj b/test/MyTested.AspNetCore.Mvc.Session.Test/MyTested.AspNetCore.Mvc.Session.Test.csproj index dd6c26606..adb59a0cd 100644 --- a/test/MyTested.AspNetCore.Mvc.Session.Test/MyTested.AspNetCore.Mvc.Session.Test.csproj +++ b/test/MyTested.AspNetCore.Mvc.Session.Test/MyTested.AspNetCore.Mvc.Session.Test.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 true MyTested.AspNetCore.Mvc.Test ../../tools/Key.snk @@ -29,8 +29,11 @@ - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/test/MyTested.AspNetCore.Mvc.TempData.Test/MyTested.AspNetCore.Mvc.TempData.Test.csproj b/test/MyTested.AspNetCore.Mvc.TempData.Test/MyTested.AspNetCore.Mvc.TempData.Test.csproj index 49d75c3a1..76aaa883b 100644 --- a/test/MyTested.AspNetCore.Mvc.TempData.Test/MyTested.AspNetCore.Mvc.TempData.Test.csproj +++ b/test/MyTested.AspNetCore.Mvc.TempData.Test/MyTested.AspNetCore.Mvc.TempData.Test.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 true MyTested.AspNetCore.Mvc.Test ../../tools/Key.snk @@ -29,8 +29,11 @@ - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/test/MyTested.AspNetCore.Mvc.Test.Setups/MyTested.AspNetCore.Mvc.Test.Setups.csproj b/test/MyTested.AspNetCore.Mvc.Test.Setups/MyTested.AspNetCore.Mvc.Test.Setups.csproj index 1d03b18c4..6647ac053 100644 --- a/test/MyTested.AspNetCore.Mvc.Test.Setups/MyTested.AspNetCore.Mvc.Test.Setups.csproj +++ b/test/MyTested.AspNetCore.Mvc.Test.Setups/MyTested.AspNetCore.Mvc.Test.Setups.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 MyTested.AspNetCore.Mvc.Test.Setups true ../../tools/Key.snk @@ -18,7 +18,7 @@ - + diff --git a/test/MyTested.AspNetCore.Mvc.Test.Setups/Routing/HomeController.cs b/test/MyTested.AspNetCore.Mvc.Test.Setups/Routing/HomeController.cs index e8f082916..49e5ef48c 100644 --- a/test/MyTested.AspNetCore.Mvc.Test.Setups/Routing/HomeController.cs +++ b/test/MyTested.AspNetCore.Mvc.Test.Setups/Routing/HomeController.cs @@ -1,6 +1,7 @@ namespace MyTested.AspNetCore.Mvc.Test.Setups.Routing { using System; + using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; @@ -21,5 +22,15 @@ public IActionResult FailingAction() public void Empty() { } public async Task EmptyTask() => await Task.CompletedTask; + + public async Task CancelledTask(int id, CancellationToken cancellationToken) + { + if (cancellationToken.IsCancellationRequested) + { + return await Task.FromResult(Ok($"Cancelled with id: {id}")); + } + + return await Task.FromResult(Ok(id)); + } } } diff --git a/test/MyTested.AspNetCore.Mvc.Test.Setups/Startups/StartupWithException.cs b/test/MyTested.AspNetCore.Mvc.Test.Setups/Startups/StartupWithException.cs new file mode 100644 index 000000000..86ebba413 --- /dev/null +++ b/test/MyTested.AspNetCore.Mvc.Test.Setups/Startups/StartupWithException.cs @@ -0,0 +1,18 @@ +namespace MyTested.AspNetCore.Mvc.Test.Setups.Startups +{ + using System; + using Microsoft.AspNetCore.Builder; + using Microsoft.Extensions.DependencyInjection; + + public class StartupWithException + { + public virtual void ConfigureServices(IServiceCollection services) + => throw new Exception("Exception during service registration."); + + public virtual void Configure(IApplicationBuilder app) + => app + .UseRouting() + .UseEndpoints(endpoints => endpoints + .MapDefaultControllerRoute()); + } +} diff --git a/test/MyTested.AspNetCore.Mvc.Test.Setups/TestObjectFactory.cs b/test/MyTested.AspNetCore.Mvc.Test.Setups/TestObjectFactory.cs index c92ba519e..d2057789d 100644 --- a/test/MyTested.AspNetCore.Mvc.Test.Setups/TestObjectFactory.cs +++ b/test/MyTested.AspNetCore.Mvc.Test.Setups/TestObjectFactory.cs @@ -108,7 +108,8 @@ public static void SetCustomHttpResponseProperties(HttpResponse response) public static IOutputFormatter GetOutputFormatter() => new NewtonsoftJsonOutputFormatter( GetJsonSerializerSettings(), ArrayPool.Create(), - new MvcOptions()); + new MvcOptions(), + new MvcNewtonsoftJsonOptions()); public static Uri GetUri() => new Uri("http://somehost.com/someuri/1?query=Test"); diff --git a/test/MyTested.AspNetCore.Mvc.Test/MyTested.AspNetCore.Mvc.Test.csproj b/test/MyTested.AspNetCore.Mvc.Test/MyTested.AspNetCore.Mvc.Test.csproj index 3b0ebfba1..a30699ae2 100644 --- a/test/MyTested.AspNetCore.Mvc.Test/MyTested.AspNetCore.Mvc.Test.csproj +++ b/test/MyTested.AspNetCore.Mvc.Test/MyTested.AspNetCore.Mvc.Test.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 true MyTested.AspNetCore.Mvc.Test ../../tools/Key.snk @@ -25,8 +25,11 @@ - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/test/MyTested.AspNetCore.Mvc.Universe.Test/MyTested.AspNetCore.Mvc.Universe.Test.csproj b/test/MyTested.AspNetCore.Mvc.Universe.Test/MyTested.AspNetCore.Mvc.Universe.Test.csproj index 5a7c530ce..9b9fe1f80 100644 --- a/test/MyTested.AspNetCore.Mvc.Universe.Test/MyTested.AspNetCore.Mvc.Universe.Test.csproj +++ b/test/MyTested.AspNetCore.Mvc.Universe.Test/MyTested.AspNetCore.Mvc.Universe.Test.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 true MyTested.AspNetCore.Mvc.Test ../../tools/Key.snk @@ -26,10 +26,13 @@ - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + - + diff --git a/test/MyTested.AspNetCore.Mvc.Universe.Test/ServicesTests.cs b/test/MyTested.AspNetCore.Mvc.Universe.Test/ServicesTests.cs index f3d8df095..37e95c194 100644 --- a/test/MyTested.AspNetCore.Mvc.Universe.Test/ServicesTests.cs +++ b/test/MyTested.AspNetCore.Mvc.Universe.Test/ServicesTests.cs @@ -1,425 +1,427 @@ -namespace MyTested.AspNetCore.Mvc.Test -{ - using System; - using System.Linq; - using Internal; - using Internal.Caching; - using Internal.Contracts; - using Internal.EntityFrameworkCore; - using Internal.Formatters; - using Internal.Routing; - using Internal.Services; - using Internal.Session; - using Microsoft.AspNetCore.Mvc; - using Microsoft.AspNetCore.Mvc.ViewFeatures; - using Microsoft.AspNetCore.Session; - using Microsoft.EntityFrameworkCore; - using Microsoft.EntityFrameworkCore.Infrastructure; - using Microsoft.EntityFrameworkCore.InMemory.Infrastructure.Internal; - using Microsoft.Extensions.Caching.Memory; - using Microsoft.Extensions.DependencyInjection; - using Microsoft.Extensions.Options; - using Setups; - using Setups.Common; - using Xunit; - - public class ServicesTests - { - [Fact] - public void AddMvcUniverseTestingShouldReplaceDefaultMemoryCacheWithMockedVersion() - { - var services = new ServiceCollection(); - - services.AddMvc(); - var defaultMemoryCache = services.BuildServiceProvider().GetService(); - - services.AddMvcUniverseTesting(); - var mockMemoryCache = services.BuildServiceProvider().GetService(); - - Assert.NotNull(defaultMemoryCache); - Assert.NotNull(mockMemoryCache); - Assert.NotSame(mockMemoryCache, defaultMemoryCache); - Assert.IsAssignableFrom(defaultMemoryCache); - Assert.IsAssignableFrom(mockMemoryCache); - Assert.IsAssignableFrom(mockMemoryCache); - Assert.Contains(services, s => s.ServiceType == typeof(IMemoryCache) && s.Lifetime == ServiceLifetime.Transient); - } - - [Fact] - public void AddMvcUniverseTestingWithoutMemoryCacheShouldAddMockedVersion() - { - var services = new ServiceCollection(); - - services.AddMvc(); - services.Remove(); - - var defaultMemoryCache = services.BuildServiceProvider().GetService(); - - services.AddMvcUniverseTesting(); - var mockMemoryCache = services.BuildServiceProvider().GetService(); - - Assert.Null(defaultMemoryCache); - Assert.NotNull(mockMemoryCache); - Assert.IsAssignableFrom(mockMemoryCache); - Assert.IsAssignableFrom(mockMemoryCache); - Assert.Contains(services, s => s.ServiceType == typeof(IMemoryCache) && s.Lifetime == ServiceLifetime.Transient); - } - - [Fact] - public void AddMvcUniverseTestingShouldOverrideDefaultMemoryCacheWithMockedVersion() - { - MyApplication - .StartsFrom() - .WithServices(services => - { - services.AddMvcUniverseTesting(); - }); - - var memoryCache = TestServiceProvider.GetService(); - - Assert.NotNull(memoryCache); - Assert.IsAssignableFrom(memoryCache); - - MyApplication.StartsFrom(); - } - - [Fact] - public void AddMvcUniverseTestingShouldReplaceCustomSessionStoreWithMockedVersion() - { - var services = new ServiceCollection(); - - services.AddMvc(); - services.AddTransient(); - - var customSessionStore = services.BuildServiceProvider().GetService(); - - services.AddMvcUniverseTesting(); - var mockSessionStore= services.BuildServiceProvider().GetService(); - - Assert.NotNull(customSessionStore); - Assert.NotNull(mockSessionStore); - Assert.NotSame(mockSessionStore, customSessionStore); - Assert.IsAssignableFrom(customSessionStore); - Assert.IsAssignableFrom(mockSessionStore); - Assert.IsAssignableFrom(mockSessionStore); - } - - [Fact] - public void AddMvcUniverseTestingWithoutSessionStoreShouldAddMockedVersion() - { - var services = new ServiceCollection(); - - services.AddMvc(); - var defaultSessionStore = services.BuildServiceProvider().GetService(); - - services.AddMvcUniverseTesting(); - var mockSessionStore = services.BuildServiceProvider().GetService(); - - Assert.Null(defaultSessionStore); - Assert.NotNull(mockSessionStore); - Assert.IsAssignableFrom(mockSessionStore); - Assert.IsAssignableFrom(mockSessionStore); - } - - [Fact] - public void AddMvcUniverseTestingShouldOverrideNullSessionStoreWithMockedVersion() - { - MyApplication - .StartsFrom() - .WithServices(services => - { - services.AddMvcUniverseTesting(); - }); - - var sessionStore = TestServiceProvider.GetService(); - - Assert.NotNull(sessionStore); - Assert.IsAssignableFrom(sessionStore); - - MyApplication.StartsFrom(); - } - - [Fact] - public void AddMvcUniverseTestingShouldReplaceNonInMemoryDatabaseWithInMemoryScopedOne() - { - var services = new ServiceCollection(); - - services.AddMvc(); - services.AddDbContext(options => - options.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=TestDb;Trusted_Connection=True;MultipleActiveResultSets=true;Connect Timeout=30;")); - - services.AddMvcUniverseTesting(); - - var serviceProvider = services.BuildServiceProvider(); - - var dbContextService = services.FirstOrDefault(s => s.ServiceType == typeof(CustomDbContext)); - - Assert.NotNull(dbContextService); - Assert.Equal(ServiceLifetime.Scoped, dbContextService.Lifetime); - - var customDbContext = serviceProvider.GetService(); - - Assert.NotNull(customDbContext); - - var dbContextOptions = serviceProvider.GetService>(); - - Assert.NotNull(dbContextOptions); - Assert.Equal(3, dbContextOptions.Extensions.Count()); - - var coreOptionsExtension = dbContextOptions.FindExtension(); - var inMemoryOptionsExtension = dbContextOptions.FindExtension(); - var scopedInMemoryOptionsExtension = dbContextOptions.FindExtension(); - - Assert.NotNull(coreOptionsExtension); - Assert.NotNull(inMemoryOptionsExtension); - Assert.NotNull(scopedInMemoryOptionsExtension); - } - - [Fact] - public void AddMvcUniverseTestingShouldReplaceOptionsWithScopedOnes() - { - var services = new ServiceCollection(); - - services.AddMvc(); - - Assert.Contains(services, s => s.ServiceType == typeof(IOptions<>) && s.Lifetime == ServiceLifetime.Singleton); - - services.AddMvcUniverseTesting(); - - Assert.Contains(services, s => s.ServiceType == typeof(IOptions<>) && s.Lifetime == ServiceLifetime.Scoped); - } - - [Fact] - public void AddMvcUniverseTestingShouldAddStringInputFormatter() - { - MyApplication.StartsFrom(); - - var builtInOptions = TestServiceProvider.GetService>(); - builtInOptions.Value.InputFormatters.RemoveType(); - - Assert.NotNull(builtInOptions); - Assert.True(builtInOptions.Value.InputFormatters.Count == 1); - Assert.DoesNotContain(typeof(StringInputFormatter), builtInOptions.Value.InputFormatters.Select(f => f.GetType())); - - MyApplication.StartsFrom() - .WithServices(services => services.AddMvcUniverseTesting()); - - builtInOptions = TestServiceProvider.GetService>(); - - Assert.NotNull(builtInOptions); - Assert.True(builtInOptions.Value.InputFormatters.Count == 2); - Assert.Contains(typeof(StringInputFormatter), builtInOptions.Value.InputFormatters.Select(f => f.GetType())); - } - - [Fact] - public void AddMvcUniverseTestingWithStringInputFormatterShouldNotOverrideIt() - { - var inputFormatter = new StringInputFormatter(); - - MyApplication.StartsFrom() - .WithServices(services => - { - services.Configure(options => - { - options.InputFormatters.Add(inputFormatter); - }); - - services.AddMvcUniverseTesting(); - }); - - var builtInOptions = TestServiceProvider.GetService>(); - - Assert.NotNull(builtInOptions); - Assert.True(builtInOptions.Value.InputFormatters.Count == 2); - Assert.Contains(typeof(StringInputFormatter), builtInOptions.Value.InputFormatters.Select(f => f.GetType())); - Assert.Same(inputFormatter, builtInOptions.Value.InputFormatters.FirstOrDefault(f => f.GetType() == typeof(StringInputFormatter))); - } - - [Fact] - public void AddMvcUniverseTestingShouldReplaceTempDataProviderWithMockedVersion() - { - MyApplication.StartsFrom() - .WithServices(services => - { - services.Replace(ServiceLifetime.Scoped); - }); - - var tempDataPovider = TestServiceProvider.GetService(); - - Assert.NotNull(tempDataPovider); - Assert.True(typeof(CustomTempDataProvider) == tempDataPovider.GetType()); - - MyApplication.StartsFrom() - .WithServices(services => - { - services.AddMvcUniverseTesting(); - }); - - tempDataPovider = TestServiceProvider.GetService(); - - Assert.NotNull(tempDataPovider); - Assert.True(typeof(TempDataProviderMock) == tempDataPovider.GetType()); - } - - [Fact] - public void AddMvcUniverseTestingWithoutTempDataProviderShouldAddMockedVersion() - { - var services = new ServiceCollection(); - - services.AddMvc(); - services.Remove(); - - var defaultTempDataProvider = services.BuildServiceProvider().GetService(); - - services.AddMvcUniverseTesting(); - var mockTempDataProvider = services.BuildServiceProvider().GetService(); - - Assert.Null(defaultTempDataProvider); - Assert.NotNull(mockTempDataProvider); - Assert.IsAssignableFrom(mockTempDataProvider); - Assert.IsAssignableFrom(mockTempDataProvider); - } - - [Fact] - public void AddMvcUniverseTestingShouldAddViewComponentTesting() - { - var services = new ServiceCollection(); - - services.AddMvc(); - - var viewComponentPropertyActivator = services.BuildServiceProvider().GetService(); - var viewComponentDescriptorCache = services.BuildServiceProvider().GetService(); - - Assert.Null(viewComponentPropertyActivator); - Assert.Null(viewComponentDescriptorCache); - - services.AddMvcUniverseTesting(); - - viewComponentPropertyActivator = services.BuildServiceProvider().GetService(); - viewComponentDescriptorCache = services.BuildServiceProvider().GetService(); - - Assert.NotNull(viewComponentPropertyActivator); - Assert.NotNull(viewComponentDescriptorCache); - - Assert.Contains(services, s => s.ServiceType == typeof(IViewComponentPropertyActivator) && s.Lifetime == ServiceLifetime.Singleton); - Assert.Contains(services, s => s.ServiceType == typeof(IViewComponentDescriptorCache) && s.Lifetime == ServiceLifetime.Singleton); - - Assert.IsAssignableFrom(viewComponentPropertyActivator); - Assert.IsAssignableFrom(viewComponentDescriptorCache); - } - - [Fact] - public void AddMvcUniverseTestingShouldAddControllersTestingServices() - { - var services = new ServiceCollection(); - - services.AddMvc(); - - var validControllersCache = services.BuildServiceProvider().GetService(); - - Assert.Null(validControllersCache); - - services.AddMvcUniverseTesting(); - validControllersCache = services.BuildServiceProvider().GetService(); - - Assert.NotNull(validControllersCache); - Assert.IsAssignableFrom(validControllersCache); - Assert.Contains(services, s => s.ServiceType == typeof(IValidControllersCache) && s.Lifetime == ServiceLifetime.Singleton); - } - - [Fact] - public void AddMvcUniverseTestingShouldAddControllersTestingServicesAndConfigureConventions() - { - MyApplication.StartsFrom(); - - var mvcOptions = TestServiceProvider.GetService>(); - - Assert.NotNull(mvcOptions); - Assert.NotEmpty(mvcOptions.Value.Conventions); - Assert.True(mvcOptions.Value.Conventions.Count == 1); - - MyApplication.StartsFrom() - .WithServices(services => - { - services.AddMvcUniverseTesting(); - }); - - var validControllersCache = TestServiceProvider.GetService(); - - Assert.NotNull(validControllersCache); - Assert.IsAssignableFrom(validControllersCache); - - mvcOptions = TestServiceProvider.GetService>(); - - Assert.NotNull(mvcOptions); - Assert.NotEmpty(mvcOptions.Value.Conventions); - Assert.True(mvcOptions.Value.Conventions.Count == 2); - } - - [Fact] - public void AddMvcUniverseTestingShouldAddRoutingTestingServices() - { - var services = new ServiceCollection(); - - services.AddMvc(); - - var routingServices = services.BuildServiceProvider().GetService(); - - Assert.Null(routingServices); - - services.AddMvcUniverseTesting(); - routingServices = services.BuildServiceProvider().GetService(); - - Assert.NotNull(routingServices); - Assert.IsAssignableFrom(routingServices); - Assert.Contains(services, s => s.ServiceType == typeof(IRoutingServices) && s.Lifetime == ServiceLifetime.Singleton); - } - - [Fact] - public void AddMvcUniverseTestingWithRoutingTestingServicesShouldNotOverrideThem() - { - IRoutingServices routingServices = new RoutingServices(); - - var services = new ServiceCollection(); - - services.AddMvc(); - services.AddSingleton(typeof(IRoutingServices), routingServices); - - Assert.NotNull(services.BuildServiceProvider().GetService()); - - services.AddMvcUniverseTesting(); - var actualRoutingServices = services.BuildServiceProvider().GetService(); - - Assert.NotNull(routingServices); - Assert.IsAssignableFrom(routingServices); - Assert.Same(routingServices, actualRoutingServices); - } - - [Fact] - public void AddMvcUniverseTestingShouldAddRoutingTestingServicesAndDisableEndPointRouting() - { - MyApplication.StartsFrom() - .WithServices(services => - { - services.AddMvcUniverseTesting(); - }); - - var mvcOptions = TestServiceProvider.GetService>(); - - Assert.True(mvcOptions.Value.EnableEndpointRouting); - } - - [Fact] - public void WithoutServiceCollectionShouldThrowException() - { - IServiceCollection services = null; - - Test.AssertException( - () => - { - services.AddMvcUniverseTesting(); - }, - "serviceCollection cannot be null."); - } - } -} +namespace MyTested.AspNetCore.Mvc.Test +{ + using System; + using System.Linq; + using Internal; + using Internal.Caching; + using Internal.Contracts; + using Internal.EntityFrameworkCore; + using Internal.Formatters; + using Internal.Routing; + using Internal.Services; + using Internal.Session; + using Microsoft.AspNetCore.Mvc; + using Microsoft.AspNetCore.Mvc.ViewFeatures; + using Microsoft.AspNetCore.Session; + using Microsoft.EntityFrameworkCore; + using Microsoft.EntityFrameworkCore.Infrastructure; + using Microsoft.EntityFrameworkCore.InMemory.Infrastructure.Internal; + using Microsoft.Extensions.Caching.Memory; + using Microsoft.Extensions.DependencyInjection; + using Microsoft.Extensions.Options; + using Setups; + using Setups.Common; + using Xunit; + + public class ServicesTests + { + [Fact] + public void AddMvcUniverseTestingShouldReplaceDefaultMemoryCacheWithMockedVersion() + { + var services = new ServiceCollection(); + + services.AddMvc(); + var defaultMemoryCache = services.BuildServiceProvider().GetService(); + + services.AddMvcUniverseTesting(); + var mockMemoryCache = services.BuildServiceProvider().GetService(); + + Assert.NotNull(defaultMemoryCache); + Assert.NotNull(mockMemoryCache); + Assert.NotSame(mockMemoryCache, defaultMemoryCache); + Assert.IsAssignableFrom(defaultMemoryCache); + Assert.IsAssignableFrom(mockMemoryCache); + Assert.IsAssignableFrom(mockMemoryCache); + Assert.Contains(services, s => s.ServiceType == typeof(IMemoryCache) && s.Lifetime == ServiceLifetime.Transient); + } + + [Fact] + public void AddMvcUniverseTestingWithoutMemoryCacheShouldAddMockedVersion() + { + var services = new ServiceCollection(); + + services.AddMvc(); + services.Remove(); + + var defaultMemoryCache = services.BuildServiceProvider().GetService(); + + services.AddMvcUniverseTesting(); + var mockMemoryCache = services.BuildServiceProvider().GetService(); + + Assert.Null(defaultMemoryCache); + Assert.NotNull(mockMemoryCache); + Assert.IsAssignableFrom(mockMemoryCache); + Assert.IsAssignableFrom(mockMemoryCache); + Assert.Contains(services, s => s.ServiceType == typeof(IMemoryCache) && s.Lifetime == ServiceLifetime.Transient); + } + + [Fact] + public void AddMvcUniverseTestingShouldOverrideDefaultMemoryCacheWithMockedVersion() + { + MyApplication + .StartsFrom() + .WithServices(services => + { + services.AddMvcUniverseTesting(); + }); + + var memoryCache = TestServiceProvider.GetService(); + + Assert.NotNull(memoryCache); + Assert.IsAssignableFrom(memoryCache); + + MyApplication.StartsFrom(); + } + + [Fact] + public void AddMvcUniverseTestingShouldReplaceCustomSessionStoreWithMockedVersion() + { + var services = new ServiceCollection(); + + services.AddMvc(); + services.AddTransient(); + + var customSessionStore = services.BuildServiceProvider().GetService(); + + services.AddMvcUniverseTesting(); + var mockSessionStore= services.BuildServiceProvider().GetService(); + + Assert.NotNull(customSessionStore); + Assert.NotNull(mockSessionStore); + Assert.NotSame(mockSessionStore, customSessionStore); + Assert.IsAssignableFrom(customSessionStore); + Assert.IsAssignableFrom(mockSessionStore); + Assert.IsAssignableFrom(mockSessionStore); + } + + [Fact] + public void AddMvcUniverseTestingWithoutSessionStoreShouldAddMockedVersion() + { + var services = new ServiceCollection(); + + services.AddMvc(); + var defaultSessionStore = services.BuildServiceProvider().GetService(); + + services.AddMvcUniverseTesting(); + var mockSessionStore = services.BuildServiceProvider().GetService(); + + Assert.Null(defaultSessionStore); + Assert.NotNull(mockSessionStore); + Assert.IsAssignableFrom(mockSessionStore); + Assert.IsAssignableFrom(mockSessionStore); + } + + [Fact] + public void AddMvcUniverseTestingShouldOverrideNullSessionStoreWithMockedVersion() + { + MyApplication + .StartsFrom() + .WithServices(services => + { + services.AddMvcUniverseTesting(); + }); + + var sessionStore = TestServiceProvider.GetService(); + + Assert.NotNull(sessionStore); + Assert.IsAssignableFrom(sessionStore); + + MyApplication.StartsFrom(); + } + + [Fact] + public void AddMvcUniverseTestingShouldReplaceNonInMemoryDatabaseWithInMemoryScopedOne() + { + var services = new ServiceCollection(); + + services.AddMvc(); + services.AddDbContext(options => + options.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=TestDb;Trusted_Connection=True;MultipleActiveResultSets=true;Connect Timeout=30;")); + + services.AddMvcUniverseTesting(); + + var serviceProvider = services.BuildServiceProvider(); + + var dbContextService = services.FirstOrDefault(s => s.ServiceType == typeof(CustomDbContext)); + + Assert.NotNull(dbContextService); + Assert.Equal(ServiceLifetime.Scoped, dbContextService.Lifetime); + + var customDbContext = serviceProvider.GetService(); + + Assert.NotNull(customDbContext); + + var dbContextOptions = serviceProvider.GetService>(); + + Assert.NotNull(dbContextOptions); + Assert.Equal(3, dbContextOptions.Extensions.Count()); + +#pragma warning disable EF1001 // Internal EF Core API usage. + var coreOptionsExtension = dbContextOptions.FindExtension(); + var inMemoryOptionsExtension = dbContextOptions.FindExtension(); + var scopedInMemoryOptionsExtension = dbContextOptions.FindExtension(); +#pragma warning restore EF1001 // Internal EF Core API usage. + + Assert.NotNull(coreOptionsExtension); + Assert.NotNull(inMemoryOptionsExtension); + Assert.NotNull(scopedInMemoryOptionsExtension); + } + + [Fact] + public void AddMvcUniverseTestingShouldReplaceOptionsWithScopedOnes() + { + var services = new ServiceCollection(); + + services.AddMvc(); + + Assert.Contains(services, s => s.ServiceType == typeof(IOptions<>) && s.Lifetime == ServiceLifetime.Singleton); + + services.AddMvcUniverseTesting(); + + Assert.Contains(services, s => s.ServiceType == typeof(IOptions<>) && s.Lifetime == ServiceLifetime.Scoped); + } + + [Fact] + public void AddMvcUniverseTestingShouldAddStringInputFormatter() + { + MyApplication.StartsFrom(); + + var builtInOptions = TestServiceProvider.GetService>(); + builtInOptions.Value.InputFormatters.RemoveType(); + + Assert.NotNull(builtInOptions); + Assert.True(builtInOptions.Value.InputFormatters.Count == 1); + Assert.DoesNotContain(typeof(StringInputFormatter), builtInOptions.Value.InputFormatters.Select(f => f.GetType())); + + MyApplication.StartsFrom() + .WithServices(services => services.AddMvcUniverseTesting()); + + builtInOptions = TestServiceProvider.GetService>(); + + Assert.NotNull(builtInOptions); + Assert.True(builtInOptions.Value.InputFormatters.Count == 2); + Assert.Contains(typeof(StringInputFormatter), builtInOptions.Value.InputFormatters.Select(f => f.GetType())); + } + + [Fact] + public void AddMvcUniverseTestingWithStringInputFormatterShouldNotOverrideIt() + { + var inputFormatter = new StringInputFormatter(); + + MyApplication.StartsFrom() + .WithServices(services => + { + services.Configure(options => + { + options.InputFormatters.Add(inputFormatter); + }); + + services.AddMvcUniverseTesting(); + }); + + var builtInOptions = TestServiceProvider.GetService>(); + + Assert.NotNull(builtInOptions); + Assert.True(builtInOptions.Value.InputFormatters.Count == 2); + Assert.Contains(typeof(StringInputFormatter), builtInOptions.Value.InputFormatters.Select(f => f.GetType())); + Assert.Same(inputFormatter, builtInOptions.Value.InputFormatters.FirstOrDefault(f => f.GetType() == typeof(StringInputFormatter))); + } + + [Fact] + public void AddMvcUniverseTestingShouldReplaceTempDataProviderWithMockedVersion() + { + MyApplication.StartsFrom() + .WithServices(services => + { + services.Replace(ServiceLifetime.Scoped); + }); + + var tempDataPovider = TestServiceProvider.GetService(); + + Assert.NotNull(tempDataPovider); + Assert.True(typeof(CustomTempDataProvider) == tempDataPovider.GetType()); + + MyApplication.StartsFrom() + .WithServices(services => + { + services.AddMvcUniverseTesting(); + }); + + tempDataPovider = TestServiceProvider.GetService(); + + Assert.NotNull(tempDataPovider); + Assert.True(typeof(TempDataProviderMock) == tempDataPovider.GetType()); + } + + [Fact] + public void AddMvcUniverseTestingWithoutTempDataProviderShouldAddMockedVersion() + { + var services = new ServiceCollection(); + + services.AddMvc(); + services.Remove(); + + var defaultTempDataProvider = services.BuildServiceProvider().GetService(); + + services.AddMvcUniverseTesting(); + var mockTempDataProvider = services.BuildServiceProvider().GetService(); + + Assert.Null(defaultTempDataProvider); + Assert.NotNull(mockTempDataProvider); + Assert.IsAssignableFrom(mockTempDataProvider); + Assert.IsAssignableFrom(mockTempDataProvider); + } + + [Fact] + public void AddMvcUniverseTestingShouldAddViewComponentTesting() + { + var services = new ServiceCollection(); + + services.AddMvc(); + + var viewComponentPropertyActivator = services.BuildServiceProvider().GetService(); + var viewComponentDescriptorCache = services.BuildServiceProvider().GetService(); + + Assert.Null(viewComponentPropertyActivator); + Assert.Null(viewComponentDescriptorCache); + + services.AddMvcUniverseTesting(); + + viewComponentPropertyActivator = services.BuildServiceProvider().GetService(); + viewComponentDescriptorCache = services.BuildServiceProvider().GetService(); + + Assert.NotNull(viewComponentPropertyActivator); + Assert.NotNull(viewComponentDescriptorCache); + + Assert.Contains(services, s => s.ServiceType == typeof(IViewComponentPropertyActivator) && s.Lifetime == ServiceLifetime.Singleton); + Assert.Contains(services, s => s.ServiceType == typeof(IViewComponentDescriptorCache) && s.Lifetime == ServiceLifetime.Singleton); + + Assert.IsAssignableFrom(viewComponentPropertyActivator); + Assert.IsAssignableFrom(viewComponentDescriptorCache); + } + + [Fact] + public void AddMvcUniverseTestingShouldAddControllersTestingServices() + { + var services = new ServiceCollection(); + + services.AddMvc(); + + var validControllersCache = services.BuildServiceProvider().GetService(); + + Assert.Null(validControllersCache); + + services.AddMvcUniverseTesting(); + validControllersCache = services.BuildServiceProvider().GetService(); + + Assert.NotNull(validControllersCache); + Assert.IsAssignableFrom(validControllersCache); + Assert.Contains(services, s => s.ServiceType == typeof(IValidControllersCache) && s.Lifetime == ServiceLifetime.Singleton); + } + + [Fact] + public void AddMvcUniverseTestingShouldAddControllersTestingServicesAndConfigureConventions() + { + MyApplication.StartsFrom(); + + var mvcOptions = TestServiceProvider.GetService>(); + + Assert.NotNull(mvcOptions); + Assert.NotEmpty(mvcOptions.Value.Conventions); + Assert.True(mvcOptions.Value.Conventions.Count == 1); + + MyApplication.StartsFrom() + .WithServices(services => + { + services.AddMvcUniverseTesting(); + }); + + var validControllersCache = TestServiceProvider.GetService(); + + Assert.NotNull(validControllersCache); + Assert.IsAssignableFrom(validControllersCache); + + mvcOptions = TestServiceProvider.GetService>(); + + Assert.NotNull(mvcOptions); + Assert.NotEmpty(mvcOptions.Value.Conventions); + Assert.True(mvcOptions.Value.Conventions.Count == 2); + } + + [Fact] + public void AddMvcUniverseTestingShouldAddRoutingTestingServices() + { + var services = new ServiceCollection(); + + services.AddMvc(); + + var routingServices = services.BuildServiceProvider().GetService(); + + Assert.Null(routingServices); + + services.AddMvcUniverseTesting(); + routingServices = services.BuildServiceProvider().GetService(); + + Assert.NotNull(routingServices); + Assert.IsAssignableFrom(routingServices); + Assert.Contains(services, s => s.ServiceType == typeof(IRoutingServices) && s.Lifetime == ServiceLifetime.Singleton); + } + + [Fact] + public void AddMvcUniverseTestingWithRoutingTestingServicesShouldNotOverrideThem() + { + IRoutingServices routingServices = new RoutingServices(); + + var services = new ServiceCollection(); + + services.AddMvc(); + services.AddSingleton(typeof(IRoutingServices), routingServices); + + Assert.NotNull(services.BuildServiceProvider().GetService()); + + services.AddMvcUniverseTesting(); + var actualRoutingServices = services.BuildServiceProvider().GetService(); + + Assert.NotNull(routingServices); + Assert.IsAssignableFrom(routingServices); + Assert.Same(routingServices, actualRoutingServices); + } + + [Fact] + public void AddMvcUniverseTestingShouldAddRoutingTestingServicesAndDisableEndPointRouting() + { + MyApplication.StartsFrom() + .WithServices(services => + { + services.AddMvcUniverseTesting(); + }); + + var mvcOptions = TestServiceProvider.GetService>(); + + Assert.True(mvcOptions.Value.EnableEndpointRouting); + } + + [Fact] + public void WithoutServiceCollectionShouldThrowException() + { + IServiceCollection services = null; + + Test.AssertException( + () => + { + services.AddMvcUniverseTesting(); + }, + "serviceCollection cannot be null."); + } + } +} diff --git a/test/MyTested.AspNetCore.Mvc.Versioning.Test/MyTested.AspNetCore.Mvc.Versioning.Test.csproj b/test/MyTested.AspNetCore.Mvc.Versioning.Test/MyTested.AspNetCore.Mvc.Versioning.Test.csproj index b14023ccc..e91b7f960 100644 --- a/test/MyTested.AspNetCore.Mvc.Versioning.Test/MyTested.AspNetCore.Mvc.Versioning.Test.csproj +++ b/test/MyTested.AspNetCore.Mvc.Versioning.Test/MyTested.AspNetCore.Mvc.Versioning.Test.csproj @@ -1,7 +1,7 @@ - netcoreapp3.1 + net6.0 true MyTested.AspNetCore.Mvc.Test ../../tools/Key.snk @@ -23,8 +23,11 @@ - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/test/MyTested.AspNetCore.Mvc.ViewComponents.Attributes.Test/MyTested.AspNetCore.Mvc.ViewComponents.Attributes.Test.csproj b/test/MyTested.AspNetCore.Mvc.ViewComponents.Attributes.Test/MyTested.AspNetCore.Mvc.ViewComponents.Attributes.Test.csproj index 7c1846e8c..ed8d05419 100644 --- a/test/MyTested.AspNetCore.Mvc.ViewComponents.Attributes.Test/MyTested.AspNetCore.Mvc.ViewComponents.Attributes.Test.csproj +++ b/test/MyTested.AspNetCore.Mvc.ViewComponents.Attributes.Test/MyTested.AspNetCore.Mvc.ViewComponents.Attributes.Test.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 true MyTested.AspNetCore.Mvc.Test ../../tools/Key.snk @@ -26,9 +26,12 @@ - + - + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/test/MyTested.AspNetCore.Mvc.ViewComponents.Results.Test/MyTested.AspNetCore.Mvc.ViewComponents.Results.Test.csproj b/test/MyTested.AspNetCore.Mvc.ViewComponents.Results.Test/MyTested.AspNetCore.Mvc.ViewComponents.Results.Test.csproj index 3dbf09f71..b05f7ba64 100644 --- a/test/MyTested.AspNetCore.Mvc.ViewComponents.Results.Test/MyTested.AspNetCore.Mvc.ViewComponents.Results.Test.csproj +++ b/test/MyTested.AspNetCore.Mvc.ViewComponents.Results.Test/MyTested.AspNetCore.Mvc.ViewComponents.Results.Test.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 true MyTested.AspNetCore.Mvc.Test ../../tools/Key.snk @@ -27,8 +27,11 @@ - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/test/MyTested.AspNetCore.Mvc.ViewComponents.Test/MyTested.AspNetCore.Mvc.ViewComponents.Test.csproj b/test/MyTested.AspNetCore.Mvc.ViewComponents.Test/MyTested.AspNetCore.Mvc.ViewComponents.Test.csproj index 122a2a7a9..90a3fe80b 100644 --- a/test/MyTested.AspNetCore.Mvc.ViewComponents.Test/MyTested.AspNetCore.Mvc.ViewComponents.Test.csproj +++ b/test/MyTested.AspNetCore.Mvc.ViewComponents.Test/MyTested.AspNetCore.Mvc.ViewComponents.Test.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 true MyTested.AspNetCore.Mvc.Test ../../tools/Key.snk @@ -27,8 +27,11 @@ - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/test/MyTested.AspNetCore.Mvc.ViewData.Test/MyTested.AspNetCore.Mvc.ViewData.Test.csproj b/test/MyTested.AspNetCore.Mvc.ViewData.Test/MyTested.AspNetCore.Mvc.ViewData.Test.csproj index 1c14429d0..44ff4c63a 100644 --- a/test/MyTested.AspNetCore.Mvc.ViewData.Test/MyTested.AspNetCore.Mvc.ViewData.Test.csproj +++ b/test/MyTested.AspNetCore.Mvc.ViewData.Test/MyTested.AspNetCore.Mvc.ViewData.Test.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 true MyTested.AspNetCore.Mvc.Test ../../tools/Key.snk @@ -28,8 +28,11 @@ - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/test/MyTested.AspNetCore.Mvc.ViewData.Test/PluginsTests/ViewDataTestPluginTests.cs b/test/MyTested.AspNetCore.Mvc.ViewData.Test/PluginsTests/ViewDataTestPluginTests.cs index 7edf237ff..2bf1f7f26 100644 --- a/test/MyTested.AspNetCore.Mvc.ViewData.Test/PluginsTests/ViewDataTestPluginTests.cs +++ b/test/MyTested.AspNetCore.Mvc.ViewData.Test/PluginsTests/ViewDataTestPluginTests.cs @@ -33,6 +33,7 @@ public void ShouldInvokeMethodOfTypeVoidWithValidServiceCollection() testPlugin.DefaultServiceRegistrationDelegate(serviceCollection); - Assert.True(serviceCollection.Count == 157); } + Assert.True(serviceCollection.Count == 165); + } } } diff --git a/test/MyTested.AspNetCore.Mvc.ViewFeatures.Test/MyTested.AspNetCore.Mvc.ViewFeatures.Test.csproj b/test/MyTested.AspNetCore.Mvc.ViewFeatures.Test/MyTested.AspNetCore.Mvc.ViewFeatures.Test.csproj index 0ca1cee3d..025fcdf3f 100644 --- a/test/MyTested.AspNetCore.Mvc.ViewFeatures.Test/MyTested.AspNetCore.Mvc.ViewFeatures.Test.csproj +++ b/test/MyTested.AspNetCore.Mvc.ViewFeatures.Test/MyTested.AspNetCore.Mvc.ViewFeatures.Test.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net6.0 true MyTested.AspNetCore.Mvc.Test ../../tools/Key.snk @@ -26,8 +26,11 @@ - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive +