Using autofac with moq

三世轮回 提交于 2019-12-03 12:17:14

I found the solution, Yes it is possible!

  var AppContainer = ApplicationContainer.GetApplicationContainer();
  var studyLoaderMock = new Mock<IStudyLoader>().Object;
  cb.RegisterInstance(studyLoaderMock).As<IStudyLoader>();
  cb.Update(AppContainer);

The method you are looking for is called RegisterInstance:

var mock = ...;
var cb = new ContainerBuilder();
cb.RegisterInstance(mock.Object);
var container = cb.Build();

That said, an existing container (in your case the AppContainer) that already have a registration for the interface you want to mock will not be able to resolve the instance. It will continue to resolve the production instance.

A "workaround", which I would strongly advice you to follow, is to not involve the AppContainer in your test, but rather build a container tailored for your test so you have better control of what goes in there and not. The effect is that you get to register whatever mock instance you need along with the SUT.

Old Question, but AutoFac Now since version 4.0.1 support Moq4 and provide extra package you can install Autofac.Extras.Moq

    Install-Package Autofac.Extras.Moq

Autofac.Extras.Moq auto install both AutoFac and Moq

You needn't to create a Container and register StudyLoader. It's implicitly defined by Autofac.Extras.Moq.

Autofac.Extras.Moq is valuable for nested dependency. You don't care to register all the dependency graph. Only Mock your SystemUnderTest SUT object

You can implement you test case as given below:

         [TestFixture]
        class StudyLoaderTest
        {
            [Test]
            public void Test()
            {
                using (var mock = AutoMock.GetLoose())
                {
                    // The AutoMock class will inject a mock IStudyLoader
                    // into the StudyLoader constructor
                    //no need to create/configure a container
                    var studyLoader = mock.Create<StudyLoader>();               
                    Assert.AreEqual("hi AutoFac Moq", studyLoader.Name);
                }
            }
        }

         class StudyLoader : IStudyLoader
        {
            public string Name { get; set; } = "hi AutoFac Moq";
        }
         interface IStudyLoader
        {
            string Name { get; set; }
        }

I don't have particular experience with autofac, but most other containers which I worked with allow to register an instance of object, that is already created. So you can create your mock, and then register mock.Object as implementation for IStudyLoader. Consult autofac documentation on how to do this.

Since this question has been asked, there is now an AutoFac.Extras.Moq package, supported by AutoFac:

http://autofaccn.readthedocs.io/en/latest/integration/moq.html

This allows you to create on the fly dependencies within your Unit Tests.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!