Moq v4.13.0 Release Notes

Release Date: 2019-08-31 // almost 3 years ago
  • 🔄 Changed

    • 👌 Improved error message that is supplied with ArgumentException thrown when Setup or Verify are 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 Callback delegate validation regardless of whether or not Callback is preceded by a Returns: Validation for post-Returns callback delegates used to be very relaxed, but is now equally strict as in the pre-Returns case.) (@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] and Verify[Add|Remove|All] (@lepijohnny, #825)

    • ➕ Added support for lambda expressions while creating a mock through new Mock<SomeType>(() => new SomeType("a", "b")) and repository.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 type
    • It.IsSubtype<T> — matches T and proper subtypes of T
    • It.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 IInvocation representing 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, the argument received 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 parameter parameterType. (@stakx, #908)

    🛠 Fixed

    • 🤡 Moq does not mock explicit interface implementation and protected virtual correctly. (@oddbear, #657)

    • Invocations.Clear() does not cause Verify to fail (@jchessir, #733)

    • Regression: SetupAllProperties can no longer set up properties whose names start with Item. (@mattzink, #870; @kaan-kaya, #869)

    • 🤡 Regression: MockDefaultValueProvider will no longer attempt to set CallBase to true for mocks generated for delegates. (@dammejed, #874)

    • 🤡 Verify throws TargetInvocationException instead of MockException when 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: SetupAllProperties removes indexer setups. (@stakx, #901)

    • Parameter types are ignored when matching an invoked generic method against setups. (@stakx, #903)

    • For [Value]Task<object>, .ReturnsAsync(null) throws NullReferenceException instead of producing a completed task with result null (@voroninp, #909)