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

Mocking class with no parameterless ctor #700

Closed
Mardoxx opened this issue Oct 3, 2018 · 3 comments
Closed

Mocking class with no parameterless ctor #700

Mardoxx opened this issue Oct 3, 2018 · 3 comments

Comments

@Mardoxx
Copy link

Mardoxx commented Oct 3, 2018

Has this been considered?

public class MockMe
{
    MockMe(ISomeDependency someDependency)
    {
    }

    MockMe(ISomeDependency someDependency, ISomeOtherThing someOtherThing)
    {
    }
}

Would be nice to be able to do new Mock<MockMe>(); without having to specify params.
Maybe have it choose simplest ctor (least num params) and then attempt to throw in mocks of these parameters (or default values).

Here new Mock<MockMe>(); would be equiv of new Mock<MockMe>(Mock.Of<ISomeDependency>());

@stakx
Copy link
Contributor

stakx commented Oct 3, 2018

Moq is not a DI container and as far as I am aware, it doesn't strive to be one. If we added this functionality to Moq, it would either be very incomplete (when compared to a proper DI container), or we'd have to bring in a lot of functionality that has nothing to do with mocking. Both outcomes are equally undesirable.

It would be much better, IMO, to build this functionality / integration outside of the core library. Say, create a helper function that analyzes the desired type's parameters, fetches appropriate values from somewhere else, then invokes the Mock<T> ctor with these values.

If you don't want to build this yourself, I'm sure there are existing projects that do this kind of thing for you -- AutoFixture and Moq.AutoMock come to mind.

@stakx stakx closed this as completed Oct 3, 2018
@Mardoxx
Copy link
Author

Mardoxx commented Oct 4, 2018

That's exactly what I was looking for, thanks so much 😄

@stakx
Copy link
Contributor

stakx commented Oct 4, 2018

You're welcome!

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

No branches or pull requests

2 participants