Ahatornn.TestGenerator
1.1.29
dotnet add package Ahatornn.TestGenerator --version 1.1.29
NuGet\Install-Package Ahatornn.TestGenerator -Version 1.1.29
<PackageReference Include="Ahatornn.TestGenerator" Version="1.1.29" />
<PackageVersion Include="Ahatornn.TestGenerator" Version="1.1.29" />
<PackageReference Include="Ahatornn.TestGenerator" />
paket add Ahatornn.TestGenerator --version 1.1.29
#r "nuget: Ahatornn.TestGenerator, 1.1.29"
#:package Ahatornn.TestGenerator@1.1.29
#addin nuget:?package=Ahatornn.TestGenerator&version=1.1.29
#tool nuget:?package=Ahatornn.TestGenerator&version=1.1.29
TestGenerator
Data generator for test projects
Every time you write tests, you have to initialize data to test specific cases of your services. To separate models with default values from prepared models, use a managed data generator. This will allow you to verify the correct behavior of the business logic when checking tests and get rid of the unwanted effects of default behavior for empty models in the test.
Use TestEntityProvider.Shared.Create<>
for creating test entity. For example:
var actCarrier1 = TestEntityProvider.Shared.Create<ActCarrier>();
var actCarrierOrder1 = TestEntityProvider.Shared.Create<ActCarrierOrder>(x => x.ActCarrierId = actCarrier.Id);
var actCarrierOrder2 = TestEntityProvider.Shared.Create<ActCarrierOrder>(x =>
{
x.ActCarrierId = actCarrierDeleted.Id;
x.ExternalOrderId = orderId;
});
Method Create<> using instead
var actCarrier1 = new ActCarrier
{
Id = Guid.NewGuid(),
Currency = $"Currency{Guid.NewGuid():N}",
DocumentDate = DateTimeOffset.UtcNow,
DocumentNumber = $"DocumentNumber{Guid.NewGuid():N}",
ExternalContractId = Guid.NewGuid(),
ExternalContractorId = Guid.NewGuid(),
ExternalId = Guid.NewGuid(),
SourceUpdatedAt = DateTimeOffset.UtcNow,
};
By default, TestEntityProvider generates data for the following property types (if they are writable)
- String:
$"{propertyInfo.Name}{Guid.NewGuid():N}" - DateTime:
DateTime.Now - DateTimeOffset:
DateTimeOffset.Now - Guid:
Guid.NewGuid()
You can expand the list of available data generators by implementing the interface IPropertyValueGenerator. However, in this case you will need to use the TestEntityProvider instance obtained using the TestEntityProviderBuilder, rather than using TestEntityProvider.Shared.
Let's see how to implement a data generator for all boolean types.
First, let's implement IPropertyValueGenerator for the boolean type
public class BoolAlwaysTrueValueGenerator : IPropertyValueGenerator
{
Type IPropertyValueGenerator.PropertyValueType => typeof(bool);
void IPropertyValueGenerator.Generate<TEntity>([NotNull] TEntity entity, [NotNull] PropertyInfo propertyInfo)
where TEntity : class
{
if (!(propertyInfo.CanWrite && propertyInfo.PropertyType == typeof(bool)))
{
throw new InvalidOperationException($"The property {propertyInfo.Name} could not be write for {GetType().Name}");
}
propertyInfo.SetValue(entity, true);
}
}
Now let's create an TestEntityProviderBuilder by passing a custom IPropertyValueGenerator
var provider = new TestEntityProviderBuilder
.AddGenerator(new BoolAlwaysTrueValueGenerator())
.Build();
And finally use provider to generate data
var result = provider.Create<SimpleTestModel>();
If you regularly need to set the same values for certain properties when using TestEntityProvider, use the AddPreset mechanism. Just specify the necessary values for properties of a certain type when creating the TestEntityProvider
var provider = new TestEntityProviderBuilder
.AddPreset<SimpleTestModel>(x => x.Cost = 11.7m)
.Build();
Now every time you call provider.Create<SimpleTestModel>() you will receive a value of 11.7m for the Cost property of each model obtained using TestEntityProvider. Of course, if necessary, you can override the previously specified value provider.Create<SimpleTestModel>(x => x.Cost = 8.3m)
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 is compatible. 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 is compatible. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net6.0
- No dependencies.
-
net8.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.
v 1.1.29:
- TargetFrameworks: net8.0, net6.0
v 1.1.28:
- Now MIT license
v 1.1.25:
- Use TestEntityProviderBuilder for add some Presets or Generators
v 1.0.18:
- Created shared TestEntityProvider
Created shared TestEntityProvider