Skip to content

Commit

Permalink
Merge pull request #185 from pkpjpm/bugfix/issue-184
Browse files Browse the repository at this point in the history
Issue 184: allow null to match nullable value types
  • Loading branch information
kzu committed Jun 25, 2015
2 parents 7e7df4c + b819b9c commit 44e4554
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
7 changes: 5 additions & 2 deletions Source/Match.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//Copyright (c) 2007. Clarius Consulting, Manas Technology Solutions, InSTEDD
//Copyright (c) 2007. Clarius Consulting, Manas Technology Solutions, InSTEDD
//http://code.google.com/p/moq/
//All rights reserved.

Expand Down Expand Up @@ -128,7 +128,10 @@ internal override bool Matches(object value)
{
return false;
}
if (value == null && typeof(T).IsValueType)

var matchType = typeof(T);
if (value == null && matchType.IsValueType
&& ( !matchType.IsGenericType || matchType.GetGenericTypeDefinition() != typeof(Nullable<>)))
{
// If this.Condition expects a value type and we've been passed null,
// it can't possibly match.
Expand Down
25 changes: 23 additions & 2 deletions UnitTests/Regressions/IssueReportsFixture.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
Expand Down Expand Up @@ -234,7 +234,28 @@ public void when_a_method_doesnt_have_generic_parameters_exception_doesnt_includ

#endregion // #176

// Old @ Google Code
#region #184

public class Issue184
{
public interface ISimpleInterface
{
void Method(Guid? g);
}

[Fact]
public void strict_mock_accepts_null_as_nullable_guid_value()
{
var mock = new Mock<ISimpleInterface>(MockBehavior.Strict);
mock.Setup(x => x.Method(It.IsAny<Guid?>()));
mock.Object.Method(null);
mock.Verify();
}
}

#endregion // #184

// Old @ Google Code

#region #47

Expand Down

0 comments on commit 44e4554

Please sign in to comment.