如何使用最小起订量模拟 Autofac 聚合服务?
How to Mock an Autofac Aggregate Service with Moq?
我在 .NET 4.6.2 上并通过 Nuget 使用以下版本的程序集:
服务
Autofac - 4.8.1
Autofac.Extras.AggregateService - 4.1.0
Autofac.Wcf - 4.1.0
Castle.Core - 4.3.1
测试
Autofac.Extras.Moq - 4.3.0
起订量 - 4.10.1
我为主机容器使用的设置与 Docs 中的 "Getting Started" 示例完全一样,您最终会得到一个从 DynamicProxy
生成的代理,这很有效好吧,通过消除构造函数重载。
当谈到对使用这种类型的注入的服务进行单元测试时,我似乎对如何正确模拟它感到困惑。
我花了几个小时尝试各种方法,并且 none 已经成功了。这基本上是我所拥有的:
public interface IMyAggregateService
{
IFirstService FirstService { get; }
ISecondService SecondService { get; }
IThirdService ThirdService { get; }
IFourthService FourthService { get; }
}
public class SomeController
{
private readonly IMyAggregateService _aggregateService;
public SomeController(IMyAggregateService aggregateService)
{
_aggregateService = aggregateService;
}
}
using (var mock = AutoMock.GetLoose())
{
//var depends = mock.Mock<IMyAggregateService>().SetupAllProperties(); //Not working
//depends.SetupProperty(p => p.IFirstService, ?? );
//depends.SetupProperty(p => p.ISecondService, ?? );
//depends.SetupProperty(p => p.IThirdService, ?? );
//depends.SetupProperty(p => p.IFourthService, ?? );
var sut = mock.Create<SomeController>();
Action action = () => sut.SomeAction();
action.Should().xxxx
}
所以我遇到的第一个问题是 IMyAggregateService
上没有设置器,所以 SetupProperty
方法不起作用。当我使用 SetupAllProperties
时,在 运行 时一切都为 null,所以这不会起作用。我什至拉下了 Autofac.Extras.AggregateService
代码并检查了测试项目,但我从中确实找不到任何可能有用的东西,除了 AggregateServiceGenerator
可能在某些时候有用。
所以我的问题是,
"How do I properly mock an aggregate service, and provide behaviors for it during unit testing?"
为了获得额外的荣誉,我还想知道如何提供任何作为属性的依赖项的特定实现,如下所示:
using (var mock = AutoMock.GetLoose())
{
mock.Provide<IFirstService>(this.MyImplProperty);
"How do I properly mock an aggregate service, and provide behaviors
for it during unit testing?"
尝试这样做(它不使用 automoq 只是一个最小起订量:))
var firstServiceImpl= new Mock<IFirstService>();
var secondServiceImp2= new Mock<ISecondService>();
var myAggregateServie= new Mock<IMyAggregateService>();
myAggregateServie.SetupGet(x => x.FirstService ).Returns(firstServiceImpl);
myAggregateServie.SetupGet(x => x.SecondService ).Returns(secondServiceImp2);
.
.
.
然后你可以模拟你的服务行为,并验证调用,一些伪代码看起来像这样。
//Mock as above
//Mock service behavior
firstServiceImpl.Setup(m=>m.Method1()).Returns(1)
//test method of you controller, lets assume that the method1 of controller
//is only calling firstService method named Method1()
var result = SampleController.Method1();
Assert.IsTrue( result == 1)
firstService.Verify(m=>m.Method1(), Times.Once()).
我在 .NET 4.6.2 上并通过 Nuget 使用以下版本的程序集:
服务
Autofac - 4.8.1
Autofac.Extras.AggregateService - 4.1.0
Autofac.Wcf - 4.1.0
Castle.Core - 4.3.1
测试
Autofac.Extras.Moq - 4.3.0
起订量 - 4.10.1
我为主机容器使用的设置与 Docs 中的 "Getting Started" 示例完全一样,您最终会得到一个从 DynamicProxy
生成的代理,这很有效好吧,通过消除构造函数重载。
当谈到对使用这种类型的注入的服务进行单元测试时,我似乎对如何正确模拟它感到困惑。
我花了几个小时尝试各种方法,并且 none 已经成功了。这基本上是我所拥有的:
public interface IMyAggregateService
{
IFirstService FirstService { get; }
ISecondService SecondService { get; }
IThirdService ThirdService { get; }
IFourthService FourthService { get; }
}
public class SomeController
{
private readonly IMyAggregateService _aggregateService;
public SomeController(IMyAggregateService aggregateService)
{
_aggregateService = aggregateService;
}
}
using (var mock = AutoMock.GetLoose())
{
//var depends = mock.Mock<IMyAggregateService>().SetupAllProperties(); //Not working
//depends.SetupProperty(p => p.IFirstService, ?? );
//depends.SetupProperty(p => p.ISecondService, ?? );
//depends.SetupProperty(p => p.IThirdService, ?? );
//depends.SetupProperty(p => p.IFourthService, ?? );
var sut = mock.Create<SomeController>();
Action action = () => sut.SomeAction();
action.Should().xxxx
}
所以我遇到的第一个问题是 IMyAggregateService
上没有设置器,所以 SetupProperty
方法不起作用。当我使用 SetupAllProperties
时,在 运行 时一切都为 null,所以这不会起作用。我什至拉下了 Autofac.Extras.AggregateService
代码并检查了测试项目,但我从中确实找不到任何可能有用的东西,除了 AggregateServiceGenerator
可能在某些时候有用。
所以我的问题是,
"How do I properly mock an aggregate service, and provide behaviors for it during unit testing?"
为了获得额外的荣誉,我还想知道如何提供任何作为属性的依赖项的特定实现,如下所示:
using (var mock = AutoMock.GetLoose())
{
mock.Provide<IFirstService>(this.MyImplProperty);
"How do I properly mock an aggregate service, and provide behaviors for it during unit testing?"
尝试这样做(它不使用 automoq 只是一个最小起订量:))
var firstServiceImpl= new Mock<IFirstService>();
var secondServiceImp2= new Mock<ISecondService>();
var myAggregateServie= new Mock<IMyAggregateService>();
myAggregateServie.SetupGet(x => x.FirstService ).Returns(firstServiceImpl);
myAggregateServie.SetupGet(x => x.SecondService ).Returns(secondServiceImp2);
.
.
.
然后你可以模拟你的服务行为,并验证调用,一些伪代码看起来像这样。
//Mock as above
//Mock service behavior
firstServiceImpl.Setup(m=>m.Method1()).Returns(1)
//test method of you controller, lets assume that the method1 of controller
//is only calling firstService method named Method1()
var result = SampleController.Method1();
Assert.IsTrue( result == 1)
firstService.Verify(m=>m.Method1(), Times.Once()).