elideus-dot-net-framework-tests
2.1.0
dotnet add package elideus-dot-net-framework-tests --version 2.1.0
NuGet\Install-Package elideus-dot-net-framework-tests -Version 2.1.0
<PackageReference Include="elideus-dot-net-framework-tests" Version="2.1.0" />
<PackageVersion Include="elideus-dot-net-framework-tests" Version="2.1.0" />
<PackageReference Include="elideus-dot-net-framework-tests" />
paket add elideus-dot-net-framework-tests --version 2.1.0
#r "nuget: elideus-dot-net-framework-tests, 2.1.0"
#:package elideus-dot-net-framework-tests@2.1.0
#addin nuget:?package=elideus-dot-net-framework-tests&version=2.1.0
#tool nuget:?package=elideus-dot-net-framework-tests&version=2.1.0
Elideus | DotNet Framework Tests
Tests is a subpackage of the Elideus-DotNet-Framework, and it sets up the boilerplate for testing a project developed with the framework.
Setup
To start, create a builder that implements the ElideusDotNetFrameworkTestsBuilder
:
[assembly: AssemblyFixture(typeof(MyAppTestsBuilder))]
namespace MyApp.Tests
{
public class MyAppTestsBuilder : ElideusDotNetFrameworkTestsBuilder
{
public MyAppTestsBuilder() : base()
{
}
}
}
This builder has an annotation that tells xUnit to share it across all tests in the same assembly. Only one instance of the builder will be created. The builder�s main role is to initialize dependencies and mocks.
Application Context Mock
You need to mock the ApplicationContext
, which holds app configurations and AutoMapper profiles.
The tests package provides ApplicationContextMock
, which handles basic mocking. You just need to override the Configurations
and MapperProfiles
properties:
public class MyAppContextMock : ApplicationContextMock
{
protected override Dictionary<string, string?> Configurations { get; set; } = new Dictionary<string, string?>
{
{ $"{Database}:{ConnectionString}", "1234connectionstring5678" },
};
protected override List<Profile> MapperProfiles { get; set; } = new List<Profile>
{
new UserMapperProfile(),
};
}
Now override Initialize
in the builder to apply this mock:
protected override void Initialize()
{
base.Initialize();
ApplicationContextMock = new MyAppContextMock().Mock();
}
Mocking Dependencies
You can also mock dependencies in the Initialize
method. For example, you can mock an authentication service:
public class MyAuthDependencyMock : IMyAuthDependency
{
public async Task<bool> Authenticate(MyMethodInput input)
{
return true;
}
}
And then register it like this:
ApplicationContextMock!.AddTestDependency(new MyAuthDependencyMock());
Now, IMyAuthDependency
will always resolve to your mock implementation.
Writing Tests
To write a test, create a class that inherits from OperationTest
. You need to specify the Operation, Input, and Output types.
In the constructor, use the ApplicationContextMock
from your test builder and instantiate the operation to test:
public class GetUsersOperationTests : OperationTest<GetUsersOperation, GetUsersInput, GetUsersOutput>
{
public GetUsersOperationTests(MyAppTestsBuilder _testBuilder) : base(_testBuilder)
{
OperationToTest = new GetUsersOperation(_testBuilder.ApplicationContextMock!, string.Empty);
}
}
Use the SimulateOperationToTestCall
method to call the operation in a test:
[Fact]
public async Task ShouldBe_Success()
{
var response = await SimulateOperationToTestCall(new GetUsersInput
{
OnlyAdminUsers = true,
});
Assert.True(response.Error == null);
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. 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. |
-
net9.0
- elideus-dot-net-framework-core (>= 2.1.0)
- SpecFlow.xUnit (>= 3.9.74)
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.1.0 | 179 | 4/10/2025 |