我有以下单元测试,我使用MOQ设置从类返回的对象。但是,当我尝试引用mock.Object时,它引用的是接口类型而不是我尝试返回的类型
var throughFareIdentifer = new ThoroughfareNumberIdentifier();
var throughfareMock = new Mock<ILLUSiteInformation>();
throughfareMock.Setup(x => x.GetThroughfareNumber("15")).Returns(throughFareIdentifer);
var siteInformation = _lluSiteInformation.GetSiteDetails("", "", "", "", "", "", "", "", throughfareMock.Object);
throughfareMock.Object应该是ThroughfareNumberIdentifier而不是IlluSiteInformation。
对此有任何帮助将不胜感激
感谢
克里斯
答案 0 :(得分:1)
它正在做你告诉它要做的事情。通过创建new Mock<ILLUSiteInformation>();
,你会说“给我一个类型为ILLUSiteInformation
的模拟”。
使用设置时:
throughfareMock.Setup(x => x.GetThroughfareNumber("15")).Returns(throughFareIdentifer);
您说“当GetThroughfareNumber
被调用,并将数字15作为字符串传递时,返回throughFareIdentifier
”。
调用throughfareMock.Object.GetThroughfareNumber()
而不是像使用Mock对象那样
_lluSiteInformation.GetSiteDetails("", "", "", "", "", "", "", "", throughfareMock.Object.GetThroughfareNumber("15");
确保您只使用数字15作为字符串(因为这是您设置的)。如果要使用任何字符串,请调用
throughfareMock.Setup(x => x.GetThroughfareNumber(It.IsAny<string>)).Returns(throughFareIdentifer);
如果你想使用int
throughfareMock.Setup(x => x.GetThroughfareNumber(It.IsAny<int>)).Returns(throughFareIdentifer);