Skip to content

Commit de2a408

Browse files
author
Mahmoud Ali
committed
Reuse Provide implementation for generic and non-generic methods
1 parent b18d67f commit de2a408

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

src/FakeItEasy.AutoFakeIt/AutoFakeIt.cs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ public class AutoFakeIt
2020
/// <returns>A class with all its dependencies faked.</returns>
2121
/// <exception cref="ArgumentException">Throws an ArgumentException if we can't find a public constructor
2222
/// with "fakeable" dependencies.</exception>
23-
public T Generate<T>() where T : class => (T)Generate(typeof(T));
23+
public T Generate<T>() where T : notnull => (T)Generate(typeof(T));
2424

2525
/// <summary>
26-
/// Generates a object automatically injecting FakeItEasy's fakes for all its dependencies.
26+
/// Generates an object of the specified type automatically injecting FakeItEasy's fakes for all its dependencies.
2727
/// </summary>
28-
/// <param name="type">The class you want to generate, usually your System Under Test.</param>
28+
/// <param name="type">The type of the class you want to generate, usually your System Under Test.</param>
2929
/// <returns>A class with all its dependencies faked.</returns>
3030
/// <exception cref="ArgumentException">Throws an ArgumentException if we can't find a public constructor
3131
/// with "fakeable" dependencies.</exception>
@@ -87,27 +87,22 @@ private void InsertMissingFakedObjects(Dictionary<Type, object> candidateFakeObj
8787
/// </summary>
8888
/// <typeparam name="T">The fake object you want to retrieve.</typeparam>
8989
/// <returns>An object of the given type, either previously provided or a new one just generated with FakeItEasy.</returns>
90-
public T Resolve<T>() where T : notnull
91-
{
92-
if (_fakedObjects.ContainsKey(typeof(T)))
93-
return (T)_fakedObjects[typeof(T)];
94-
95-
_fakedObjects[typeof(T)] = A.Fake<T>();
96-
return (T)_fakedObjects[typeof(T)];
97-
}
90+
public T Resolve<T>() where T : notnull => (T)Resolve(typeof(T));
9891

9992
/// <summary>
100-
/// Returns the object used for <param name="type" />. If an object of the given type was not explicitly
93+
/// Returns the object used for the type passed. If an object of the given type was not explicitly
10194
/// provided, it will create a FakeItEasy's fake that will be used for all subsequent calls.
10295
/// </summary>
10396
/// <returns>An object of the given type, either previously provided or a new one just generated with FakeItEasy.</returns>
10497
public object Resolve(Type type)
10598
{
10699
if (_fakedObjects.ContainsKey(type))
107100
return _fakedObjects[type];
108-
109-
_fakedObjects[type] = Create.Fake(type);
110-
return _fakedObjects[type];
101+
else
102+
{
103+
_fakedObjects[type] = Create.Fake(type);
104+
return _fakedObjects[type];
105+
}
111106
}
112107

113108

@@ -127,6 +122,7 @@ public object Resolve(Type type)
127122
/// <see cref="Resolve"/> or for a dependency on <see cref="Generate"/>. </summary>
128123
/// <remarks><para>This is useful for providing your own specific instance of a type, either because you can't
129124
/// use an automatically generated fake, or because you have a concrete type that you prefer to use.</para>
125+
/// <param name="registerType">The type of the object you want to provide.</param>
130126
/// <param name="dependency">The object you want to set.</param>
131127
/// <para>It will override any previously registered object of the same type of <param name="registerType"/>.</para></remarks>
132128
/// <exception cref="ArgumentException">Throws an ArgumentException if dependency is not assignable to registerType.</exception>

tests/FakeItEasy.AutoFakeIt.UnitTests/Specs/AutoFakeItTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,5 +159,16 @@ public void GenerateShouldUseProvidedDependency()
159159
var sut = autoFakeIt.Generate<DependenciesSut>();
160160
sut.DependencyA.Should().Be(depA);
161161
}
162+
163+
[Test]
164+
public void GenericResolveShouldWorkWithNonGenericProvide()
165+
{
166+
var autoFakeIt = new AutoFakeIt();
167+
var depA = new DependencyA();
168+
autoFakeIt.Provide(typeof(DependencyA), depA);
169+
170+
var resolvedDepA = autoFakeIt.Resolve<DependencyA>();
171+
resolvedDepA.Should().NotBeNull();
172+
}
162173
}
163174
}

0 commit comments

Comments
 (0)