Many.Mocks 3.0.0-beta1

This is a prerelease version of Many.Mocks.
dotnet add package Many.Mocks --version 3.0.0-beta1                
NuGet\Install-Package Many.Mocks -Version 3.0.0-beta1                
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="3.0.0-beta1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Many.Mocks --version 3.0.0-beta1                
#r "nuget: Many.Mocks, 3.0.0-beta1"                
#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=3.0.0-beta1&prerelease

// Install Many.Mocks as a Cake Tool
#tool nuget:?package=Many.Mocks&version=3.0.0-beta1&prerelease                

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?

There are several ways depending on wether the default mocks are valid for you or not. But the easiest way is:

var mocks = typeof(UserManager)
                .GetMocksFromConstructors(); //Get mocks from constructor

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

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

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

var customMockToReplace = new Mock<IServiceProvider>();
var result = mocks.TryInstantiate(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.

v3.0.0-beta1
- UseToTryInstantiate() renamed to TryInstantiate()
- Now you can can instantiate a class using mocks from the bag (new UseToTryInstantiate overrides)
- Now you can invoke methods from the bag (new Invoke overrides)
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