开发者

Dependency Injection and Moqing

The way I understand it, DI allows you to use an IoC container to do something like

If a cons开发者_StackOverflowtructor needs an IFoo, use a concrete class Foo : IFoo.

But how is a Mock object using Moq different? Doesn't it also use DI to create a fake Foo?

Thanks.


"Dependency Injection" refers to the general practice of supplying some external resource to an object that requires it. The external resource, or dependency, could be supplied via the object's constructor, a property or method, or even as a method parameter. And you're right, a common practice is to use an IOC to manage the possible dependencies and supply them to their "clients".

Moq, like other mocking frameworks (or isolation frameworks) is a tool that can be used to generate fake (or stub or mock) objects that can be used as dependencies for the class you're testing. Most mocking frameworks (including, Moq, I think, but I don't use it myself) do not dictate how the fakes are passed to the class under test (TypeMock Isolator is an exception here, in that it has magic that can inject the dependencies into the class under test).

There's no reason why you couldn't use an IOC to register your Moq-created fakes and supply them to your class under test, but that really has nothing to do with Moq (or NMock or Rhino Mocks).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜