Skip to content
This repository was archived by the owner on Jun 30, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
973fd6d
Initial support for a legacy API shim
kzu Jul 23, 2019
b83445a
Add codegen support for new Mock<T>
kzu Jul 23, 2019
a58ad2d
Setup should happen as soon as possible
kzu Jul 23, 2019
72a1a28
Make sure we use the test calling assembly
adalon Jul 24, 2019
df46b70
Added SkipBehavior support for behavior pipeline
adalon Jul 24, 2019
5aaca90
Add new behavior for setup scope runs
kzu Jul 24, 2019
0c2ff79
Add setup overloads to avoid the mock T argument
kzu Jul 24, 2019
60237fe
Added AsMoq extension method
adalon Jul 24, 2019
eda348b
Simplify Behavior implementation by using SkipBehaviors
kzu Jul 24, 2019
edc7b68
Revert "Add setup overloads to avoid the mock T argument"
kzu Jul 24, 2019
efeefc3
Fix failing tests
kzu Jul 24, 2019
4e9d640
Move setup scope to Moq
kzu Jul 24, 2019
004e96e
Fix visiblity of various Sdk-like classes in the Moq main assembly
kzu Jul 24, 2019
91b4853
Added CallBase support
adalon Jul 24, 2019
58a67fe
Don't run FixupImports twice
kzu Jul 25, 2019
3f191e7
Ensure both analyzers and codefixers have NuGetPackageId metadata
kzu Jul 25, 2019
4c5db10
Optimize codegen performance for real world solutions
kzu Jul 25, 2019
e159b48
Do not clean unused namespaces, since it is costly for little benefit
kzu Jul 25, 2019
814afdc
Bump to latest Roslyn for VS2017 and updated supported code fix names
kzu Jul 25, 2019
708db5b
Ensure a clean restore is performed always, add CI feed
kzu Jul 25, 2019
20b9fd2
Set proper names for CallBase tests
adalon Jul 25, 2019
6143a8a
Bump TFV to the 16.0+ official one supporting NS2
kzu Jul 29, 2019
e5b1c4e
Properly generate code for generic mocks
kzu Aug 3, 2019
d9bbc2e
Add support for mocking generic types
kzu Aug 6, 2019
34a6c49
Move OverrideAllMembersCodeFix to CodeFix assembly to avoid csc error
kzu Aug 6, 2019
1e97239
Don't assume mocked types will be public
kzu Aug 6, 2019
e30d526
Cleanup and encapsulate the batch code fixer and avoid state capturing
kzu Aug 6, 2019
f4b4be4
Delete OverrideAllMembersCodeFix class that moved to CodeFix
kzu Aug 6, 2019
d5e5fd7
Re-enable end to end tests for VB since they work now
kzu Aug 6, 2019
cfddaf2
Fix roslyn internals tests from moved RoslynInternals.cs file
kzu Aug 8, 2019
2e4f6e8
Fix minor style issues flagged by codefactor.io
kzu Aug 8, 2019
462cf05
Unify naming conventions for runtime lookup
kzu Aug 8, 2019
83a22b0
Minor docs tweaks to CallBase
kzu Aug 8, 2019
db87731
Drastically simplify As<T> support by adding new Mock<T...Tn>
kzu Aug 8, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add new behavior for setup scope runs
When the pipeline is run in a `SetupScope`, we should not run the entire pipeline, but rather just the behaviors we know are safe for execution during setup, specifically `MockContextBehavior` (to set up the `MockContext.CurrentInvocation` and `MockContext.CurrentSetup`) and `DefaultValueBehavior` to return a default value for further fluent API invocations to work on.

This simplifies the behaviors since they don't need to check and bail during active setup scopes, and makes them more self-contained.
  • Loading branch information
kzu committed Jul 24, 2019
commit 5aaca9093ad1dbfa37d046c4da70f950e7e2ae1c
29 changes: 29 additions & 0 deletions src/Moq/Moq.Sdk/SetupScopeBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Linq;
using Stunts;

namespace Moq.Sdk
{
/// <summary>
/// A behavior that skips all behaviors that do not apply during a setup scope.
/// </summary>
public class SetupScopeBehavior : IStuntBehavior
{
/// <summary>
/// Applies only if <see cref="SetupScope.IsActive"/> is <see langword="true"/>.
/// </summary>
public bool AppliesTo(IMethodInvocation invocation) => SetupScope.IsActive;

/// <summary>
/// Skips all non-setup behaviors from execution.
/// </summary>
public IMethodReturn Execute(IMethodInvocation invocation, GetNextBehavior next)
{
foreach (var behavior in invocation.Target.AsMock().Behaviors.Where(x => !(x is DefaultValueBehavior) && !(x is MockContextBehavior)))
{
invocation.SkipBehaviors.Add(behavior.GetType());
}

return next().Invoke(invocation, next);
}
}
}
16 changes: 2 additions & 14 deletions src/Moq/Moq.Sdk/StrictMockBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ namespace Moq.Sdk
/// </summary>
public class StrictMockBehavior : IStuntBehavior
{
IStuntBehavior fallback = new DefaultValueBehavior();

/// <summary>
/// Always returns <see langword="true" />
/// </summary>
Expand All @@ -20,16 +18,6 @@ public class StrictMockBehavior : IStuntBehavior
/// <summary>
/// Throws <see cref="StrictMockException"/>.
/// </summary>
public IMethodReturn Execute(IMethodInvocation invocation, GetNextBehavior next)
{
if (invocation == null) throw new ArgumentNullException(nameof(invocation));

if (!SetupScope.IsActive)
throw new StrictMockException();

// Otherwise, fallback to returning default values so that
// the fluent setup API can do its work.
return fallback.Execute(invocation, next);
}
public IMethodReturn Execute(IMethodInvocation invocation, GetNextBehavior next) => throw new StrictMockException();
}
}
}
11 changes: 10 additions & 1 deletion src/Moq/Moq.Tests/MoqTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using static Moq.Syntax;
using Stunts;
using Sample;
using System.Linq;
using Xunit.Abstractions;

namespace Moq.Tests
Expand All @@ -16,6 +15,16 @@ public class MoqTests

public MoqTests(ITestOutputHelper output) => this.output = output;

[Fact]
public void SetupDoesNotRecordCalls()
{
var calculator = Mock.Of<ICalculator>();

calculator.Setup(() => calculator.TurnOn());

Assert.Empty(calculator.AsMock().Invocations);
}

[Fact]
public void CanRaiseEvents()
{
Expand Down
1 change: 1 addition & 0 deletions src/Moq/Moq/MockInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static void Initialize(this IMocked mocked, MockBehavior behavior)
{
mocked.Mock.Behaviors.Clear();

mocked.Mock.Behaviors.Add(new SetupScopeBehavior());
mocked.Mock.Behaviors.Add(new MockContextBehavior());
mocked.Mock.Behaviors.Add(new MockRecordingBehavior());
mocked.Mock.Behaviors.Add(new EventBehavior());
Expand Down