Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion AutofacContrib.NSubstitute.Tests/ExampleFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public void Example_test_with_concrete_type_provided()
const int val = 3;

using var mock = AutoSubstitute.Configure()
.Provide<IDependency2, Dependency2>(out _)
.Provide<IDependency2, Dependency2>()
.Build();

mock.Resolve<IDependency2>().SomeOtherMethod().Returns(val); // This shouldn't do anything because of the next line
Expand Down Expand Up @@ -173,8 +173,10 @@ public void Example_test_with_substitute_for_concrete_resolved_from_autofac()
const int val2 = 3;
const int val3 = 4;

#pragma warning disable CS0618 // Type or member is obsolete
using var mock = AutoSubstitute.Configure()
.ResolveAndSubstituteFor<ConcreteClassWithDependency>(new TypedParameter(typeof(int), val1))
#pragma warning restore CS0618 // Type or member is obsolete
.Build();

mock.Resolve<IDependency2>().SomeOtherMethod().Returns(val2);
Expand All @@ -184,5 +186,38 @@ public void Example_test_with_substitute_for_concrete_resolved_from_autofac()

Assert.That(result, Is.EqualTo(val2 * val3 * 2));
}

[Test]
public void Example_provide_service()
{
const int val1 = 2;
const int val2 = 3;
const int val3 = 4;

using var mock = AutoSubstitute.Configure()
.Provide<ConcreteClassWithDependency>(new TypedParameter(typeof(int), val1))
.Build();

mock.Resolve<IDependency2>().SomeOtherMethod().Returns(val2);
mock.Resolve<IDependency1>().SomeMethod(val1).Returns(val3);

var result = mock.Resolve<MyClassWithConcreteDependencyThatHasDependencies>().AMethod();

Assert.That(result, Is.EqualTo(val2 * val3 * 2));
}

[Test]
public void Example_provide_service_with_out_param()
{
const int val1 = 2;

using var mock = AutoSubstitute.Configure()
.Provide<ConcreteClassWithDependency>(out var c, new TypedParameter(typeof(int), val1))
.Build();

var result = mock.Resolve<ConcreteClassWithDependency>();

Assert.AreSame(result, c.Value);
}
}
}
14 changes: 14 additions & 0 deletions AutofacContrib.NSubstitute/AutoMock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.ComponentModel;

namespace AutofacContrib.NSubstitute
{
/// <summary>
/// Deprecated auto mocking container. Use <see cref="AutoSubstitute"/> instead.
/// </summary>
[Obsolete("AutoMock has been deprecated in favour of AutoSubstitute.")]
[EditorBrowsable(EditorBrowsableState.Never)]
public class AutoMock : AutoSubstitute
{
}
}
6 changes: 0 additions & 6 deletions AutofacContrib.NSubstitute/AutoSubstitute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@

namespace AutofacContrib.NSubstitute
{
/// <summary>
/// Deprecated auto mocking container. Use <see cref="AutoSubstitute"/> instead.
/// </summary>
[Obsolete("AutoMock has been deprecated in favour of AutoSubstitute.")]
public class AutoMock : AutoSubstitute { }

/// <summary>
/// Auto mocking container using <see cref="Autofac"/> and <see cref="NSubstitute"/>.
/// </summary>
Expand Down
65 changes: 64 additions & 1 deletion AutofacContrib.NSubstitute/AutoSubstituteBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,68 @@ public AutoSubstituteBuilder ConfigureBuilder(Action<ContainerBuilder> action)
}

/// <summary>
/// Register the specified implementation type to the container as the specified service type and resolve it using the given parameters.
/// Register the specified implementation type to the container as itself with the given parameterst.
/// </summary>
/// <typeparam name="TService">The type to register the implementation as</typeparam>
/// <typeparam name="TImplementation">The implementation type</typeparam>
/// <param name="parameters">Optional constructor parameters</param>
/// <returns>The current <see cref="AutoSubstituteBuilder"/>.</returns>
public AutoSubstituteBuilder Provide<TService>(params Parameter[] parameters)
=> Provide<TService, TService>(out _, parameters);

/// <summary>
/// Register the type with the given factory to the container.
/// </summary>
/// <typeparam name="TService">The type to register the implementation as</typeparam>
/// <param name="providedValue">Parameter to obtain a provided value.</param>
/// <param name="factory">The factory method to produce the service.</param>
/// <returns>The current <see cref="AutoSubstituteBuilder"/>.</returns>
public AutoSubstituteBuilder Provide<TService>(out IProvidedValue<TService> providedValue, Func<IComponentContext, TService> factory)
{
var key = new object();

_builder.Register(factory)
.Keyed<TService>(key)
.As<TService>()
.InstancePerLifetimeScope();

providedValue = CreateProvidedValue<TService>(c => c.ResolveKeyed<TService>(key));

return this;
}

/// <summary>
/// Register the type with the given factory to the container.
/// </summary>
/// <typeparam name="TService">The type to register the implementation as</typeparam>
/// <param name="factory">The factory method to produce the service.</param>
/// <returns>The current <see cref="AutoSubstituteBuilder"/>.</returns>
public AutoSubstituteBuilder Provide<TService>(Func<IComponentContext, TService> factory)
=> Provide(out _, factory);

/// <summary>
/// Register the specified implementation type to the container as itself with the given parameters.
/// </summary>
/// <typeparam name="TService">The type to register the implementation as</typeparam>
/// <typeparam name="TImplementation">The implementation type</typeparam>
/// <param name="providedValue">Parameter to obtain a provided value.</param>
/// <param name="parameters">Optional constructor parameters</param>
/// <returns>The current <see cref="AutoSubstituteBuilder"/>.</returns>
public AutoSubstituteBuilder Provide<TService>(out IProvidedValue<TService> providedValue, params Parameter[] parameters)
=> Provide<TService, TService>(out providedValue, parameters);

/// <summary>
/// Register the specified implementation type to the container as the specified service type with the given parameterst.
/// </summary>
/// <typeparam name="TService">The type to register the implementation as</typeparam>
/// <typeparam name="TImplementation">The implementation type</typeparam>
/// <param name="parameters">Optional constructor parameters</param>
/// <returns>The current <see cref="AutoSubstituteBuilder"/>.</returns>
public AutoSubstituteBuilder Provide<TService, TImplementation>(params Parameter[] parameters)
=> Provide<TService, TImplementation>(out _, parameters);

/// <summary>
/// Register the specified implementation type to the container as the specified service type with the given parameterst.
/// </summary>
/// <typeparam name="TService">The type to register the implementation as</typeparam>
/// <typeparam name="TImplementation">The implementation type</typeparam>
Expand Down Expand Up @@ -155,6 +216,8 @@ public SubstituteForBuilder<TService> SubstituteForPartsOf<TService>(params obje
/// <typeparam name="TService">The type to register and return a substitute for</typeparam>
/// <param name="parameters">Any constructor parameters that Autofac can't resolve automatically</param>
/// <returns>The current <see cref="AutoSubstituteBuilder"/>.</returns>
[Obsolete("Use a Provide method instead")]
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public AutoSubstituteBuilder ResolveAndSubstituteFor<TService>(params Parameter[] parameters) where TService : class
{
_builder.RegisterType<TService>()
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void Example_test_with_concrete_type_provided()
{
const int val = 3;
using var autoSubstitute = AutoSubstitute.Configure()
.Provide<IDependency2, Dependency2>(out _)
.Provide<IDependency2, Dependency2>()
.Build();
autoSubstitute.Resolve<IDependency2>().SomeOtherMethod().Returns(val); // This shouldn't do anything because of the next line
autoSubstitute.Resolve<IDependency1>().SomeMethod(Arg.Any<int>()).Returns(c => c.Arg<int>());
Expand All @@ -128,6 +128,14 @@ public void Example_test_with_concrete_type_provided()
Assert.That(result, Is.EqualTo(Dependency2.Value));
}

[Test]
public void Example_test_with_concrete_object_provide_by_parameter()
{
using var mock = AutoSubstitute.Configure()
.Provide<ConcreteClassWithDependency>(new TypedParameter(typeof(int), 5))
.Build();
}

[Test]
public void Example_test_with_concrete_object_provide()
{
Expand Down