Many.Mocks
3.0.0-beta1
dotnet add package Many.Mocks --version 3.0.0-beta1
NuGet\Install-Package Many.Mocks -Version 3.0.0-beta1
<PackageReference Include="Many.Mocks" Version="3.0.0-beta1" />
paket add Many.Mocks --version 3.0.0-beta1
#r "nuget: Many.Mocks, 3.0.0-beta1"
// 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?
- 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
- 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
- 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 | Versions 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. |
-
.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 | 254 | 4/7/2022 | |
2.1.0 | 528 | 3/31/2022 | |
2.0.0 | 366 | 12/6/2021 | |
1.1.0 | 487 | 11/29/2020 | |
1.0.0 | 2,919 | 6/12/2020 | |
0.5.0 | 777 | 6/11/2020 | |
0.3.7 | 810 | 11/25/2019 | |
0.3.6 | 704 | 11/22/2019 | |
0.3.5 | 727 | 11/22/2019 | |
0.3.4 | 687 | 11/20/2019 | |
0.3.3 | 682 | 11/19/2019 | |
0.3.2 | 677 | 11/19/2019 | |
0.3.1 | 707 | 11/19/2019 | |
0.3.0-beta1 | 573 | 11/19/2019 | |
0.2.0-beta1 | 531 | 11/18/2019 |
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