Skip to content

Commit

Permalink
Merge pull request #1251 from stakx/bugfix/byref-parameter-handling
Browse files Browse the repository at this point in the history
Account for by-ref params in `MethodExpectation.CreateFrom(Invocation)`
  • Loading branch information
stakx committed Apr 17, 2022
2 parents b50a8a1 + d93ec43 commit fa58a8b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@ The format is loosely based on [Keep a Changelog](http://keepachangelog.com/en/1
#### Fixed

* Regression: Property stubs not working on sub mock (@aaronburro, #1240)
* Failure when invoking a method with by-ref parameter & mockable return type on a mock with `CallBase` and `DefaultValue.Mock` configured (@IanKemp, #1249)


## 4.17.1 (2022-02-26)
Expand Down
4 changes: 3 additions & 1 deletion src/Moq/MethodExpectation.cs
Expand Up @@ -37,7 +37,9 @@ public static MethodExpectation CreateFrom(Invocation invocation)
arguments = new Expression[n];
for (int i = 0; i < n; ++i)
{
arguments[i] = E.Constant(invocation.Arguments[i], parameterTypes[i]);
var parameterType = parameterTypes[i];
if (parameterType.IsByRef) parameterType = parameterType.GetElementType();
arguments[i] = E.Constant(invocation.Arguments[i], parameterType);
}
}

Expand Down
21 changes: 21 additions & 0 deletions tests/Moq.Tests/Regressions/IssueReportsFixture.cs
Expand Up @@ -3782,6 +3782,27 @@ public void Property_on_submock_should_be_stubbed_2()

#endregion

#region 1249

public class Issue1249
{
public class NonSealedType { }

public interface IFoo
{
NonSealedType Method(in int arg);
}

[Fact]
public void No_ArgumentException_due_to_parameter_refness()
{
var mock = new Mock<IFoo>() { CallBase = true, DefaultValue = DefaultValue.Mock };
_ = mock.Object.Method(default);
}
}

#endregion

// Old @ Google Code

#region #47
Expand Down

0 comments on commit fa58a8b

Please sign in to comment.