Many.Mocks 1.0.0

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

Install-Package Many.Mocks -Version 1.0.0
dotnet add package Many.Mocks --version 1.0.0
<PackageReference Include="Many.Mocks" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Many.Mocks --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Many.Mocks

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

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 mock objects from constructor

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

var result = mocks.UseToTryGetInstance(out UserManager result, (p) => p.Details.Type != typeof(IClass3)); //Get the instance filtering mocks used in ctor.

Many.Mocks

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

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 mock objects from constructor

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

var result = mocks.UseToTryGetInstance(out UserManager result, (p) => p.Details.Type != typeof(IClass3)); //Get the instance filtering mocks used in ctor.

  • .NETFramework 4.6.2

    • Moq (>= 4.13.1)
  • .NETStandard 2.0

    • 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 History

Version Downloads Last updated
1.0.0 180 6/12/2020
0.5.0 59 6/11/2020
0.3.7 157 11/25/2019
0.3.6 81 11/22/2019
0.3.5 85 11/22/2019
0.3.4 81 11/20/2019
0.3.3 79 11/19/2019
0.3.2 90 11/19/2019
0.3.1 87 11/19/2019
0.3.0-beta1 91 11/19/2019
0.2.0-beta1 84 11/18/2019