YT.IIGen
0.0.23115.7
dotnet add package YT.IIGen --version 0.0.23115.7
NuGet\Install-Package YT.IIGen -Version 0.0.23115.7
<PackageReference Include="YT.IIGen" Version="0.0.23115.7" />
paket add YT.IIGen --version 0.0.23115.7
#r "nuget: YT.IIGen, 0.0.23115.7"
// Install YT.IIGen as a Cake Addin #addin nuget:?package=YT.IIGen&version=0.0.23115.7 // Install YT.IIGen as a Cake Tool #tool nuget:?package=YT.IIGen&version=0.0.23115.7
IIGen
Interface and implementation generator for .NET types.
This library is useful in case you want to have possibilities to mock static classes, classes without interfaces and structs in your tests.
Requirements
- .NET 6 and higher
How to use
1. Install NuGet package
Use any approach you prefer to install the NuGet package YT.IIGen
.
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 YT.IIGen.Attributes;
[IIFor(typeof(DateTime), "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());
}
}
Known limitations
- The types without default constructor are not supported yet.
- Generic methods are not supported yet.
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 | |
---|---|---|---|
0.0.23115.7 | 519 | 4/25/2023 | |
0.0.23114.5 | 172 | 4/24/2023 |
- Fixed generation methods with optional enum parameters.
- Fixed generation methods with optional string parameters.
- Fixed generation methods with optional floating-point numeric parameters.
- Fixed generation methods with optional reference-type parameters.