-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPageModelTester.cs
57 lines (50 loc) · 2.2 KB
/
PageModelTester.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
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System;
using System.Linq.Expressions;
using System.Threading.Tasks;
namespace SparkyTestHelpers.AspNetMvc.Core
{
/// <summary>
/// ASP.NET Core MVC Razor Page <see cref="PageModel"/> tester.
/// </summary>
/// <typeparam name="TPageModel">The <see cref="PageModel"/> type..</typeparam>
public class PageModelTester<TPageModel> where TPageModel : PageModel
{
private readonly TPageModel _pageModel;
/// <summary>
/// Creates new <see cref="PageModelTester{TPageModel}"/> instance.
/// </summary>
/// <param name="pageModel">The <see cref="PageModel"/>.</param>
public PageModelTester(TPageModel pageModel)
{
_pageModel = pageModel;
}
/// <summary>
/// Creates a new <see cref="PageModelActionTester"/> instance.
/// </summary>
/// <param name="expression">Expression to specify the PageModel action to be tested.</param>
/// <returns>New <see cref="PageModelActionTester"/> instance.</returns>
public PageModelActionTester<TPageModel> Action(Expression<Func<TPageModel, Func<IActionResult>>> expression)
{
var func = expression.Compile();
return new PageModelActionTester<TPageModel>(_pageModel, func(_pageModel));
}
/// <summary>
/// Creates a new <see cref="PageModelActionTester"/> instance for an async action.
/// </summary>
/// <param name="expression">Expression to specify the async PageModel action to be tested.</param>
/// <returns>New <see cref="PageModelActionTester"/> instance.</returns>
public PageModelActionTester<TPageModel> Action(Expression<Func<TPageModel, Func<Task<IActionResult>>>> expression)
{
Func<TPageModel, Func<Task<IActionResult>>> funcWithTask = expression.Compile();
Func<IActionResult> func = () =>
{
Task<IActionResult> task = funcWithTask(_pageModel)();
task.Wait();
return task.Result;
};
return new PageModelActionTester<TPageModel>(_pageModel, func);
}
}
}