Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated ctor params to support nullable args #1518

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Moq.Tests/MockFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -998,13 +998,22 @@ public void ExpectGetAndExpectSetMatchArguments()
target.VerifyAll();
}

#nullable enable
[Fact]
public void ArgumentNullMatchProperCtor()
{
var target = new Mock<Foo>(null);
Assert.Null(target.Object.Bar);
}

[Fact]
public void ParamsArrayContainsNullMatchProperCtor()
{
var target = new Mock<Foo>(new Bar?[] { null });
Assert.Null(target.Object.Bar);
}
#nullable disable

[Fact]
public void DistinguishesSameMethodsWithDifferentGenericArguments()
{
Expand Down
11 changes: 4 additions & 7 deletions src/Moq/Mock`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public Mock()
/// var mock = new Mock&lt;MyProvider&gt;(someArgument, 25);
/// </code>
/// </example>
public Mock(params object[] args)
public Mock(params object?[]? args)
: this(MockBehavior.Default, args)
{
}
Expand All @@ -154,7 +154,7 @@ public Mock(params object[] args)
/// </code>
/// </example>
public Mock(MockBehavior behavior)
: this(behavior, new object[0])
: this(behavior, Array.Empty<object?>())
{
}

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

if (args == null)
{
args = new object[] { null };
}
args ??= new object?[] { null };

this.additionalInterfaces = new List<Type>();
this.behavior = behavior;
Expand Down