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

Mock any instance #214

Closed
ggsilva opened this issue Nov 2, 2015 · 4 comments
Closed

Mock any instance #214

ggsilva opened this issue Nov 2, 2015 · 4 comments

Comments

@ggsilva
Copy link

ggsilva commented Nov 2, 2015

Hi. You could implement the feature of mock any instance of a class? So I do not need dependency injection.

Example:

Mock.SetupAny(anyInstance => anyInstance.DoSomething()).Returns(something);

@kzu
Copy link
Member

kzu commented Nov 6, 2015

Sounds like AutoFixture?

@ggsilva
Copy link
Author

ggsilva commented Nov 6, 2015

Sorry about my English.

It would be something more like the java has:

http://docs.mockito.googlecode.com/hg/org/mockito/Matchers.html#any()

@ploeh
Copy link
Sponsor

ploeh commented Nov 7, 2015

AutoFixture isn't a dynamic mock library (but it does integrate with Moq as an option), so the above doesn't sound like AutoFixture.

It's not clear to me what this issue is suggesting, but Moq does work with concrete classes as well as abstract classes and interfaces. Moq can do anything you, as a programmer, can do, but nothing more.

Could the apparently more powerful capabilities in Java be due to the fact that in Java, all class members are virtual (and thus, overridable) by default, whereas with typical .NET languages, you have to explicitly make members virtual - which means that most aren't?

@stakx
Copy link
Contributor

stakx commented Jun 12, 2017

Bom dia @ggsilva! This issue has been inactive for some time, so I am closing it for the time being. If you are still interested in discussing this, just reply back and we can reopen it.

That being said: Moq is not a Dependency Injection (DI) container, and it does not aim at becoming one (AFAIK ;). The central idea behind DI is that you don't have to ask for an object, you are given one. This is not how Moq works. Moq only gives you with a mock object when you explicitly ask for one (new Mock<T>, Mock.Of<T>, etc.).

I'd say it is unlikely that features will be added to Moq just so it would be better at DI than it is now. If DI is what you need, you're better off using the right tool for that; e.g. Autofac, Ninject, or Unity.

If I have misunderstood your question, feel free to elaborate.

@stakx stakx closed this as completed Jun 12, 2017
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

4 participants