From a9d73017cd09aaceb30bca268a2f5197a4afe60f Mon Sep 17 00:00:00 2001 From: stakx Date: Sun, 2 Jun 2019 19:25:35 +0200 Subject: [PATCH] Implement strict behavior for LINQ to Mocks --- src/Moq/Linq/Mock.cs | 5 ++++- src/Moq/Linq/MockRepository.cs | 5 ++++- src/Moq/Linq/MockSetupsBuilder.cs | 8 +++++--- src/Moq/Linq/Mocks.cs | 30 ++++++++++++++++++++++++++++-- 4 files changed, 41 insertions(+), 7 deletions(-) diff --git a/src/Moq/Linq/Mock.cs b/src/Moq/Linq/Mock.cs index e440cb404..c0bec848f 100644 --- a/src/Moq/Linq/Mock.cs +++ b/src/Moq/Linq/Mock.cs @@ -38,7 +38,10 @@ public static T Of(MockBehavior behavior) where T : class // which involved a lot of avoidable `IQueryable` query provider overhead and lambda compilation. // What it really boils down to is this (much faster) code: var mock = new Mock(behavior); - mock.SetupAllProperties(); + if (behavior != MockBehavior.Strict) + { + mock.SetupAllProperties(); + } return mock.Object; } diff --git a/src/Moq/Linq/MockRepository.cs b/src/Moq/Linq/MockRepository.cs index d98e5e58f..d714c64d2 100644 --- a/src/Moq/Linq/MockRepository.cs +++ b/src/Moq/Linq/MockRepository.cs @@ -141,7 +141,10 @@ private IEnumerable CreateMocks(MockBehavior behavior) where T : class do { var mock = this.Create(behavior); - mock.SetupAllProperties(); + if (behavior != MockBehavior.Strict) + { + mock.SetupAllProperties(); + } yield return mock.Object; } diff --git a/src/Moq/Linq/MockSetupsBuilder.cs b/src/Moq/Linq/MockSetupsBuilder.cs index b139bdb13..ef4f0f0d8 100644 --- a/src/Moq/Linq/MockSetupsBuilder.cs +++ b/src/Moq/Linq/MockSetupsBuilder.cs @@ -217,9 +217,11 @@ private static Expression ConvertToSetupProperty(Expression targetObject, Expres var mockExpression = propertyCall.Object; var propertyExpression = propertyCall.Arguments.First().StripQuotes(); - // Because Mocks.CreateMocks (the underlying implementation of the IQueryable provider - // already sets up all properties as stubs, we can safely just set the value here, - // which also allows the use of this querying capability against plain DTO even + // We can safely just set the value here since `SetProperty` will temporarily enable auto-stubbing + // if the underlying `IQueryable` provider implementation hasn't already enabled it permanently by + // calling `SetupAllProperties`. + // + // This method also enables the use of this querying capability against plain DTO even // if their properties are not virtual. var setPropertyMethod = typeof(Mocks) .GetMethod("SetProperty", BindingFlags.Static | BindingFlags.NonPublic) diff --git a/src/Moq/Linq/Mocks.cs b/src/Moq/Linq/Mocks.cs index 986dd8745..1be5e5d49 100644 --- a/src/Moq/Linq/Mocks.cs +++ b/src/Moq/Linq/Mocks.cs @@ -118,7 +118,10 @@ private static IEnumerable CreateMocks(MockBehavior behavior) where T : cl do { var mock = new Mock(behavior); - mock.SetupAllProperties(); + if (behavior != MockBehavior.Strict) + { + mock.SetupAllProperties(); + } yield return mock.Object; } @@ -135,7 +138,30 @@ internal static bool SetProperty(Mock target, Expression