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

Commit 2f2a0c3

Browse files
committed
Centralized setting up default mock behaviors
By moving the set up of the default mock behaviors to the extension method IMocked.SetBehaviors(MockBehavior), we can reuse directly from both VB and C# content files APIs, as well as provide uniformity for custom mocks created manually that also want to have the default mock behaviors set up quickly and consistently with factory-created Mock.Of<T> mocks.
1 parent 298c953 commit 2f2a0c3

File tree

4 files changed

+44
-35
lines changed

4 files changed

+44
-35
lines changed

src/Moq/Moq/MockedExtensions.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.ComponentModel;
2+
using Moq.Sdk;
3+
using Stunts;
4+
5+
namespace Moq
6+
{
7+
[EditorBrowsable(EditorBrowsableState.Advanced)]
8+
public static class MockedExtensions
9+
{
10+
/// <summary>
11+
/// Clears any existing behavior (including any setups) and
12+
/// adds the necessary behaviors to the <paramref name="mock"/> so
13+
/// that it behaves as specified by the <paramref name="behavior"/>
14+
/// enumeration.
15+
/// </summary>
16+
/// <remarks>
17+
/// This method can be used by custom mocks to ensure they have the
18+
/// same default behaviors as a mock created using <c>Mock.Of{T}</c>.
19+
/// </remarks>
20+
public static void SetBehavior(this IMocked mock, MockBehavior behavior)
21+
{
22+
mock.Mock.Behaviors.Clear();
23+
24+
mock.Mock.Behaviors.Add(new MockTrackingBehavior());
25+
mock.Mock.Behaviors.Add(new EventBehavior());
26+
mock.Mock.Behaviors.Add(new PropertyBehavior { SetterRequiresSetup = behavior == MockBehavior.Strict });
27+
mock.Mock.Behaviors.Add(new DefaultEqualityBehavior());
28+
29+
if (behavior == MockBehavior.Strict)
30+
mock.Mock.Behaviors.Add(new StrictMockBehavior());
31+
else
32+
mock.Mock.Behaviors.Add(new DefaultValueBehavior());
33+
}
34+
}
35+
}

src/Moq/Moq/contentFiles/cs/netstandard1.3/Mock.Overloads.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
using System;
2-
using System.Reflection;
3-
using Moq.Sdk;
4-
using Stunts;
5-
61
namespace Moq
72
{
83
/// <summary>
94
/// Instantiates stunts for the specified types.
105
/// </summary>
11-
internal partial class Mock
6+
partial class Mock
127
{
138
/// <summary>
149
/// Creates a mock that inherits or implements the type <typeparamref name="T"/>.

src/Moq/Moq/contentFiles/cs/netstandard1.3/Mock.cs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,23 @@
11
using System;
22
using System.Reflection;
33
using Moq.Sdk;
4-
using Stunts;
54

65
namespace Moq
76
{
87
/// <summary>
9-
/// Instantiates stunts for the specified types.
8+
/// Instantiates mocks for the specified types.
109
/// </summary>
11-
internal partial class Mock
10+
partial class Mock
1211
{
12+
/// <summary>
13+
/// Creates the mock instance by using the specified types to
14+
/// lookup the mock type in the assembly defining this class.
15+
/// </summary>
1316
static T Create<T>(MockBehavior behavior, object[] constructorArgs, params Type[] interfaces)
1417
{
1518
var mocked = (IMocked)MockFactory.Default.CreateMock(typeof(Mock).GetTypeInfo().Assembly, typeof(T), interfaces, constructorArgs);
1619

17-
mocked.Mock.Behaviors.Add(new MockTrackingBehavior());
18-
19-
if (behavior == MockBehavior.Strict)
20-
{
21-
mocked.Mock.Behaviors.Add(new PropertyBehavior { SetterRequiresSetup = true });
22-
mocked.Mock.Behaviors.Add(new StrictMockBehavior());
23-
}
24-
else
25-
{
26-
mocked.Mock.Behaviors.Add(new PropertyBehavior());
27-
mocked.Mock.Behaviors.Add(new DefaultValueBehavior());
28-
}
29-
30-
mocked.Mock.Behaviors.Add(new DefaultEqualityBehavior());
20+
mocked.SetBehavior(behavior);
3121

3222
return (T)mocked;
3323
}

src/Moq/Moq/contentFiles/vb/netstandard1.3/Mock.vb

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
Imports System.Reflection
22
Imports Moq.Sdk
3-
Imports Stunts
43

54
Namespace Global.Moq
65

@@ -9,17 +8,7 @@ Namespace Global.Moq
98
Private Shared Function Create(Of T)(ByVal behavior As MockBehavior, ByVal constructorArgs As Object(), ParamArray interfaces As Type()) As T
109
Dim mocked = DirectCast(MockFactory.[Default].CreateMock(GetType(Mock).GetTypeInfo().Assembly, GetType(T), interfaces, constructorArgs), IMocked)
1110

12-
mocked.Mock.Behaviors.Add(New MockTrackingBehavior())
13-
14-
If behavior = MockBehavior.Strict Then
15-
mocked.Mock.Behaviors.Add(New PropertyBehavior() With {.SetterRequiresSetup = True})
16-
mocked.Mock.Behaviors.Add(New StrictMockBehavior())
17-
Else
18-
mocked.Mock.Behaviors.Add(New PropertyBehavior())
19-
mocked.Mock.Behaviors.Add(New DefaultValueBehavior())
20-
End If
21-
22-
mocked.Mock.Behaviors.Add(New DefaultEqualityBehavior())
11+
mocked.SetBehavior(behavior)
2312

2413
Return DirectCast(mocked, T)
2514
End Function

0 commit comments

Comments
 (0)