Skip to content

Commit 488f458

Browse files
committed
Objects with a non nullable constructor argument (e.g. int) can now be mocked
1 parent dab0ca8 commit 488f458

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

source/NSubstituteAutoMocker.UnitTests/NSubstituteAutoMocker.UnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
<Compile Include="DocumentationSnippets\FirstExample.cs" />
5858
<Compile Include="NSubstituteAutoMockerTests.cs" />
5959
<Compile Include="Properties\AssemblyInfo.cs" />
60+
<Compile Include="SamplesToTest\ClassWithPrimativeConstructors.cs" />
6061
<Compile Include="SamplesToTest\SealedClass.cs" />
6162
<Compile Include="SamplesToTest\ClassWithJustDefaultConstructor.cs" />
6263
<Compile Include="SamplesToTest\ClassWithPrivateDefaultConstructor.cs" />

source/NSubstituteAutoMocker.UnitTests/NSubstituteAutoMockerTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ public void ClassUnderTestCanBeSealed()
4848
new NSubstituteAutoMocker<SealedClass>();
4949
Assert.IsNotNull(autoMocker.ClassUnderTest);
5050
}
51+
52+
[TestMethod]
53+
public void PrimativeParameterTypesGetSetToDefaultValue()
54+
{
55+
NSubstituteAutoMocker<ClassWithPrimativeConstructors> autoMocker =
56+
new NSubstituteAutoMocker<ClassWithPrimativeConstructors>();
57+
Assert.AreEqual(0, autoMocker.ClassUnderTest.IntValue);
58+
Assert.AreEqual(null, autoMocker.ClassUnderTest.StringValue);
59+
}
5160
}
5261

5362
[TestClass]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace NSubstituteAutoMocker.UnitTests.SamplesToTest
2+
{
3+
public class ClassWithPrimativeConstructors
4+
{
5+
public ClassWithPrimativeConstructors()
6+
{
7+
}
8+
9+
public ClassWithPrimativeConstructors(IDependency1 dependency1, IDependency2 dependency2, string stringValue, int intValue)
10+
{
11+
Dependency1 = dependency1;
12+
Dependency2 = dependency2;
13+
}
14+
15+
public IDependency1 Dependency1 { get; set; }
16+
17+
public IDependency2 Dependency2 { get; set; }
18+
19+
public string StringValue { get; set; }
20+
21+
public int IntValue { get; set; }
22+
}
23+
}

source/NSubstituteAutoMocker/NSubstituteAutoMocker.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public NSubstituteAutoMocker(Type[] parameterTypes, Func<ParameterInfo, object,
3636
foreach (ParameterInfo info in parameters)
3737
{
3838
Type type = info.ParameterType;
39-
var constructorArg = Substitute.For(new Type[] { type }, null);
39+
var constructorArg = CreateInstance(type);
4040
if (parameterOverrideFunc != null)
4141
{
4242
constructorArg = parameterOverrideFunc(info, constructorArg);
@@ -48,6 +48,21 @@ public NSubstituteAutoMocker(Type[] parameterTypes, Func<ParameterInfo, object,
4848
ClassUnderTest = Activator.CreateInstance(typeof(T), args) as T;
4949
}
5050

51+
private object CreateInstance(Type type)
52+
{
53+
if (type.IsPrimitive)
54+
{
55+
return Activator.CreateInstance(type);
56+
}
57+
58+
if (type == typeof(string))
59+
{
60+
return null;
61+
}
62+
63+
return Substitute.For(new Type[] { type }, null);
64+
}
65+
5166
private ConstructorInfo MatchConstructorWithParameters(Type type, Type[] parameterTypes)
5267
{
5368
ConstructorInfo result;

0 commit comments

Comments
 (0)