-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathViewComponentTester.cs
73 lines (63 loc) · 3.11 KB
/
ViewComponentTester.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq.Expressions;
using System.Threading.Tasks;
namespace SparkyTestHelpers.AspNetMvc.Core
{
/// <summary>
/// ASP.NET Core <see cref="ViewComponent"/> tester.
/// </summary>
/// <typeparam name="TViewComponent">The <see cref="ViewComponent"/> type.</typeparam>
public class ViewComponentTester<TViewComponent> where TViewComponent : class
{
private readonly TViewComponent _viewComponent;
/// <summary>
/// Creates new <see cref="ViewComponentTester{TViewComponent}"/> instance.
/// </summary>
/// <param name="viewComponent">The <see cref="ViewComponent"/>.</param>
public ViewComponentTester(TViewComponent viewComponent)
{
_viewComponent = AssertIsViewComponent(viewComponent);
}
/// <summary>
/// Creates a new <see cref="ViewComponentInvocationTester"/> instance for a synchronous method.
/// </summary>
/// <param name="expression">Expression to specify the Invoke function to be tested.</param>
/// <returns>New <see cref="ViewComponentInvocationTester"/> instance.</returns>
public ViewComponentInvocationTester Invocation(Expression<Func<TViewComponent, Func<IViewComponentResult>>> expression)
{
Func<TViewComponent, Func<IViewComponentResult>> func = expression.Compile();
return new ViewComponentInvocationTester(_viewComponent, func(_viewComponent));
}
/// <summary>
/// Creates a new <see cref="ViewComponentInvocationTester"/> instance for an async method.
/// </summary>
/// <param name="expression">Expression to specify the InvokeAsync function to be tested.</param>
/// <returns>New <see cref="ViewComponentInvocationTester"/> instance.</returns>
public ViewComponentInvocationTester Invocation(Expression<Func<TViewComponent, Func<Task<IViewComponentResult>>>> expression)
{
Func<TViewComponent, Func<Task<IViewComponentResult>>> funcWithTask = expression.Compile();
Func<IViewComponentResult> func = () =>
{
Task<IViewComponentResult> task = funcWithTask(_viewComponent)();
task.Wait();
return task.Result;
};
return new ViewComponentInvocationTester(_viewComponent, func);
}
private TViewComponent AssertIsViewComponent(TViewComponent viewComponent)
{
Type thisType = typeof(TViewComponent);
if (TypeTester.IsOfType(viewComponent, typeof(ViewComponent))
|| thisType.Name.EndsWith("ViewComponent")
|| thisType.GetCustomAttributes(typeof(ViewComponentAttribute), inherit: true).Length > 0)
{
return viewComponent;
}
throw new ActionTestException(
"A ViewComponent must inherit from ViewComponent"
+ ", have a name ending with \"ViewComponent\", or have a [ViewComponent] attribute."
+ $" {thisType.FullName} is not a valid ViewComponent type.");
}
}
}