Skip to content

Commit 18dc741

Browse files
Asafimakzu
authored andcommitted
Updated ctor params to support nullable reference or a nullable item in the array. Added nullable preprocessor directive in unit tests
1 parent a481e8d commit 18dc741

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

src/Moq.Tests/MockFixture.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,13 +998,22 @@ public void ExpectGetAndExpectSetMatchArguments()
998998
target.VerifyAll();
999999
}
10001000

1001+
#nullable enable
10011002
[Fact]
10021003
public void ArgumentNullMatchProperCtor()
10031004
{
10041005
var target = new Mock<Foo>(null);
10051006
Assert.Null(target.Object.Bar);
10061007
}
10071008

1009+
[Fact]
1010+
public void ParamsArrayContainsNullMatchProperCtor()
1011+
{
1012+
var target = new Mock<Foo>(new Bar?[] { null });
1013+
Assert.Null(target.Object.Bar);
1014+
}
1015+
#nullable disable
1016+
10081017
[Fact]
10091018
public void DistinguishesSameMethodsWithDifferentGenericArguments()
10101019
{

src/Moq/Mock`1.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public Mock()
139139
/// var mock = new Mock&lt;MyProvider&gt;(someArgument, 25);
140140
/// </code>
141141
/// </example>
142-
public Mock(params object[] args)
142+
public Mock(params object?[]? args)
143143
: this(MockBehavior.Default, args)
144144
{
145145
}
@@ -154,7 +154,7 @@ public Mock(params object[] args)
154154
/// </code>
155155
/// </example>
156156
public Mock(MockBehavior behavior)
157-
: this(behavior, new object[0])
157+
: this(behavior, Array.Empty<object?>())
158158
{
159159
}
160160

@@ -168,14 +168,11 @@ public Mock(MockBehavior behavior)
168168
/// The mock will try to find the best match constructor given the constructor arguments,
169169
/// and invoke that to initialize the instance. This applies only to classes, not interfaces.
170170
/// </remarks>
171-
public Mock(MockBehavior behavior, params object[] args)
171+
public Mock(MockBehavior behavior, params object?[]? args)
172172
{
173173
Guard.IsMockable(typeof(T));
174174

175-
if (args == null)
176-
{
177-
args = new object[] { null };
178-
}
175+
args ??= new object?[] { null };
179176

180177
this.additionalInterfaces = new List<Type>();
181178
this.behavior = behavior;

0 commit comments

Comments
 (0)