Skip to content
This repository was archived by the owner on Jun 30, 2023. It is now read-only.

Commit 0c2ff79

Browse files
committed
Add setup overloads to avoid the mock T argument
This allows simplified lambdas for setup, so that ``` mock.Setup(x => x.TurnOn()); ``` becomes ``` mock.Setup(() => mock.TurnOn()); ``` A future version could also add `Setup(() ...)` to Syntax for the same effect.
1 parent 5aaca90 commit 0c2ff79

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/Moq/Moq/SetupExtension.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ namespace Moq
1111
[EditorBrowsable(EditorBrowsableState.Never)]
1212
public static class SetupExtension
1313
{
14+
/// <summary>
15+
/// Sets up the mock with the given void method call.
16+
/// </summary>
17+
[SetupScope]
18+
public static ISetup Setup<T>(this T mock, Action action)
19+
{
20+
using (new SetupScope())
21+
{
22+
action();
23+
return DefaultSetup.Default;
24+
}
25+
}
26+
1427
/// <summary>
1528
/// Sets up the mock with the given void method call.
1629
/// </summary>
@@ -36,6 +49,18 @@ public static TResult Setup<T, TResult>(this T mock, Func<T, TResult> function)
3649
}
3750
}
3851

52+
/// <summary>
53+
/// Sets up the mock with the given function.
54+
/// </summary>
55+
[SetupScope]
56+
public static TResult Setup<T, TResult>(this T mock, Func<TResult> function)
57+
{
58+
using (new SetupScope())
59+
{
60+
return function();
61+
}
62+
}
63+
3964
/// <summary>
4065
/// Sets up the mock with the given method reference, typically used to
4166
/// access and set ref/out arguments. A code fix will automatically

0 commit comments

Comments
 (0)