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

Added support for lambda expressions while creating a mock #888

Merged
merged 9 commits into from
Aug 29, 2019
Merged

Added support for lambda expressions while creating a mock #888

merged 9 commits into from
Aug 29, 2019

Conversation

frblondin
Copy link
Contributor

Implements #884.

The following methods have been added:

Mock<T>.ctor(Expression<Func<T>> constructorExpression, MockBehavior behavior = MockBehavior.Strict)
MockRepository.Create<T>(Expression<Func<T>> constructorExpression, MockBehavior behavior = MockBehavior.Strict)

Ideally, the parameter order will be based on that of existing constructors / methods.

As per your remark in the issue, I favored the use of an optional parameter for the behavior. Using an optional parameter doesn't allow us to keep the same order because the behavior is necessary the last parameter (the one being optional...).

The implementation works as follows:

  • The lambda expression is being analyzed using a new visitor
  • I wanted these new methods to successfully get the argument values whatever they use (captured local variables, method calls, constructors, new array inits...). Once the NewExpression is visited, an internal lambda will be created & invoked to produce the arguments as an instance of object[]

I was about to suggest to mark the existing mock creation method/constructors (taking params object[]) as deprecated... but in any case we still need them when creating a mock of an abstract class!

I added unit tests & updated the changelog.

Copy link
Contributor

@stakx stakx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall! I'd like to give this another, closer look, unfortunately I'm running out of time today. In the meantime, could you please take care of the following few details?

Thank you!

CHANGELOG.md Outdated Show resolved Hide resolved
src/Moq/Linq/ConstructorCallVisitor.cs Outdated Show resolved Hide resolved
src/Moq/Mock.Generic.cs Outdated Show resolved Hide resolved
@@ -5,7 +5,7 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;

using System.Linq.Expressions;
using Moq.Properties;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please insert a blank line above this line, so that imports are grouped by the main namespace (System.*, Moq.*).

src/Moq/Obsolete/MockFactory.cs Outdated Show resolved Hide resolved
src/Moq/ExpressionExtensions.cs Outdated Show resolved Hide resolved
src/Moq/Linq/ConstructorCallVisitor.cs Outdated Show resolved Hide resolved
src/Moq/ExpressionExtensions.cs Outdated Show resolved Hide resolved
src/Moq/ExpressionExtensions.cs Outdated Show resolved Hide resolved
src/Moq/Properties/Resources.resx Outdated Show resolved Hide resolved
@frblondin
Copy link
Contributor Author

Looks all good to me. Let me know!

Copy link
Contributor

@stakx stakx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the wait, @frblondin.

I've got a few more things, I've been paying some more attention to fitting this new feature into the existing codebase in a consistent manner... so there's some nitpicking involved. I hope you don't mind.

src/Moq/Expressions/Visitors/ConstructorCallVisitor.cs Outdated Show resolved Hide resolved
src/Moq/Expressions/Visitors/ConstructorCallVisitor.cs Outdated Show resolved Hide resolved
src/Moq/Mock.Generic.cs Outdated Show resolved Hide resolved
src/Moq/Obsolete/MockFactory.cs Outdated Show resolved Hide resolved
tests/Moq.Tests/MockFixture.cs Show resolved Hide resolved
src/Moq/Expressions/Visitors/ConstructorCallVisitor.cs Outdated Show resolved Hide resolved
src/Moq/Expressions/Visitors/ConstructorCallVisitor.cs Outdated Show resolved Hide resolved
src/Moq/Mock.Generic.cs Outdated Show resolved Hide resolved
src/Moq/Obsolete/MockFactory.cs Outdated Show resolved Hide resolved
@frblondin
Copy link
Contributor Author

I integrated most your changes: except the new overloads to avoid use of optional parameters (creates ambiguities) and your question about use of lambda syntax for parameter-less constructors.

Hope this version will be the good one ;-)

Copy link
Contributor

@stakx stakx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nearly there. :-)

(Sorry for the nitpicking, it's easier to get these things done right now while we're focusing on it, rather than later.)

except the new overloads to avoid use of optional parameters (creates ambiguities)

Ah, right you are! Agreed, let's keep the optional parameters. 👍

tests/Moq.Tests/MockRepositoryFixture.cs Outdated Show resolved Hide resolved
tests/Moq.Tests/Regressions/IssueReportsFixture.cs Outdated Show resolved Hide resolved
src/Moq/Properties/Resources.resx Outdated Show resolved Hide resolved
@frblondin
Copy link
Contributor Author

New round of updates, I believe your remaining change requests have been taken into account - you to confirm & resolve!

Thanks for you time.

@stakx stakx added this to the 4.13.0 milestone Aug 29, 2019
Copy link
Contributor

@stakx stakx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

There is another whitespace issue in Resources.resx (a succession of redundant blank lines) but I've caused you as much work there as I dare 😆, so if you don't mind I'll clean this up myself quickly after merging.

🚀

src/Moq/Properties/Resources.resx Show resolved Hide resolved
@stakx stakx merged commit 7bf42fb into devlooped:master Aug 29, 2019
ishimko pushed a commit to ishimko/moq4 that referenced this pull request Sep 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants