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

Expression execution during override detection must not stop setup from being added #1032

Merged
merged 3 commits into from
Jun 24, 2020
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is loosely based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).


## Unreleased

#### Fixed

* Regression: `NullReferenceException` on subsequent setup if expression contains null reference (@IanYates83, #1031)


## 4.14.3 (2020-06-18)

#### Fixed
Expand Down
12 changes: 11 additions & 1 deletion src/Moq/Evaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,17 @@ public override Expression Visit(Expression expression)
base.Visit(expression);
if (!this.cannotBeEvaluated)
{
if (this.fnCanBeEvaluated(expression))
bool canBeEvaluated;
try
{
canBeEvaluated = this.fnCanBeEvaluated(expression);
}
catch
{
canBeEvaluated = false;
}

if (canBeEvaluated)
{
this.candidates.Add(expression);
}
Expand Down
50 changes: 50 additions & 0 deletions tests/Moq.Tests/Regressions/IssueReportsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3355,6 +3355,56 @@ public interface ITata
}
#endregion

#region 1031

public class Issue1031
{
[Fact]
public void MultiSetupNRE_Abbreviated()
{
StubDataStore obj = null;
var exampleMock = new Mock<IDataStore>(MockBehavior.Strict);
exampleMock.Setup(m => m.IsStored(It.Is<string>(s => obj.Value == s))); // Binds correctly
exampleMock.Setup(m => m.IsStored(It.Is<string>(s => obj.Value != s))); // Null Reference Exception
}

[Fact]
public void MultiSetupNRE()
{
// Arrange
var exampleMock = new Mock<IDataStore>(MockBehavior.Strict);

StubDataStore obj = null;
exampleMock.Setup(m => m.IsStored(It.Is<string>(s => obj.Value == s))) // Binds correctly
.Returns(true).Verifiable();
exampleMock.Setup(m => m.IsStored(It.Is<string>(s => obj.Value != s))) // Null Reference Exception
.Returns(false).Verifiable();

// Act
obj = new StubDataStore { Value = "a" };
var resHit = exampleMock.Object.IsStored("a");
var resMiss = exampleMock.Object.IsStored("b");

// Assert
exampleMock.Verify();
Assert.True(resHit);
Assert.False(resMiss);
}

public class StubDataStore
{
public string Value { get; set; }
}

public interface IDataStore
{
bool IsStored(string arg);
}
}


#endregion

// Old @ Google Code

#region #47
Expand Down