Skip to content

Commit

Permalink
SrkToolkit.Domain.AspNetCore2: migrate controller.ValidateResult (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
sandrock committed Mar 28, 2023
1 parent 2499cef commit d69a6fc
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ITempDataProvider>().Object); }
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ErrorCode>();
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<ErrorCode>();
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,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\SrkToolkit.Domain.AspNetCore2\SrkToolkit.Domain.AspNetCore2.csproj" />
<ProjectReference Include="..\SrkToolkit.Web.AspNetCore2\SrkToolkit.Web.AspNetCore2.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
<None Include="..\SrkToolkit.snk">
<Link>SrkToolkit.snk</Link>
</None>
<Compile Remove="SrkDomainControllerExtensions.cs" />
<None Include="SrkDomainControllerExtensions.cs" />
</ItemGroup>

<ItemGroup>
Expand All @@ -44,16 +42,12 @@

<Target Name="UseExplicitPackageVersions" BeforeTargets="GenerateNuspec">
<ItemGroup>
<_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)'">
<ProjectVersion>@(_ProjectReferenceWithExplicitPackageVersion->'%(PackageVersion)')</ProjectVersion>
</_ProjectReferenceWithReassignedVersion>
<_ProjectReferenceWithReassignedVersion Include="@(_ProjectReferencesWithVersions)"
Condition="'%(Identity)' != '' And '@(_ProjectReferencesWithVersions)' == '@(_ProjectReferenceWithExactPackageVersion)'">
<_ProjectReferenceWithReassignedVersion Include="@(_ProjectReferencesWithVersions)" Condition="'%(Identity)' != '' And '@(_ProjectReferencesWithVersions)' == '@(_ProjectReferenceWithExactPackageVersion)'">
<ProjectVersion>[@(_ProjectReferencesWithVersions->'%(ProjectVersion)')]</ProjectVersion>
</_ProjectReferenceWithReassignedVersion>
<_ProjectReferencesWithVersions Remove="@(_ProjectReferenceWithReassignedVersion)" />
Expand Down

0 comments on commit d69a6fc

Please sign in to comment.