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

Enable parameterized Mock.Of<> in query comprehension from clause #1085

Merged
merged 4 commits into from
Oct 28, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ The format is loosely based on [Keep a Changelog](http://keepachangelog.com/en/1

* `SetupProperty` fails if property getter and setter are not both defined in mocked type (@stakx, #1017)
* Expression tree argument not matched when it contains a captured variable &ndash; evaluate all captures to their current values when comparing two expression trees (@QTom01, #1054)
* Failure when parameterized `Mock.Of<>` is used in query comprehension `from` clause (@stakx, #982)


## 4.14.7 (2020-10-14)
Expand Down
9 changes: 9 additions & 0 deletions src/Moq/Linq/MockSetupsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ internal class MockSetupsBuilder : ExpressionVisitor
private static readonly string[] unsupportedMethods = new[] { "All", "Any", "Last", "LastOrDefault", "Single", "SingleOrDefault" };

private int stackIndex;
private int quoteDepth;

public MockSetupsBuilder()
{
Expand Down Expand Up @@ -91,6 +92,14 @@ protected override Expression VisitUnary(UnaryExpression node)
return ConvertToSetup(node.Operand, Expression.Constant(false)) ?? base.VisitUnary(node);
}

if (node.NodeType == ExpressionType.Quote)
{
this.quoteDepth++;
var result = this.quoteDepth > 1 ? node : base.VisitUnary(node);
this.quoteDepth--;
return result;
}

return base.VisitUnary(node);
}

Expand Down
23 changes: 22 additions & 1 deletion tests/Moq.Tests/Linq/QueryableMocksFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

using Xunit;

namespace Moq.Tests
namespace Moq.Tests.Linq
{
public class QueryableMocksFixture
{
Expand Down Expand Up @@ -212,6 +212,27 @@ public void Strict_Mocks_Of_with_specification_expression_will_throw_for_non_set
Assert.Throws<MockException>(() => _ = foo.Name);
}

[Fact]
public void Multiple_mocks_with_query_comprehension_syntax__predicates_in_where_clause()
{
var x = (from x1 in Mocks.Of<IFoo>()
from __ in Mocks.Of<IFoo>()
where x1.Name == "1" && __.Name == "2"
select x1)
.First();
Assert.Equal("1", x.Name);
}

[Fact]
public void Multiple_mocks_with_query_comprehension_syntax__predicates_in_from_clause()
{
var x = (from x1 in Mocks.Of<IFoo>(_ => _.Name == "1")
from __ in Mocks.Of<IFoo>(_ => _.Name == "2")
select x1)
.First();
Assert.Equal("1", x.Name);
}

public class Dto
{
public string Value { get; set; }
Expand Down