Releases: devlooped/moq
4.13.0
Changed
-
Improved error message that is supplied with
ArgumentExceptionthrown whenSetuporVerifyare called on a protected method if the method could not be found with both the name and compatible argument types specified (@thomasfdm, #852). -
mock.Invocations.Clear()now removes traces of previous invocations more thoroughly by additionally resetting all setups to an "unmatched" state. (@stakx, #854) -
Consistent
Callbackdelegate validation regardless of whether or notCallbackis preceded by aReturns: Validation for post-Returnscallback delegates used to be very relaxed, but is now equally strict as in the pre-Returnscase.) (@stakx, #876) -
Subscription to mocked events used to be handled less strictly than subscription to regular CLI events. As with the latter, subscribing to mocked events now also requires all handlers to have the same delegate type. (@stakx, #891)
-
Moq will throw when it detects that an argument matcher will never match anything due to the presence of an implicit conversion operator. (@michelcedric, #897, #898)
-
New algorithm for matching invoked methods against methods specified in setup/verification expressions. (@stakx, #904)
Added
-
Added support for setup and verification of the event handlers through
Setup[Add|Remove]andVerify[Add|Remove|All](@lepijohnny, #825) -
Added support for lambda expressions while creating a mock through
new Mock<SomeType>(() => new SomeType("a", "b"))andrepository.Create<SomeType>(() => new SomeType("a", "b")). This makes the process of mocking a class without a parameterless constructor simpler (compiler syntax checker...). (@frblondin, #884) -
Support for matching generic type arguments:
mock.Setup(m => m.Method<It.IsAnyType>(...)). (@stakx, #908)The standard type matchers are:
It.IsAnyType— matches any typeIt.IsSubtype<T>— matchesTand proper subtypes ofTIt.IsValueType— matches only value types
You can create your own custom type matchers:
[TypeMatcher] class Either<A, B> : ITypeMatcher { public bool Matches(Type type) => type == typeof(A) || type == typeof(B); }
-
In order to support type matchers (see bullet point above), some new overloads have been added to existing methods:
-
setup.Callback(new InvocationAction(invocation => ...)),
setup.Returns(new InvocationFunc(invocation => ...)):The lambda specified in these new overloads will receive an
IInvocationrepresenting the current invocation from which type arguments as well as arguments can be discovered. -
Match.Create<T>((object argument, Type parameterType) => ..., ...),
It.Is<T>((object argument, Type parameterType) => ...):Used to create custom matchers that work with type matchers. When a type matcher is used for
T, theargumentreceived by the custom matchers is untyped (object), and its actual type (or rather the type of the parameter for which the argument was passed) is provided via an additional parameterparameterType. (@stakx, #908)
-
Fixed
-
Moq does not mock explicit interface implementation and
protected virtualcorrectly. (@oddbear, #657) -
Invocations.Clear()does not causeVerifyto fail (@jchessir, #733) -
Regression:
SetupAllPropertiescan no longer set up properties whose names start withItem. (@mattzink, #870; @kaan-kaya, #869) -
Regression:
MockDefaultValueProviderwill no longer attempt to setCallBaseto true for mocks generated for delegates. (@dammejed, #874) -
VerifythrowsTargetInvocationExceptioninstead ofMockExceptionwhen one of the recorded invocations was to an async method that threw. (@Cufeadir, #883) -
Moq does not distinguish between distinct events if they have the same name (@stakx, #893)
-
Regression in 4.12.0:
SetupAllPropertiesremoves indexer setups. (@stakx, #901) -
Parameter types are ignored when matching an invoked generic method against setups. (@stakx, #903)
-
For
[Value]Task<object>,.ReturnsAsync(null)throwsNullReferenceExceptioninstead of producing a completed task with resultnull(@voroninp, #909)
4.12.0
Changed
- Improved performance for
Mock.Of<T>andmock.SetupAllProperties()as the latter now performs property setups just-in-time, instead of as an ahead-of-time batch operation. (@vanashimko, #826) - Setups with no
.Returns(…)nor.CallBase()no longer returndefault(T)for loose mocks, but a value that is consistent with the mock'sCallBaseandDefaultValue[Provider]settings. (@stakx, #849)
Added
- New method overload
sequenceSetup.ReturnsAsync(Func<T>)(@stakx, #841) - LINQ to Mocks support for strict mocks, i.e. new method overloads for
Mock.Of,Mocks.Of,mockRepository.Of, andmockRepository.OneOfthat accept aMockBehaviorparameter. (@stakx, #842)
Fixed
- Adding
Callbackto a mock breaks async tests (@marcin-chwedczuk-meow, #702) mock.SetupAllProperties()now setups write-only properties for strict mocks, so that accessing such properties will not throw anymore. (@vanashimko, #836)- Regression:
mock.SetupAllProperties()andMock.Of<T>fail due to inaccessible property accessors (@Mexe13, #845) - Regression:
VerifyNoOtherCallscauses stack overflow when mock setup returns the mocked object (@bash, #846) Capture.In()no longer captures arguments when other setup arguments do not match (@ocoanet, #844).CaptureMatchno longer invokes the capture callback when other setup arguments do not match (@ocoanet, #844).
4.11.0
Same as 4.11.0 Release Candidate 2. See changelog entries for the two pre-release versions.
4.11.0 Release Candidate 2
This is a pre-release version.
Changed
- Debug symbols (
Moq.pdb) have moved into a separate NuGet symbol package (as per the current official guideline). If you want the Visual Studio debugger to step into Moq's source code, disable Just My Code, enable SourceLink, and configure NuGet's symbol server. (@stakx, #789)
Fixed
4.11.0 Release Candidate 1
This is a pre-release version.
It contains several minor breaking changes, and there have been extensive internal rewrites in order to fix some very long-standing bugs in relation to argument matchers in fluent setup expressions.
Changed
-
The library now targets .NET Standard 2.0 instead of .NET Standard 1.x. This has been decided based on the official cross-platform targeting guideline and the End of Life announcement for .NET Core 1.x (@stakx, #784, #785)
-
Method overload resolution may change for:
mock.Protected().Setup("VoidMethod", ...)mock.Protected().Verify("VoidMethod", ...)mock.Protected().Verify<TResult>("NonVoidMethod", ...)
due to a new overload: If the first argument is a
bool, make sure that argument gets interpreted as part ofargs, not asexactParameterMatch(see also Added section below). (@stakx & @Shereef, #751, #753) -
mock.Verify[All]now performs a more thorough error aggregation. Error messages of inner/recursive mocks are included in the error message using indentation to show the relationship between mocks. (@stakx, #762) -
mock.Verifyno longer creates setups, nor will it override existing setups, as a side-effect of using a recursive expression. (@stakx, #765) -
More accurate detection of argument matchers with
SetupSetandVerifySet, especially when used in fluent setup expressions or with indexers (@stakx, #767) -
mock.Verify(expression)error messages now contain a full listing of all invocations that occurred across all involved mocks. Setups are no longer listed, since they are completely irrelevant in the context of call verification. (@stakx, #779, #780) -
Indexers used as arguments in setup expressions are now eagerly evaluated, like all other properties already are (except when they refer to matchers) (@stakx, #794)
-
Update package reference to
Castle.Core(DynamicProxy) from version 4.3.1 to 4.4.0 (@stakx, #797)
Added
-
New method overloads:
mock.Protected().Setup("VoidMethod", exactParameterMatch, args)mock.Protected().Verify("VoidMethod", times, exactParameterMatch, args)mock.Protected().Verify<TResult>("NonVoidMethod", times, exactParameterMatch, args)
having a
bool exactParameterMatchparameter. Due to method overload resolution, it was easy to think this already existed when in fact it did not, leading to failing tests. (@Shereef & @stakx, #753, #751) -
Ability in
mock.Raiseandsetup.Raisesto raise events on sub-objects (inner mocks) (@stakx, #772)
Removed
- Pex interop (which has not been maintained for years). You might notice changes when using Visual Studio's IntelliTest feature. (@stakx, #786)
Fixed
- Setting multiple indexed object's property directly via LINQ fails (@TylerBrinkley, #314)
InvalidOperationExceptionwhen specifiying setup on mock with mock containing property of typeNullable<T>(@dav1dev, #725)Verifygets confused between the same generic and non-generic signature (@lepijohnny, #749)- Setup gets included in
Verifydespite being "unreachable" (@stakx, #703) Verifycan create setups that cause subsequentVerifyAllto fail (@stakx & @lepijohnny, #699)- Incomplete stack trace when raising an event with
mock.Raisethrows (@MutatedTomato, #738) Mock.Raiseonly raises events on root object (@hallipr, #166)- Mocking indexer captures
It.IsAny()as the value, even if given in the indexer argument (@idigra, #696) VerifySetfails on non-trivial property setup (@TimothyHayes, #430)- Use of
SetupSet'forgets' method setup (@TimothyHayes, #432) - Recursive mocks don't work with argument matching (@thalesmello, #142)
- Recursive property setup overrides previous setups (@jamesfoster, #110)
- Formatting of enumerable object for error message broke EF Core test case (@MichaelSagalovich, #741)
Verify[All]fails because of lazy (instead of eager) setup argument expression evaluation (@aeslinger, #711)ArgumentOutOfRangeExceptionwhen setup expression contains indexer access (@mosentok, #714)- Incorrect implementation of
Times.Equals(@stakx, #805)
4.10.1
Fixed
NullReferenceExceptionwhen usingSetupSeton indexers with multiple parameters (@idigra, #694)CallBaseshould not be allowed for delegate mocks (@tehmantra, #706)
Changed
- Dropped the dependency on the
System.ValueTupleNuGet package, at no functional cost (i.e. value tuples are still supported just fine) (@stakx, #721) - Updated failure messages to show richer class names (@powerdude, #727)
- Upgraded
System.Reflection.TypeExtensionsandSystem.Threading.Tasks.Extensionsdependencies to versions 4.5.1 (@stakx, #729)
4.10.0
Added
ExpressionCompiler: An extensibility point for setting up alternate LINQ expression tree compilation strategies (@stakx, #647)setup.CallBase()forvoidmethods (@stakx, #664)VerifyNoOtherCallsforMockRepository(@BlythMeister, #682)
Changed
- Make
VerifyNoOtherCallstake into account previous calls to parameterlessVerify()andVerifyAll()(@stakx, #659) - Breaking change:
VerifyAllnow succeeds after a call toSetupAllPropertieseven when not all property accessors were invoked (stakx, #684)
Fixed
- More precise
outparameter detection for mocking COM interfaces with[in,out]parameters (@koutinho, #645) - Prevent false 'Different number of parameters' error with
Returnscallback methods that have been compiled fromExpressions (@stakx, #654) Verifyexception should report configured setups for delegate mocks (@stakx, #679)Verifyexception should include complete call expression for delegate mocks (@stakx, #680)- Bug report #556: "Recursive setup expression creates ghost setups that make
VerifyAllfail" (@stakx, #684) - Bug report #191: "Upgrade from 4.2.1409.1722 to 4.2.1507.0118 changed
VerifyAllbehavior" (@stakx, #684)
4.9.0
Added
- Add
Mock.Invocationsproperty to support inspection of invocations on a mock (@Tragedian, #560)
Changed
- Update package reference to
Castle.Core(DynamicProxy) from version 4.3.0 to 4.3.1 (@stakx, #635) - Floating point values are formatted with higher precision (satisfying round-tripping) in diagnostic messages (@stakx, #637)
Fixed
Obsoleted
4.8.3
Added
- Add
ISetupSequentialResult<TResult>.Returnsmethod overload that support delegate for deferred results (@snrnats, #594) - Support for C# 7.2's
inparameter modifier (@stakx, #624, #625) - Missing methods
ReturnsAsyncandThrowsAsyncfor sequential setups of methods returning aValueTask(@stakx, #626)
Changed
- Breaking change: All
ReturnsAsyncandThrowsAsyncsetup methods now consistently return a newTaskon each invocation (@snrnats, #595) - Speed up
Mock.Of<T>()by approx. one order of magnitude (@informatorius, #598) - Update package reference to
Castle.Core(DynamicProxy) from version 4.2.1 to 4.3.0 (@stakx, #624)