gpt4 book ai didi

c# - 我如何模拟我的通用 Unity 解析包装器?接到电话是模棱两可的

转载 作者:太空宇宙 更新时间:2023-11-03 21:38:25 27 4
gpt4 key购买 nike

public T Resolve<T>() {
return _container.Resolve<T>();
}

Mock<ServiceLocator> serviceLocator = new Mock<ServiceLocator>();
Mock<SecurityHelper> securityHelper= new Mock<SecurityHelper>();
serviceLocator.Setup(x => x.Resolve<SecurityHelper>()).Returns(securityHelper);

产生这个错误,我该如何解决这个问题?

Error 36 The call is ambiguous between the following methods or properties: 'Moq.Language.IReturns<MVCWebsite.Helpers.ServiceLocator,MVCWebsite.Helpers.SecurityHelper>.Returns(System.Func<MVCWebsite.Helpers.SecurityHelper>)' and 'Moq.Language.IReturns<MVCWebsite.Helpers.ServiceLocator,MVCWebsite.Helpers.SecurityHelper>.Returns(MVCWebsite.Helpers.SecurityHelper)' C:\dev\Foundation\Foundation.Test\Fixtures\Account\AccountCtrlFixture.cs 61 13 Foundation.Test

编辑:(在 Sergey 的帮助下,以下是最终的工作解决方案)

[Test]
public void TestResolveMockPoc()
{
//Mock service locator object
Mock<ServiceLocator> serviceLocator = new Mock<ServiceLocator>();

//Mock appSettings object
Mock<AppSettings> appSettings = new Mock<AppSettings>();
appSettings.Object.AppName = "IfYouSeeThisPocIsWorking";

//Mock Resolve method to return my mocked object, for this particular type.
serviceLocator.Setup(x => x.Resolve<AppSettings>()).Returns(appSettings.Object);

//Override ServiceLocator inside the ServiceLocator helper (this is what is called by actual project)
ServiceLocator.Locator = serviceLocator.Object;

//Assert if this Moq Poc worked :-) ;)
string updatedAppName = ServiceLocator.AppSettings().AppName;
Assert.AreEqual(ServiceLocator.AppSettings().AppName, "IfYouSeeThisPocIsWorking");


}

最佳答案

我认为您需要做的是返回与您的 Mock SecurityHelper 关联的对象:

serviceLocator.Setup(x => x.Resolve<SecurityHelper>())
.Returns<SecurityHelper>(securityHelper.Object);

关于c# - 我如何模拟我的通用 Unity 解析包装器?接到电话是模棱两可的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20621372/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com