Skip to content

Commit 1dc9ad9

Browse files
authored
Add provide method that only takes a service (#44)
Fixes #9
1 parent b175d28 commit 1dc9ad9

File tree

5 files changed

+123
-9
lines changed

5 files changed

+123
-9
lines changed

AutofacContrib.NSubstitute.Tests/ExampleFixture.cs

+36-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public void Example_test_with_concrete_type_provided()
138138
const int val = 3;
139139

140140
using var mock = AutoSubstitute.Configure()
141-
.Provide<IDependency2, Dependency2>(out _)
141+
.Provide<IDependency2, Dependency2>()
142142
.Build();
143143

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

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

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

185187
Assert.That(result, Is.EqualTo(val2 * val3 * 2));
186188
}
189+
190+
[Test]
191+
public void Example_provide_service()
192+
{
193+
const int val1 = 2;
194+
const int val2 = 3;
195+
const int val3 = 4;
196+
197+
using var mock = AutoSubstitute.Configure()
198+
.Provide<ConcreteClassWithDependency>(new TypedParameter(typeof(int), val1))
199+
.Build();
200+
201+
mock.Resolve<IDependency2>().SomeOtherMethod().Returns(val2);
202+
mock.Resolve<IDependency1>().SomeMethod(val1).Returns(val3);
203+
204+
var result = mock.Resolve<MyClassWithConcreteDependencyThatHasDependencies>().AMethod();
205+
206+
Assert.That(result, Is.EqualTo(val2 * val3 * 2));
207+
}
208+
209+
[Test]
210+
public void Example_provide_service_with_out_param()
211+
{
212+
const int val1 = 2;
213+
214+
using var mock = AutoSubstitute.Configure()
215+
.Provide<ConcreteClassWithDependency>(out var c, new TypedParameter(typeof(int), val1))
216+
.Build();
217+
218+
var result = mock.Resolve<ConcreteClassWithDependency>();
219+
220+
Assert.AreSame(result, c.Value);
221+
}
187222
}
188223
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.ComponentModel;
3+
4+
namespace AutofacContrib.NSubstitute
5+
{
6+
/// <summary>
7+
/// Deprecated auto mocking container. Use <see cref="AutoSubstitute"/> instead.
8+
/// </summary>
9+
[Obsolete("AutoMock has been deprecated in favour of AutoSubstitute.")]
10+
[EditorBrowsable(EditorBrowsableState.Never)]
11+
public class AutoMock : AutoSubstitute
12+
{
13+
}
14+
}

AutofacContrib.NSubstitute/AutoSubstitute.cs

-6
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@
44

55
namespace AutofacContrib.NSubstitute
66
{
7-
/// <summary>
8-
/// Deprecated auto mocking container. Use <see cref="AutoSubstitute"/> instead.
9-
/// </summary>
10-
[Obsolete("AutoMock has been deprecated in favour of AutoSubstitute.")]
11-
public class AutoMock : AutoSubstitute { }
12-
137
/// <summary>
148
/// Auto mocking container using <see cref="Autofac"/> and <see cref="NSubstitute"/>.
159
/// </summary>

AutofacContrib.NSubstitute/AutoSubstituteBuilder.cs

+64-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,68 @@ public AutoSubstituteBuilder ConfigureBuilder(Action<ContainerBuilder> action)
7272
}
7373

7474
/// <summary>
75-
/// Register the specified implementation type to the container as the specified service type and resolve it using the given parameters.
75+
/// Register the specified implementation type to the container as itself with the given parameterst.
76+
/// </summary>
77+
/// <typeparam name="TService">The type to register the implementation as</typeparam>
78+
/// <typeparam name="TImplementation">The implementation type</typeparam>
79+
/// <param name="parameters">Optional constructor parameters</param>
80+
/// <returns>The current <see cref="AutoSubstituteBuilder"/>.</returns>
81+
public AutoSubstituteBuilder Provide<TService>(params Parameter[] parameters)
82+
=> Provide<TService, TService>(out _, parameters);
83+
84+
/// <summary>
85+
/// Register the type with the given factory to the container.
86+
/// </summary>
87+
/// <typeparam name="TService">The type to register the implementation as</typeparam>
88+
/// <param name="providedValue">Parameter to obtain a provided value.</param>
89+
/// <param name="factory">The factory method to produce the service.</param>
90+
/// <returns>The current <see cref="AutoSubstituteBuilder"/>.</returns>
91+
public AutoSubstituteBuilder Provide<TService>(out IProvidedValue<TService> providedValue, Func<IComponentContext, TService> factory)
92+
{
93+
var key = new object();
94+
95+
_builder.Register(factory)
96+
.Keyed<TService>(key)
97+
.As<TService>()
98+
.InstancePerLifetimeScope();
99+
100+
providedValue = CreateProvidedValue<TService>(c => c.ResolveKeyed<TService>(key));
101+
102+
return this;
103+
}
104+
105+
/// <summary>
106+
/// Register the type with the given factory to the container.
107+
/// </summary>
108+
/// <typeparam name="TService">The type to register the implementation as</typeparam>
109+
/// <param name="factory">The factory method to produce the service.</param>
110+
/// <returns>The current <see cref="AutoSubstituteBuilder"/>.</returns>
111+
public AutoSubstituteBuilder Provide<TService>(Func<IComponentContext, TService> factory)
112+
=> Provide(out _, factory);
113+
114+
/// <summary>
115+
/// Register the specified implementation type to the container as itself with the given parameters.
116+
/// </summary>
117+
/// <typeparam name="TService">The type to register the implementation as</typeparam>
118+
/// <typeparam name="TImplementation">The implementation type</typeparam>
119+
/// <param name="providedValue">Parameter to obtain a provided value.</param>
120+
/// <param name="parameters">Optional constructor parameters</param>
121+
/// <returns>The current <see cref="AutoSubstituteBuilder"/>.</returns>
122+
public AutoSubstituteBuilder Provide<TService>(out IProvidedValue<TService> providedValue, params Parameter[] parameters)
123+
=> Provide<TService, TService>(out providedValue, parameters);
124+
125+
/// <summary>
126+
/// Register the specified implementation type to the container as the specified service type with the given parameterst.
127+
/// </summary>
128+
/// <typeparam name="TService">The type to register the implementation as</typeparam>
129+
/// <typeparam name="TImplementation">The implementation type</typeparam>
130+
/// <param name="parameters">Optional constructor parameters</param>
131+
/// <returns>The current <see cref="AutoSubstituteBuilder"/>.</returns>
132+
public AutoSubstituteBuilder Provide<TService, TImplementation>(params Parameter[] parameters)
133+
=> Provide<TService, TImplementation>(out _, parameters);
134+
135+
/// <summary>
136+
/// Register the specified implementation type to the container as the specified service type with the given parameterst.
76137
/// </summary>
77138
/// <typeparam name="TService">The type to register the implementation as</typeparam>
78139
/// <typeparam name="TImplementation">The implementation type</typeparam>
@@ -159,6 +220,8 @@ public SubstituteForBuilder<TService> SubstituteForPartsOf<TService>(params obje
159220
/// <typeparam name="TService">The type to register and return a substitute for</typeparam>
160221
/// <param name="parameters">Any constructor parameters that Autofac can't resolve automatically</param>
161222
/// <returns>The current <see cref="AutoSubstituteBuilder"/>.</returns>
223+
[Obsolete("Use a Provide method instead")]
224+
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
162225
public AutoSubstituteBuilder ResolveAndSubstituteFor<TService>(params Parameter[] parameters) where TService : class
163226
{
164227
_builder.RegisterType<TService>()

README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public void Example_test_with_concrete_type_provided()
118118
{
119119
const int val = 3;
120120
using var autoSubstitute = AutoSubstitute.Configure()
121-
.Provide<IDependency2, Dependency2>(out _)
121+
.Provide<IDependency2, Dependency2>()
122122
.Build();
123123
autoSubstitute.Resolve<IDependency2>().SomeOtherMethod().Returns(val); // This shouldn't do anything because of the next line
124124
autoSubstitute.Resolve<IDependency1>().SomeMethod(Arg.Any<int>()).Returns(c => c.Arg<int>());
@@ -128,6 +128,14 @@ public void Example_test_with_concrete_type_provided()
128128
Assert.That(result, Is.EqualTo(Dependency2.Value));
129129
}
130130

131+
[Test]
132+
public void Example_test_with_concrete_object_provide_by_parameter()
133+
{
134+
using var mock = AutoSubstitute.Configure()
135+
.Provide<ConcreteClassWithDependency>(new TypedParameter(typeof(int), 5))
136+
.Build();
137+
}
138+
131139
[Test]
132140
public void Example_test_with_concrete_object_provide()
133141
{

0 commit comments

Comments
 (0)