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

Support expression-based mock creation for constructor calls that takes parameters #884

Closed
frblondin opened this issue Aug 9, 2019 · 4 comments
Assignees
Milestone

Comments

@frblondin
Copy link
Contributor

Existing mock creation overloads for constructors that take parameters has some limitations:

  • Compiler can't check whether the constructor actually exists. Issues/regressions are detected during runtime rather than leveraging code editor capacity
  • Editor features such as Finding references... to a constructor is missing calls to the constructor made by these mocks
  • When refactoring code, we end up missing those references... and introduce errors (back to first bullet point)

I think it would be relatively simple to add new overloads to Mock and MockRepository that take an Expression<Func<T>> as a parameter. Something like:

var mock1 = new Mock<SomeType>(() => new SomeType("foo"));
var mock2 = repository.Create(() => new SomeType("foo"));

If you think it's a good idea I could submit a PR. What do you think?

@stakx
Copy link
Contributor

stakx commented Aug 11, 2019

Hi @frblondin, that idea would be in line with one of Moq's declared goals (promoting correct code through static typing), so I don't have any objection to adding the new overloads that you're suggesting. Feel free to submit a PR.

add new overloads to Mock and MockRepository that take an Expression<Func<T>> as a parameter

  • The overload should go in Mock<T>, not in Mock (as I'm sure you realise).

  • There has to be a possibility to optionally specify a MockBehavior; either via an optional parameter, or an additional overload. Ideally, the parameter order will be based on that of existing constructors / methods.

@stakx stakx added this to the 4.14.0 milestone Aug 11, 2019
@stakx
Copy link
Contributor

stakx commented Aug 11, 2019

(@frblondin, I have assigned this issue to you and a future version milestone, just to help me track this issue more easily. But don't let that put any pressure on you.)

@frblondin
Copy link
Contributor Author

Just submitted a PR: #888!

@stakx stakx modified the milestones: 4.14.0, 4.13.0 Aug 29, 2019
@stakx
Copy link
Contributor

stakx commented Aug 31, 2019

Implemented in #888.

@stakx stakx closed this as completed Aug 31, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants