DynaMock is a lightweight .NET library for dynamic dependency injection mock creation and testing, especially tailored for use with xUnit and Moq. It simplifies:
- Generating mock DI containers dynamically
- Invoking private methods for unit testing
- Reducing boilerplate when your service has many dependencies
- Automatic mock resolution: Instantiates classes with many dependencies without manually wiring up each mock
- Invoke private / non-public members: Call and test private or internal methods via reflection
- Integration with Moq: Retrieve
Mock<T>instances to set up behavior, verify calls, etc. - Designed for xUnit / unit testing: Helps reduce test setup complexity
- Clone or add dynamock as a dependency in your test project.
- Add the project reference or install via your package manager / project file.
// Automatically creates an instance of TestService
var testService = DynaMock.NewInstance<TestService>();// Assuming TestService depends on IRepository5
testService.GetMock<IRepository5>()
.Setup(repo => repo.GetNumber())
.Returns(42);testService.DoSomething();
// Your public method can run using the dynamically created mocksvar result = testService.InvokeMethod("PrivateMethodName", arg1, arg2);
// `InvokeMethod` will call the private method via reflection and return the resultpublic class TestServiceTests
{
private readonly TestService _service;
public TestServiceTests()
{
_service = DynaMock.NewInstance<TestService>();
}
[Fact]
public void PrivateMethod_WhenCondition_ShouldReturnExpected()
{
// Arrange
_service.GetMock<IRepository5>()
.Setup(r => r.GetNumber())
.Returns(10);
// Act
var result = _service.InvokeMethod("Foo_2", 1, 2);
// Assert
Assert.Equal(12, result);
}
}/DynamicMock # Main library project
/Sample # Sample / Test project demonstrating usage
├─ TestService.cs
├─ IRepository1.cs
├─ IRepository5.cs
└─ TestServiceTests.cs
- Dynamic DI Container
DynaMock.NewInstance<T>()uses reflection to create a new instance ofTand automatically instantiates mocks for its dependencies. - Mock retrieval
Each dependency is exposed as aMock<TDependency>so you can configure behaviors. - Private method invocation
InvokeMethoduses reflection to find and execute a private / internal method by name and parameter values.
- You have a class with many constructor dependencies, and writing manual mocks is tedious.
- You want to unit test private methods (e.g., for complex internal logic).
- You use xUnit + Moq and prefer a DRY test setup.
- You want to simplify test maintenance by reducing boilerplate.
- Reflection-based invocation of private methods may break with obfuscation or certain access modifiers.
- Overuse of testing non-public methods can lead to fragile tests; prefer testing behavior via public interfaces when feasible.
- While dynamic DI is convenient, explicit mock registration may still be needed for very complex dependency graphs or non-default constructors.
- Fork the repo
- Create a feature branch (
git checkout -b feat/YourFeature) - Commit your changes (
git commit -m 'Add new feature') - Push to the branch (
git push origin feat/YourFeature) - Open a Pull Request
This project is licensed under the Apache License 2.0. See the LICENSE file for details.
- Inspired by common needs in unit testing for dependency injection and private logic coverage
- Built with Moq and xUnit in mind