[英]How to Mock a named constructor dependency
我有一个具有以下签名的MVC控制器:
public AccountController(
IRulesFacade rulesFacade,
[Dependency("personAppClient")] IAppClient personAppClient,
[Dependency("notificationsClient")] IAppClient notificationsClient,
ITransformer<ExpandoObject> transformer)
您将注意到有两个CTOR属性,它们是相同的接口类型,但名称不同。
在我的Unity配置中,我具有以下代码行:
container.RegisterType<IAppClientConfigProvider, AppClientConfigProvider>("personAppClient", new InjectionConstructor(
new ResolvedParameter<IAppClient>("personAppClient"),
personBaseUrl));
container.RegisterType<IAppClientConfigProvider, AppClientConfigProvider>("notificationsClient", new InjectionConstructor(
new ResolvedParameter<IAppClient>("notificationsClient"),
notificationsBaseUrl));
在我的UnitTest中,我具有以下内容以及一些相关的安装程序代码:
MockAppClient = new Mock<IAppClient>();
MockAppClient.Setup(ac => ac.AddAsync<ExpandoObject>(It.IsAny<ExpandoObject>()))
.Returns(() => Task.FromResult(User));
我的问题是如何创建可以为依赖关系提供必要“名称”的Mock?
创建两个独立的依赖项模拟并将其传递给SUT
// Arrange
var personAppClientMock = new Mock<IAppClient>();
personAppClientMock.Setup(_ => _.AddAsync(It.IsAny<ExpandoObject>()))
.ReturnsAsync(User);
var notificationsClientMock = new Mock<IAppClient>();
notificationsClientMock.Setup(_ => _.AddAsync(It.IsAny<ExpandoObject>()))
.ReturnsAsync(SomeOtherObject);
//...other related setup code
var sut = new AccountController(
rulesFacadeMock.Object,
personAppClientMock.Object,
notificationsClientMock.Object,
transformerMock.Object);
// Act
//...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.