Skip to content

Commit

Permalink
Quickstart
Browse files Browse the repository at this point in the history
  • Loading branch information
devodo committed Jan 6, 2023
1 parent ca67e89 commit 6cf51ae
Show file tree
Hide file tree
Showing 41 changed files with 349 additions and 1,127 deletions.
14 changes: 4 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dotnet add package DivertR

# Feature Summary

1. Test double proxy framework for mocking, faking, stubbing, spying, etc. [[more]](./docs/Redirect.md)
1. Test double proxy framework for mocking, faking, stubbing, spying, etc. [[more]](https://devodo.github.io/DivertR/redirects/)
2. Proxies that wrap and forward to root (original) instances so tests run against the integrated system whilst modifying and spying on specific parts as needed. [[more]](./docs/Redirect.md#proxy)
3. A lightweight, fluent interface for configuring proxies to redirect calls to delegates or substitute instances. [[more]](./docs/Redirect.md#via)
4. Dynamic update and reset of proxies in a running application enabling changes between tests without requiring restart and initialisation overhead. [[more]](./docs/Redirect.md#reset)
Expand Down Expand Up @@ -78,12 +78,6 @@ public async Task GivenFooRepoException_WhenGetFoo_ThenReturns500InternalServerE
}
```

# Quickstart

For more examples and a demonstration of setting up a test harness for a WebApp see this [WebApp Testing Sample](./test/DivertR.WebAppTests/WebAppTests.cs)
or follow below for a quickstart:

* [Redirects](./docs/Redirect.md) for creating and configuring proxies.
* [Dependency Injection](./docs/DI.md) integration.
* [Recording and Verifying](./docs/Verify.md) calls.
* [About](./docs/About.md) DivertR.
# Documentation
* [Documentation](https://devodo.github.io/DivertR/)
* [Quickstart](https://devodo.github.io/DivertR/quickstart/)
17 changes: 8 additions & 9 deletions docs/About.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# About
---
layout: default
title: About
nav_order: 3
---

# About

DivertR is similar to well known mocking frameworks like Moq or FakeItEasy but provides additional features for use with integration testing such as dynamically manipulating the dependency injection (DI) layer at runtime.
You can redirect calls to test doubles, such as substitute instances, mocks or delegates, and then optionally relay them back to the original services.
Expand All @@ -8,11 +14,4 @@ which also lets you customise the DI configuration, e.g. to substitute test doub

DivertR was born out of the need to efficiently modify DI configurations between tests running against the same TestServer instance.
It has grown into a framework that facilitates testing of wired up systems, bringing a familiar unit/mocking testing style into the realm of component and integration testing,
by providing features to conveniently substitute dependency behaviour (including error conditions) and verify inputs and outputs from recorded call information.

# DispatchProxy

The default DivertR proxy factory is implemented using [System.Reflection.DispatchProxy](https://learn.microsoft.com/en-us/dotnet/api/system.reflection.dispatchproxy). As this is part of the .NET Standard 2.1, DivertR therefore does not have any dependencies on external libraries however it is limited to proxying interface types only.

If class proxies are required, [DivertR.DynamicProxy](../src/DivertR.DynamicProxy/README.md) is an alternative proxy factory implemented using [Castle DynamicProxy](http://www.castleproject.org/projects/dynamicproxy/) that does support classes.
However, when proxying classes, care should be taken as only calls to non-virtual members cannot be intercepted, this can cause inconsistent behaviour e.g. when wrapping root instances.
by providing features to conveniently substitute dependency behaviour (including error conditions) and verify inputs and outputs from recorded call information.
27 changes: 0 additions & 27 deletions docs/dependency_injection/index.md

This file was deleted.

23 changes: 0 additions & 23 deletions docs/dependency_injection/proxy_lifetime.md

This file was deleted.

43 changes: 0 additions & 43 deletions docs/dependency_injection/redirect_configuration.md

This file was deleted.

35 changes: 0 additions & 35 deletions docs/dependency_injection/redirect_registration.md

This file was deleted.

39 changes: 0 additions & 39 deletions docs/dependency_injection/via_redirect.md

This file was deleted.

24 changes: 17 additions & 7 deletions docs/DI.md → docs/quickstart/dependency_injection.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
# Dependency Injection
---
layout: default
title: Dependency Injection
nav_order: 2
parent: Quickstart
---

# Dependency Injection

DivertR is designed to be embedded easily and transparently into the dependency injection (DI) container to facilitate testing an integrated, wired-up system.
It does this by decorating existing DI service registrations with [Redirects](./Redirect.md) that replace the originals.
Expand All @@ -7,9 +14,9 @@ These Redirects create proxies that wrap the instances resolved from the origina
By default Redirect proxies transparently forward calls to their roots and therefore, in this initial state, the behaviour of the DI system is unchanged.
Then specific parts of the system can be modified as required by dynamically updating and resetting proxies between tests without requiring restart.

# .NET ServiceCollection
## .NET ServiceCollection

Out the box DivertR has support for the .NET `Microsoft.Extensions.DependencyInjection.IServiceCollection`. The examples below use the following `ServiceCollection` and its registrations:
Out the box DivertR has support for the .NET `Microsoft.Extensions.DependencyInjection.IServiceCollection`. The examples that follow use this `ServiceCollection` and its registered dependencies:

```csharp
IServiceCollection services = new ServiceCollection();
Expand Down Expand Up @@ -50,7 +57,7 @@ Console.WriteLine(demo.Name); // "Foo";

# Redirect Configuration

The resolved `IFoo` instance above is a Redirect proxy generated by the underlying `IRedirect<IFoo>` decorator that uses the original DI registration to initialise the proxy root.
The resolved `IFoo` instance above is a Redirect proxy generated by the underlying `IRedirect<IFoo>` decorator that uses the original DI registration to initialise the proxy root.
In its initial state the `IFoo` proxy forwards all calls directly to its root. However, this behaviour can be modified by obtaining the underlying `Redirect`
from the `Diverter` instance and adding a *Via*:

Expand Down Expand Up @@ -85,7 +92,7 @@ Console.WriteLine(foo.Name); // "Foo"
Console.WriteLine(foo2.Name); // "Foo2"
```

# ViaRedirect
# Via Redirect

Sometimes a test needs to manipulate instances that are not directly created by the DI container.
E.g. if we assume the `IBarFactory` service registration given above is a factory that creates `IBar` instances.
Expand Down Expand Up @@ -115,9 +122,12 @@ diverter.ResetAll();
Console.WriteLine(bar.Name); // "MrBar"
```

`RedirectVia` intercepts the method return values and wraps them as proxies created from a Via.
It returns this Via that can then be used to control the behaviour of the proxy wrappers.

# Proxy Lifetime

DivertR aims to leave the original system behaviour unchanged and therefore
DivertR aims to leave the original system behaviour unchanged and therefore
when existing DI registrations are replaced by Redirect decorators the lifetime of the registration is preserved.

For multiple instance registrations such as transients, a separate proxy instance is created for each but all from the same Redirect instance.
Expand All @@ -130,4 +140,4 @@ If a DI created root instance implements the `IDisposable` interface then the DI
If a DI Redirect proxy is an `IDisposable` then **only** the proxy instance is disposed by the DI container and not the root.
In this case the responsibilty is left to the proxy for forwarding the dispose call to its root (and it does this by default).

The above also applies to `IAsyncDisposable`.
The above also applies to `IAsyncDisposable`.
Loading

0 comments on commit 6cf51ae

Please sign in to comment.