Skip to content

Commit f801ed8

Browse files
author
Robert Lindblom
committed
Possibility to override type if construction fails.
1 parent 5e845d7 commit f801ed8

File tree

5 files changed

+44
-1
lines changed

5 files changed

+44
-1
lines changed

AutoMockerNSubstitute.1.0.1.nupkg

11.3 KB
Binary file not shown.

source/NSubstituteAutoMocker.UnitTests/AutoMockerNSubstitute.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ public void ThrowsExceptionIfNoMatchAvailable()
7373
Assert.Throws<ConstructorMatchException>(() => new NSubstituteAutoMocker<ClassWithAllConstructors>([typeof(IDependency2), typeof(IDependency1)]));
7474
}
7575

76+
[Fact]
77+
public void ThrowsExceptionIfNotSubstitutableParam()
78+
{
79+
Assert.Throws<ArgumentException>(() => new NSubstituteAutoMocker<ClassWithNotSubstitutableConstructorParam>());
80+
}
81+
7682
[Fact]
7783
public void CanOverrideParameterViaEvent()
7884
{
@@ -101,6 +107,19 @@ public void CanOverrideParameterViaEventAlt()
101107

102108
Assert.IsAssignableFrom<Dependency1VitoImplementation>(autoMocker.ClassUnderTest.Dependency1);
103109
}
110+
111+
[Fact]
112+
public void ThrowsExceptionIfOverrideIsNull()
113+
{
114+
Assert.Throws<ArgumentException>(() => new NSubstituteAutoMocker<ClassWithNotSubstitutableConstructorParam>((_, _) => null));
115+
}
116+
117+
[Fact]
118+
public void CanOverrideNotSubstitutableParam()
119+
{
120+
new NSubstituteAutoMocker<ClassWithNotSubstitutableConstructorParam>((paramInfo, obj) =>
121+
paramInfo.ParameterType == typeof(ConcreteClass) ? new ConcreteClassForTest() : obj);
122+
}
104123
}
105124

106125
public class Get
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace AutoMockerNSubstitute.UnitTests.SamplesToTest;
2+
3+
public class ClassWithNotSubstitutableConstructorParam(ConcreteClass testClass);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
namespace AutoMockerNSubstitute.UnitTests.SamplesToTest;
2+
3+
public class ConcreteClass(int param);
4+
public class ConcreteClassForTest() : ConcreteClass(0);

source/NSubstituteAutoMocker/NSubstituteAutoMocker.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,28 @@ public NSubstituteAutoMocker(Type[] parameterTypes, Func<ParameterInfo, object,
3535
foreach (ParameterInfo info in parameters)
3636
{
3737
Type type = info.ParameterType;
38-
var constructorArg = CreateInstance(type);
38+
object constructorArg = null;
39+
ArgumentException exception = null;
40+
41+
try
42+
{
43+
constructorArg = CreateInstance(type);
44+
}
45+
catch (ArgumentException e)
46+
{
47+
// Give caller a chance to override the parameter.
48+
exception = e;
49+
}
50+
3951
if (parameterOverrideFunc != null)
4052
{
4153
constructorArg = parameterOverrideFunc(info, constructorArg);
4254
}
55+
56+
if (constructorArg == null && exception != null)
57+
{
58+
throw exception;
59+
}
4360
_constructors.Add(info, constructorArg);
4461
}
4562

0 commit comments

Comments
 (0)