Tum4ik.StinimGen
2.1.24171.1
See the version list below for details.
dotnet add package Tum4ik.StinimGen --version 2.1.24171.1
NuGet\Install-Package Tum4ik.StinimGen -Version 2.1.24171.1
<PackageReference Include="Tum4ik.StinimGen" Version="2.1.24171.1" />
paket add Tum4ik.StinimGen --version 2.1.24171.1
#r "nuget: Tum4ik.StinimGen, 2.1.24171.1"
// Install Tum4ik.StinimGen as a Cake Addin #addin nuget:?package=Tum4ik.StinimGen&version=2.1.24171.1 // Install Tum4ik.StinimGen as a Cake Tool #tool nuget:?package=Tum4ik.StinimGen&version=2.1.24171.1
<h1 align="center"> <img src="logo.png" alt="StinimGen" style="width:128px;" /> <br/> StinimGen </h1> <h1 align="center">
Interface and implementation generator for static members.
This library is useful in case you want to have possibilities to mock static classes or static members in your tests.
Requirements
- .NET 6 and higher
Philosophy
Stinim
should be associated with the words: static, interface, implementation,Gen
- generator.- Static (also const) fields are converted to the properties.
- Static events, properties and methods are converted to the events, properties and methods respectively.
- All instance (non-static) members are ignored.
How to use
1. Install NuGet package
Use any approach you prefer to install the NuGet package Tum4ik.StinimGen
.
2. Declare an interface
Declare your interface for a type you want to make mockable, use IIForAttribute
to specify the type to wrap
and the name of the wrapper class to be generated.
using Tum4ik.StinimGen.Attributes;
[IIFor(typeof(DateTime), WrapperClassName = "DateTimeWrapper")]
internal partial interface IDateTime
{
}
3. Register
Register the declared interface and the generated wrapper class in the DI container.
container.Register<IDateTime, DateTimeWrapper>();
4. Inject and use
Inject and use your wrapper in any place you need.
internal class MyService
{
private readonly IDateTime _dateTime;
public MyService(IDateTime dateTime)
{
_dateTime = dateTime;
}
public DateTime GetCurrentDateTime()
{
return _dateTime.Now;
}
}
5. Mock and test
Now you are able to mock the functionality as usual.
using Moq;
using Xunit;
public class MyServiceTests
{
private readonly Mock<IDateTime> _dateTimeMock = new()
private readonly MyService _testeeService;
public MyServiceTests()
{
_testeeService = new(_dateTimeMock.Object);
}
[Fact]
internal void GetCurrentDateTimeTest()
{
var now = DateTime.Now;
_dateTimeMock.Setup(dt => dt.Now).Returns(now);
Assert.Equal(now, _testeeService.GetCurrentDateTime());
}
}
API
Property | Default | Description |
---|---|---|
sourceType (constructor required) | - | The type to generate interface members and an implementation wrapper for |
WrapperClassName (required) | - | The implementation wrapper class name to generate |
IsPublic | false | Controls the generated implementation wrapper accessibility: true emits public access modifier, false - internal |
IsSealed | true | Controls the ability to inherit generated implementation wrapper: true emits sealed modifier, false - nothing |
IsPartial | false | Control the ability to extend the generated implementation wrapper with custom members: true emits partial keyword, false - nothing |
Advanced topics
Custom members
It may be possible you want to extend the declared interface with your own members and provide their implementations
in the generated wrapper. For that you need to use IsPartial
property.
using Tum4ik.StinimGen.Attributes;
[IIFor(typeof(DateTime), WrapperClassName = "DateTimeWrapper", IsPartial = true)]
internal partial interface IDateTime
{
DateTime Yesterday { get; }
DateTime Tomorrow { get; }
}
partial class DateTimeWrapper
{
public DateTime Yesterday => DateTime.Now.Subtract(TimeSpan.FromDays(1));
public DateTime Tomorrow => DateTime.Now.AddDays(1);
}
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 | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. 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.0
- No dependencies.
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 |
---|---|---|
2.2.24230.3 | 135 | 8/17/2024 |
2.1.24171.1 | 159 | 6/19/2024 |
2.0.24150.12 | 139 | 5/29/2024 |
2.0.24150.10 | 137 | 5/29/2024 |
2.0.24039.8 | 279 | 2/8/2024 |
2.0.24038.5-rc | 100 | 2/7/2024 |
2.0.24033.3-rc | 100 | 2/2/2024 |
2.0.24029.1-rc | 81 | 1/29/2024 |
1.0.24027.19 | 105 | 1/27/2024 |
1.0.24024.17 | 109 | 1/24/2024 |
1.0.24024.15-beta | 93 | 1/24/2024 |
1.0.24024.13-beta | 86 | 1/24/2024 |
1.0.24024.11-beta | 87 | 1/24/2024 |
1.0.24024.9-beta | 87 | 1/24/2024 |
1.0.24024.7-beta | 85 | 1/24/2024 |
1.0.24024.5-beta | 92 | 1/24/2024 |
1.0.24024.3-beta | 85 | 1/24/2024 |
- Added possibility to control the `partial` keyword of the generated implementation wrapper.