Skip to content

Commit 338c33d

Browse files
authored
Merge 49de734 into 962e1bb
2 parents 962e1bb + 49de734 commit 338c33d

File tree

5 files changed

+94
-66
lines changed

5 files changed

+94
-66
lines changed

AutofacContrib.NSubstitute.Tests/ExampleFixture.cs

Lines changed: 36 additions & 1 deletion
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
@@ -210,8 +210,10 @@ public void Example_test_with_substitute_for_concrete_resolved_from_autofac()
210210
const int val2 = 3;
211211
const int val3 = 4;
212212

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

217219
mock.Resolve<IDependency2>().SomeOtherMethod().Returns(val2);
@@ -221,5 +223,38 @@ public void Example_test_with_substitute_for_concrete_resolved_from_autofac()
221223

222224
Assert.That(result, Is.EqualTo(val2 * val3 * 2));
223225
}
226+
227+
[Test]
228+
public void Example_provide_service()
229+
{
230+
const int val1 = 2;
231+
const int val2 = 3;
232+
const int val3 = 4;
233+
234+
using var mock = AutoSubstitute.Configure()
235+
.Provide<ConcreteClassWithDependency>(new TypedParameter(typeof(int), val1))
236+
.Build();
237+
238+
mock.Resolve<IDependency2>().SomeOtherMethod().Returns(val2);
239+
mock.Resolve<IDependency1>().SomeMethod(val1).Returns(val3);
240+
241+
var result = mock.Resolve<MyClassWithConcreteDependencyThatHasDependencies>().AMethod();
242+
243+
Assert.That(result, Is.EqualTo(val2 * val3 * 2));
244+
}
245+
246+
[Test]
247+
public void Example_provide_service_with_out_param()
248+
{
249+
const int val1 = 2;
250+
251+
using var mock = AutoSubstitute.Configure()
252+
.Provide<ConcreteClassWithDependency>(out var c, new TypedParameter(typeof(int), val1))
253+
.Build();
254+
255+
var result = mock.Resolve<ConcreteClassWithDependency>();
256+
257+
Assert.AreSame(result, c.Value);
258+
}
224259
}
225260
}
Lines changed: 14 additions & 0 deletions
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

Lines changed: 0 additions & 6 deletions
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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,38 @@ public AutoSubstituteBuilder ConfigureBuilder(Action<ContainerBuilder> action)
5050
}
5151

5252
/// <summary>
53-
/// Register the specified implementation type to the container as the specified service type and resolve it using the given parameters.
53+
/// Register the specified implementation type to the container as itself with the given parameterst.
54+
/// </summary>
55+
/// <typeparam name="TService">The type to register the implementation as</typeparam>
56+
/// <typeparam name="TImplementation">The implementation type</typeparam>
57+
/// <param name="parameters">Optional constructor parameters</param>
58+
/// <returns>The current <see cref="AutoSubstituteBuilder"/>.</returns>
59+
public AutoSubstituteBuilder Provide<TService>(params Parameter[] parameters)
60+
=> Provide<TService, TService>(out _, parameters);
61+
62+
/// <summary>
63+
/// Register the specified implementation type to the container as itself with the given parameterst.
64+
/// </summary>
65+
/// <typeparam name="TService">The type to register the implementation as</typeparam>
66+
/// <typeparam name="TImplementation">The implementation type</typeparam>
67+
/// <param name="providedValue">Parameter to obtain a provided value.</param>
68+
/// <param name="parameters">Optional constructor parameters</param>
69+
/// <returns>The current <see cref="AutoSubstituteBuilder"/>.</returns>
70+
public AutoSubstituteBuilder Provide<TService>(out IProvidedValue<TService> providedValue, params Parameter[] parameters)
71+
=> Provide<TService, TService>(out providedValue, parameters);
72+
73+
/// <summary>
74+
/// Register the specified implementation type to the container as the specified service type with the given parameterst.
75+
/// </summary>
76+
/// <typeparam name="TService">The type to register the implementation as</typeparam>
77+
/// <typeparam name="TImplementation">The implementation type</typeparam>
78+
/// <param name="parameters">Optional constructor parameters</param>
79+
/// <returns>The current <see cref="AutoSubstituteBuilder"/>.</returns>
80+
public AutoSubstituteBuilder Provide<TService, TImplementation>(params Parameter[] parameters)
81+
=> Provide<TService, TImplementation>(out _, parameters);
82+
83+
/// <summary>
84+
/// Register the specified implementation type to the container as the specified service type with the given parameterst.
5485
/// </summary>
5586
/// <typeparam name="TService">The type to register the implementation as</typeparam>
5687
/// <typeparam name="TImplementation">The implementation type</typeparam>
@@ -137,6 +168,8 @@ public SubstituteForBuilder<TService> SubstituteFor<TService>(params object[] pa
137168
/// <typeparam name="TService">The type to register and return a substitute for</typeparam>
138169
/// <param name="parameters">Any constructor parameters that Autofac can't resolve automatically</param>
139170
/// <returns>The current <see cref="AutoSubstituteBuilder"/>.</returns>
171+
[Obsolete("Use a Provide method instead")]
172+
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
140173
public AutoSubstituteBuilder ResolveAndSubstituteFor<TService>(params Parameter[] parameters) where TService : class
141174
{
142175
_builder.RegisterType<TService>()

README.md

Lines changed: 10 additions & 58 deletions
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,15 @@ 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()
133+
{
134+
using var mock = AutoSubstitute.Configure()
135+
.Provide<ConcreteClassWithDependency>(new TypedParameter(typeof(int), 5))
136+
.Build();
137+
}
138+
```
139+
131140
[Test]
132141
public void Example_test_with_concrete_object_provide()
133142
{
@@ -187,63 +196,6 @@ public void SubstituteForConfigureWithContext()
187196
}
188197
```
189198

190-
Similarly, you can resolve a concrete type from the autosubstitute container and register that with the underlying container using the `ResolveAndSubstituteFor` method:
191-
192-
```c#
193-
public class ConcreteClassWithDependency
194-
{
195-
private readonly IDependency1 _dependency;
196-
private readonly int _i;
197-
198-
public ConcreteClassWithDependency(IDependency1 dependency, int i)
199-
{
200-
_dependency = dependency;
201-
_i = i;
202-
}
203-
204-
public int Double()
205-
{
206-
return _dependency.SomeMethod(_i)*2;
207-
}
208-
}
209-
210-
public class MyClassWithConcreteDependencyThatHasDependencies
211-
{
212-
private readonly ConcreteClassWithDependency _c;
213-
private readonly IDependency2 _d2;
214-
215-
public MyClassWithConcreteDependencyThatHasDependencies(ConcreteClassWithDependency c, IDependency2 d2)
216-
{
217-
_c = c;
218-
_d2 = d2;
219-
}
220-
221-
public int AMethod()
222-
{
223-
return _d2.SomeOtherMethod() * _c.Double();
224-
}
225-
}
226-
227-
...
228-
229-
[Test]
230-
public void Example_test_with_substitute_for_concrete_resolved_from_autofac()
231-
{
232-
const int val1 = 2;
233-
const int val2 = 3;
234-
const int val3 = 4;
235-
using var AutoSubstitute = AutoSubstitute.Configure()
236-
.ResolveAndSubstituteFor<ConcreteClassWithDependency>(new TypedParameter(typeof(int), val1))
237-
.Build();
238-
AutoSubstitute.Resolve<IDependency2>().SomeOtherMethod().Returns(val2);
239-
AutoSubstitute.Resolve<IDependency1>().SomeMethod(val1).Returns(val3);
240-
241-
var result = AutoSubstitute.Resolve<MyClassWithConcreteDependencyThatHasDependencies>().AMethod();
242-
243-
Assert.That(result, Is.EqualTo(val2*val3*2));
244-
}
245-
```
246-
247199
If you need to access the underlying Autofac container for some reason then you use the `Container` property on the `AutoSubstitute` object.
248200

249201
If you want to make modifications to the container builder before the container is build from it there is a second constructor parameter you can use, for example:

0 commit comments

Comments
 (0)