Skip to content

Commit 1048faa

Browse files
committed
Added resolving of ViewComponent arguments from the lambda expression (#66)
1 parent 267217e commit 1048faa

File tree

2 files changed

+42
-15
lines changed

2 files changed

+42
-15
lines changed

src/MyTested.AspNetCore.Mvc.Controllers/Builders/Controllers/ControllerActionCallBuilder.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,20 @@ protected override void ProcessAndValidateMethod(LambdaExpression methodCall, Me
5656
}
5757
}
5858

59+
private void SetActionDescriptor(MethodInfo methodInfo)
60+
{
61+
var controllerContext = this.TestContext.ComponentContext;
62+
if (controllerContext.ActionDescriptor?.MethodInfo == null)
63+
{
64+
var controllerActionDescriptorCache = this.Services.GetService<IControllerActionDescriptorCache>();
65+
if (controllerActionDescriptorCache != null)
66+
{
67+
controllerContext.ActionDescriptor
68+
= controllerActionDescriptorCache.TryGetActionDescriptor(methodInfo);
69+
}
70+
}
71+
}
72+
5973
private void ValidateModelState(LambdaExpression actionCall)
6074
{
6175
var arguments = ExpressionParser.ResolveMethodArguments(actionCall).ToArray();
@@ -71,19 +85,5 @@ private void ValidateModelState(LambdaExpression actionCall)
7185
});
7286
}
7387
}
74-
75-
private void SetActionDescriptor(MethodInfo methodInfo)
76-
{
77-
var controllerContext = this.TestContext.ComponentContext;
78-
if (controllerContext.ActionDescriptor?.MethodInfo == null)
79-
{
80-
var controllerActionDescriptorCache = this.Services.GetService<IControllerActionDescriptorCache>();
81-
if (controllerActionDescriptorCache != null)
82-
{
83-
controllerContext.ActionDescriptor
84-
= controllerActionDescriptorCache.TryGetActionDescriptor(methodInfo);
85-
}
86-
}
87-
}
8888
}
8989
}

src/MyTested.AspNetCore.Mvc.ViewComponents/Builders/ViewComponents/ViewComponentInvocationBuilder.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
using System.Threading.Tasks;
77
using Contracts.Invocations;
88
using Invocations;
9+
using Internal.Contracts;
10+
using Microsoft.Extensions.DependencyInjection;
11+
using Utilities;
12+
using Utilities.Extensions;
913

1014
public partial class ViewComponentBuilder<TViewComponent>
1115
{
@@ -23,8 +27,31 @@ public IViewComponentResultTestBuilder<TActionResult> InvokedWith<TActionResult>
2327
return new ViewComponentResultTestBuilder<TActionResult>(this.TestContext);
2428
}
2529

26-
protected override void ProcessAndValidateMethod(LambdaExpression methodCall, MethodInfo methodInfo)
30+
protected override void ProcessAndValidateMethod(LambdaExpression invocationCall, MethodInfo methodInfo)
2731
{
32+
this.SetViewComponentDescriptor(methodInfo);
33+
this.ExtractArguments(invocationCall);
34+
}
35+
36+
private void SetViewComponentDescriptor(MethodInfo methodInfo)
37+
{
38+
var viewComponentContext = this.TestContext.ViewComponentContext;
39+
if (viewComponentContext.ViewComponentDescriptor?.MethodInfo == null)
40+
{
41+
var viewComponentDescriptorCache = this.Services.GetService<IViewComponentDescriptorCache>();
42+
if (viewComponentDescriptorCache == null)
43+
{
44+
viewComponentContext.ViewComponentDescriptor
45+
= viewComponentDescriptorCache.TryGetViewComponentDescriptor(methodInfo);
46+
}
47+
}
48+
}
49+
50+
private void ExtractArguments(LambdaExpression invocationCall)
51+
{
52+
ExpressionParser
53+
.ResolveMethodArguments(invocationCall)
54+
.ForEach(arg => this.TestContext.ViewComponentContext.Arguments.Add(arg.Name, arg.Value));
2855
}
2956
}
3057
}

0 commit comments

Comments
 (0)