繁体   English   中英

无法在AutoFac中注册实例?

[英]Failed to register Instance in AutoFac?

我想在AutoFac中使用我项目的现有模拟实例。 我不想重写我的PROD代码。 所以我发现了一些无效的AutoFac。 我想我错过了什么。

我试过下面的代码。

public AboutTideEditorMockTest () {
    aboutTideService = new AboutTideEditorService (iAboutTideEditorRepository.Object, exceptionLogServiceMock.Object);
    aboutTideServiceWithNullParam = new AboutTideEditorService (null, exceptionLogServiceMock.Object);
}

//This is my test case
[FactWithAutomaticDisplayName]
public void Test1 () {
    var cb = new ContainerBuilder ();
    var studyLoaderMock = new Mock<IAboutTideEditorService> ().Object;
    var studyLoaderMock1 = iAboutTideEditorRepository.Object;
    var studyLoaderMock2 = exceptionLogServiceMock.Object;
    cb.RegisterInstance (studyLoaderMock).As<IAboutTideEditorService> ();
    cb.RegisterInstance (studyLoaderMock1).As<IAboutTideEditorRepository> ();
    cb.RegisterInstance (studyLoaderMock2).As<IExceptionLogService> ();
    var container = cb.Build ();
    using (var scope = container.BeginLifetimeScope ()) {
        var component = scope.Resolve<AboutTideEditorService> ();
        responseData = component.AddAboutTideContent (applicationUser, aboutTide);
        Assert.Equal (ProcessStatusEnum.Invalid, responseData.Status);
    }
}

我想使用我传递给“RegisterInstance”的现有模拟实例。 当我尝试调试我的测试用例时,我得到“responseData”null。 我无法进入AddAboutTideContent。

您没有设置模拟返回值,您需要解析IAboutTideEditorService而不是AboutTideEditorService

您还需要以不同方式生成模拟。 不需要更改生产代码!

像这样做:

[FactWithAutomaticDisplayName]
public void Test1() {
  var cb = new ContainerBuilder();

  var studyLoaderMock = new Mock<IAboutTideEditorService>();
  var studyLoaderMock1 = new Mock<IAboutTideEditorRepository>(); // you don't need that when resolving only IAboutTideEditorService
  var studyLoaderMock2 = new Mock<IExceptionLogService>(); // you don't need that when resolving only IAboutTideEditorService

  cb.RegisterInstance(studyLoaderMock.Object).As<IAboutTideEditorService>();
  cb.RegisterInstance(studyLoaderMock1.Object).As<IAboutTideEditorRepository>(); // you don't need that when resolving only IAboutTideEditorService
  cb.RegisterInstance(studyLoaderMock2.Object).As<IExceptionLogService>(); // you don't need that when resolving only IAboutTideEditorService

  var container = cb.Build();

  studyLoaderMock
    .Setup(x => x.AddAboutTideContent(It.IsAny<YourTypeHereForParameterA>,
                                      It.IsAny<YourTypeHereForParameterB>)
             .Returns(new MyResponseDataType()); // add the right types here necessary, I can't tell which types they are because I am not seeing the functions code

  using (var scope = container.BeginLifetimeScope()) {
    var component = scope.Resolve<IAboutTideEditorService>(); // changed to IAboutTideEditorService
    responseData = component.AddAboutTideContent(applicationUser, aboutTide);
    Assert.Equal(ProcessStatusEnum.Invalid, responseData.Status);
  }
}

您的函数调用返回null因为这是Moq = MockBehavior.Loose的模拟的默认行为。 如果希望mock的函数返回非显式或显式参数的特定值,则必须调用Setup(delegate)Returns(objectInstance)Returns(Func<ObjectType>)

通常,您的测试设置没有多大意义。 您基本上只使用Autofac-Container注册模拟,这使得容器本身与您的测试无关。 通常只有在直接测试实现而不是模拟时才需要使用IoC进行测试。 这些测试称为Integration-Tests

这会更有意义:

[FactWithAutomaticDisplayName]
public void Test1() {
  var cb = new ContainerBuilder();

  var studyLoaderMock1 = new Mock<IAboutTideEditorRepository>();that when resolving only IAboutTideEditorService
  var studyLoaderMock2 = new Mock<IExceptionLogService>();
  var studyLoader = new AboutTideEditorService(studyLoaderMock1.Object, studyLoaderMock2.Object);

  cb.RegisterInstance(studyLoader).As<IAboutTideEditorService>();

  var container = cb.Build();

  // now setup the functions of studyLoaderMock1 and studyLoaderMock2 
  // required for your function `AddAboutTideContent` from `IAboutTideEditorService` to work.

  using (var scope = container.BeginLifetimeScope()) {
    var component = scope.Resolve<IAboutTideEditorService>(); // changed to IAboutTideEditorService
    responseData = component.AddAboutTideContent(applicationUser, aboutTide);
    Assert.Equal(ProcessStatusEnum.Invalid, responseData.Status);
  }
}

请记住,我在这里假设AboutTideEditorService所需的参数AboutTideEditorService 有关如何使用Moq设置模拟的更多信息,请查看此处

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM