Many.Mocks 2.1.0

There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Many.Mocks --version 2.1.0
NuGet\Install-Package Many.Mocks -Version 2.1.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Many.Mocks" Version="2.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Many.Mocks --version 2.1.0
#r "nuget: Many.Mocks, 2.1.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Many.Mocks as a Cake Addin
#addin nuget:?package=Many.Mocks&version=2.1.0

// Install Many.Mocks as a Cake Tool
#tool nuget:?package=Many.Mocks&version=2.1.0

Many.Mocks

Time-saving extensions to create and setup large number of mocks using Moq framework.

👉Download releases at https://www.nuget.org/packages/Many.Mocks/

How to use

You have a class with lots of mocks...

public class UserManager : UserManager<User>
    {
        /// <summary>
        /// Ctor. 1
        /// </summary>
        public UserManager(IUserStore<User> store, 
                            IOptions<IdentityOptions> optionsAccessor)
        {
        }
        /// <summary>
        /// Ctor. 2
        /// </summary>
        public UserManager(IUserStore<User> store, 
                            IOptions<IdentityOptions> optionsAccessor,
                            IPasswordHasher<User> passwordHasher, 
                            IEnumerable<IUserValidator<User>> userValidators, 
                            IEnumerable<IPasswordValidator<User>> passwordValidators, 
                            ILookupNormalizer keyNormalizer, 
                            IdentityErrorDescriber errors, 
                            IServiceProvider services,
                            ILogger<UserManager<User>> logger)
            : base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger)
        {
            
        }

        public void Method(IUserPasswordStore<User> store) { ... }
        public void Method(IUserStore<TUser> store) { ... }
    }

How to generate a bag of mocks from any method or constructor?

  1. You can get a bag of nedeed mocks just typing:
var mocks = typeof(UserManager).GetMocksFromConstructors(); //11 mocks: 2 from ctor. 1 and 9 from ctor. 2
  1. And also you can get only mocks from one specific constructor:
var mocks = typeof(UserManager).GetMocksFromConstructors(new List<Type>{ typeof(IUserStore<User>), typeof(IOptions<IdentityOptions>) }); //2 mocks from ctor. 1
  1. Same works for any method:
var mocks = typeof(UserManager).GetMocksFrom("method", new List<Type>{ typeof(IUserPasswordStore<User>) }); //1 mock

If a class is not proxiable and no mock can be created you can check it in the details:

var noMockCouldBeGeneratedForTheseClasses = mocks.Mocks.Where(p => !p.Generated); //Get the errors
var ex = noMockCouldBeGeneratedForTheseClasses.Error; //The thrown exception during mock generation

How to generate a mock for every property type of a class/interface?

Just ask for them 😃

var mocks = typeof(UserManager).GetMocksFromProperties();

How to instantiate a class injecting a bag of mocks?

var mocks = typeof(UserManager)
                .GetMocksFromConstructors()
                .Mocks.Select(p => p.Details); //Get mocks from constructor

var result = mocks.UseToTryInstantiate(out UserManager result); //Get the instance

How to instantiate a class injecting a bag of mocks and custom ones?

var mocks = typeof(UserManager)
                .GetMocksFromConstructors()
                .Mocks.Select(p => p.Details); //Get mocks from constructor

var customMockToReplace = new Mock<IServiceProvider>();
var result = mocks.UseToTryInstantiate(new List<Mock>() {customMockToReplace}, out UserManager result); //Get the instance replacing your custom mock
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.1

    • Moq (>= 4.13.1)

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.0.0-beta1 222 4/7/2022
2.1.0 481 3/31/2022
2.0.0 339 12/6/2021
1.1.0 462 11/29/2020
1.0.0 2,541 6/12/2020
0.5.0 746 6/11/2020
0.3.7 776 11/25/2019
0.3.6 672 11/22/2019
0.3.5 687 11/22/2019
0.3.4 656 11/20/2019
0.3.3 654 11/19/2019
0.3.2 647 11/19/2019
0.3.1 674 11/19/2019
0.3.0-beta1 548 11/19/2019
0.2.0-beta1 502 11/18/2019

v.2.1.0
- Performance improvements when getting mocks
v.2.0.0
- MockItem is at Many.Mocks namespace now
- Minor issues and documentation errors fixed
- Support for NETFramework removed. Only Netstandard 2.1 now
- Documentation Xml file available