Skip to content

Automatically creates services for unit testing with all constructor parameters as Moq-objects

License

Notifications You must be signed in to change notification settings

dariusz-wozniak/Automoqer

 
 

Repository files navigation

Fork of the AutoMoqer that has updated dependencies (such as Moq).

Available at Nuget (fork): NuGet

Original readme just below the line 👇


This repository contains a nuget package called "Automoqer".

About Automoqer

The purpose of Automoqer is to ease the creation of services with constructor IoC in unit testing.

Basic usage

  1. Get Automoqer via NuGet.

  2. In your unit test, create an Automoqer instance like this:

using (var serviceMocker = new AutoMoqer<ServiceToCreate>().Build())
{	
	//Example definition of a dependency mock setup:
	serviceMocker.Param<ICustomerRepository>().Setup(m => m.FindCustomer(It.Is<int>(p => p == 1))).Returns(new Customer());

	//Access the service instance:
	var service = serviceMocker.Service;

	//Example verification of a method call
	serviceMocker.Param<ILogger>().Verify(m => m.Log(It.IsAny<string>));
}

Why using Automoqer

If your services are defined like this:

public class CustomerService 
{
	private readonly ILogger _logger;
	private readonly IUnitOfWork _unitOfWork;
	private readonly IAnotherDependency _anotherDependency;

	public CustomerService(
		ILogger logger,
		IUnitOfWork unitOfWork,
		IAnotherDependency anotherDependency	
	) 
	{
		_logger = logger;
		_unitOfWork = unitOfWork;
		_anotherDependency = anotherDependency;
	}

	//...
}

Then chances are that you have a lot of unit tests that looks like this:

[Fact]
public CreateNewCustomerSuccessfully()
{
	var loggerMock = new Mock<ILogger>();
	var unitOfWorkMock = new Mock<IUnitOfWork>();
	var anotherDependencyMock = new Mock<IAnotherDependency>();

	//Your Moq .Setup are defined here..

	var service = new CustomerService(
		loggerMock.Object,
		unitOfWorkMock.Object,
		anotherDependencyMock.Object
	);

	//Actual test-case goes here...

	//Your Moq .Verify are defined here...
}

This is quite tedious to write and if you need to change the service's dependencies, you'll have a lot of test cases to change.

Automoqer removes this boilerplate for you by automatically create a Service with its constructor parameters as Moq-objects:

[Fact]
public CreateNewCustomerSuccessfully()
{
    using (var serviceMocker = new AutoMoqer<CustomerService>().Build())
    {
		//Your Moq .Setup are defined here..
		//Mocks accessed by serviceMocker.Param<ILogger>().Setup(...

		//Actual test-case goes here...
		//Service accessed by serviceMocker.Service

		//Your Moq .Verify are defined here...
	}	
}

It also runs VerifyAll() on all Moq-objects in its Dispose-method (hence the IDisposable-pattern)

Advanced usage

Provide parameter instances

You can make exceptions from having Automoqer automatically create Moq-objects from all constructor parameters. This is done by using one of the .With methods available on the AutoMoqer instance. Please note that these exceptions will not be available through the .Param method on the Automoqer-container.

By parameter type

This is how you provide your own instance of a parameter by it's type:

var logger = new TestLogger();

using (var serviceMocker = new AutoMoqer<CustomerService>()
	.With<ILogger>(logger)
	.Build())
{
	//...
}

By parameter name

This is how you provide your own instance of a parameter by it's type:

var logger = new TestLogger();

using (var serviceMocker = new AutoMoqer<CustomerService>()
	.With("logger", logger)
	.Build())
{
	//...
}			

Contributors

License and usage

MIT License

Copyright (c) 2016 Robert Bengtsson

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Automatically creates services for unit testing with all constructor parameters as Moq-objects

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%