Skip to content

Create an instance of an object by providing constructor parameters with the default mocked objects using Moq.

License

Notifications You must be signed in to change notification settings

benyblack/Moq.Inject

Repository files navigation

.NET Nuget Package

Moq.Inject

When I do unit testing (with XUnit and Moq) in a project full of controllers or any object that requires dependency injection, I often notice that a change in the constructor will cause several changes in the unit tests. If the project is in an early development stage with a lot of changes coming every day, this would be even more frustrating.

This library is an easy way to solve this problem. In some cases, you may need to instantiate your class just with some default mock classes:

[Fact]
public void Test_Something(){
    // Arrange
    var worker = Mock.Of<IWorker>();
    var configuration = Mock.Of<IConfiguration>();
    var logger = Mock.Of<ILogger>();
    var controller = new HomeController(worker, configuration, logger);
    
    // Act ...
}

With Moq.Inject it can be as simple as:

[Fact]
public void Test_Something(){
    // Arrange
    var controller = Injector.Create<HomeController>();
    
    // Act ...
}

Let's say we need to have a custom input for ILogger:

[Fact]
public void Test_Something(){
    // Arrange
    var logger = new Mock<ILogger>();
    logger.Setup(x=> AMethod()).Returns(something);
    var controller = new Injector<Controller>().Add("logger", logger.Object).Create();
    
    // Act ...
}

About

Create an instance of an object by providing constructor parameters with the default mocked objects using Moq.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages