Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/moq/Moq.AutoMocker
An auto-mocking IoC container for Moq
https://github.com/moq/Moq.AutoMocker
hacktoberfest
Last synced: about 1 month ago
JSON representation
An auto-mocking IoC container for Moq
- Host: GitHub
- URL: https://github.com/moq/Moq.AutoMocker
- Owner: moq
- License: mit
- Created: 2012-05-24T05:12:08.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2024-04-29T06:30:59.000Z (8 months ago)
- Last Synced: 2024-05-01T21:17:45.280Z (7 months ago)
- Topics: hacktoberfest
- Language: C#
- Homepage:
- Size: 776 KB
- Stars: 377
- Watchers: 12
- Forks: 54
- Open Issues: 15
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- awesome-reference-tools - AutoMocker
README
# Moq.AutoMock ![Continuous](https://github.com/moq/Moq.AutoMocker/workflows/Continuous/badge.svg) [![NuGet Status](https://img.shields.io/nuget/v/Moq.AutoMock.svg?style=flat)](https://www.nuget.org/packages/Moq.AutoMock)
An automocking container for Moq. Use this if you're invested in your IoC
container and want to decouple your unit tests from changes to their
constructor arguments.Usage
======Simplest usage is to build an instance that you can unit test.
```csharp
var mocker = new AutoMocker();
var car = mocker.CreateInstance();car.DriveTrain.ShouldNotBeNull();
car.DriveTrain.ShouldImplement();
Mock mock = Mock.Get(car.DriveTrain);
```If you have a special instance that you need to use, you can register it
with `.Use(...)`. This is very similar to registrations in a regular IoC
container (i.e. `For().Use(x)` in StructureMap).```csharp
var mocker = new AutoMocker();mocker.Use(new DriveTrain());
// OR, setup a Mock
mocker.Use(x => x.Shaft.Length == 5);var car = mocker.CreateInstance();
```Extracting Mocks
----------------At some point you might need to get to a mock that was auto-generated. For
this, use the `.Get<>()` or `.GetMock<>()` methods.```csharp
var mocker = new AutoMocker();// Let's say you have a setup that needs verifying
mocker.Use(x => x.Accelerate(42) == true);var car = mocker.CreateInstance();
car.Accelerate(42);// Then extract & verify
var driveTrainMock = mocker.GetMock();
driveTrainMock.VerifyAll();
```Alternately, there's an even faster way to verify all mocks in the container:
```csharp
var mocker = new AutoMocker();
mocker.Use(x => x.Accelerate(42) == true);var car = mocker.CreateInstance();
car.Accelerate(42);// This method verifies all mocks in the container
mocker.VerifyAll();
```