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);
