diff --git a/Sources/SrkToolkit.AspNetCore2.UnitTests/AspNetCoreTestContext.cs b/Sources/SrkToolkit.AspNetCore2.UnitTests/AspNetCoreTestContext.cs index 1840dbd..2d4eed1 100644 --- a/Sources/SrkToolkit.AspNetCore2.UnitTests/AspNetCoreTestContext.cs +++ b/Sources/SrkToolkit.AspNetCore2.UnitTests/AspNetCoreTestContext.cs @@ -190,6 +190,12 @@ public ITempDataDictionary TempData get { return this.tempData ?? (this.tempData = new TempDataDictionary(this.Context, this.TempDataProvider)); } } + public ControllerContext CreateControllerContext() + { + var item = new ControllerContext(this.ActionContext); + return item; + } + internal ITempDataProvider TempDataProvider { get { return this.tempDataProvider ?? (this.tempDataProvider = new Mock().Object); } diff --git a/Sources/SrkToolkit.AspNetCore2.UnitTests/SrkDomainControllerExtensionsTests.cs b/Sources/SrkToolkit.AspNetCore2.UnitTests/SrkDomainControllerExtensionsTests.cs new file mode 100644 index 0000000..8b31357 --- /dev/null +++ b/Sources/SrkToolkit.AspNetCore2.UnitTests/SrkDomainControllerExtensionsTests.cs @@ -0,0 +1,51 @@ + +namespace SrkToolkit.Web.Tests; + +using Microsoft.AspNetCore.Mvc; +using SrkToolkit.Domain; +using System; +using Xunit; + +public class SrkDomainControllerExtensionsTests +{ + [Fact] + public void ValidResult_TempData_Works() + { + var context = new AspNetCoreTestContext(); + var controller = new MyController(); + controller.ControllerContext = context.CreateControllerContext(); + var result = new BasicResult(); + result.Succeed = true; + var isValid = controller.ValidateResult(result, MessageDisplayMode.TempData); + Assert.True(isValid); + Assert.Empty(context.TempData); + } + + [Fact] + public void InvalidResult_TempData_Works() + { + var context = new AspNetCoreTestContext(); + var controller = new MyController(); + controller.ControllerContext = context.CreateControllerContext(); + var result = new BasicResult(); + result.Errors.Add(ErrorCode.Unauthorized); + result.Succeed = result.Errors.Count == 0; + var isValid = controller.ValidateResult(result, MessageDisplayMode.TempData); + Assert.False(isValid); + Assert.Single(context.TempData); + } + + class MyController : Controller + { + public ActionResult MyAction() + { + return this.NoContent(); + } + } + + enum ErrorCode + { + Invalid, + Unauthorized, + } +} diff --git a/Sources/SrkToolkit.AspNetCore2.UnitTests/SrkToolkit.AspNetCore2.UnitTests.csproj b/Sources/SrkToolkit.AspNetCore2.UnitTests/SrkToolkit.AspNetCore2.UnitTests.csproj index 6dd9687..fd0710a 100644 --- a/Sources/SrkToolkit.AspNetCore2.UnitTests/SrkToolkit.AspNetCore2.UnitTests.csproj +++ b/Sources/SrkToolkit.AspNetCore2.UnitTests/SrkToolkit.AspNetCore2.UnitTests.csproj @@ -35,6 +35,7 @@ + diff --git a/Sources/SrkToolkit.Domain.AspNetCore2/SrkToolkit.Domain.AspNetCore2.csproj b/Sources/SrkToolkit.Domain.AspNetCore2/SrkToolkit.Domain.AspNetCore2.csproj index 1d6acd6..ed7d4bf 100644 --- a/Sources/SrkToolkit.Domain.AspNetCore2/SrkToolkit.Domain.AspNetCore2.csproj +++ b/Sources/SrkToolkit.Domain.AspNetCore2/SrkToolkit.Domain.AspNetCore2.csproj @@ -29,8 +29,6 @@ SrkToolkit.snk - - @@ -44,16 +42,12 @@ - <_ProjectReferenceWithExplicitPackageVersion Include="@(ProjectReference->'%(FullPath)')" - Condition="'%(ProjectReference.PackageVersion)' != ''" /> - <_ProjectReferenceWithExactPackageVersion Include="@(ProjectReference->'%(FullPath)')" - Condition="'%(ProjectReference.ExactVersion)' == 'true'" /> - <_ProjectReferenceWithReassignedVersion Include="@(_ProjectReferencesWithVersions)" - Condition="'%(Identity)' != '' And '@(_ProjectReferencesWithVersions)' == '@(_ProjectReferenceWithExplicitPackageVersion)'"> + <_ProjectReferenceWithExplicitPackageVersion Include="@(ProjectReference->'%(FullPath)')" Condition="'%(ProjectReference.PackageVersion)' != ''" /> + <_ProjectReferenceWithExactPackageVersion Include="@(ProjectReference->'%(FullPath)')" Condition="'%(ProjectReference.ExactVersion)' == 'true'" /> + <_ProjectReferenceWithReassignedVersion Include="@(_ProjectReferencesWithVersions)" Condition="'%(Identity)' != '' And '@(_ProjectReferencesWithVersions)' == '@(_ProjectReferenceWithExplicitPackageVersion)'"> @(_ProjectReferenceWithExplicitPackageVersion->'%(PackageVersion)') - <_ProjectReferenceWithReassignedVersion Include="@(_ProjectReferencesWithVersions)" - Condition="'%(Identity)' != '' And '@(_ProjectReferencesWithVersions)' == '@(_ProjectReferenceWithExactPackageVersion)'"> + <_ProjectReferenceWithReassignedVersion Include="@(_ProjectReferencesWithVersions)" Condition="'%(Identity)' != '' And '@(_ProjectReferencesWithVersions)' == '@(_ProjectReferenceWithExactPackageVersion)'"> [@(_ProjectReferencesWithVersions->'%(ProjectVersion)')] <_ProjectReferencesWithVersions Remove="@(_ProjectReferenceWithReassignedVersion)" />