我刚刚进行了moq单元测试,如下所示。
由于设置无效,测试失败。
有谁能告诉我为什么设置错误?
[TestInitialize]
public void SetupMockRepository()
{
_mockPropertySearchRepository = new Mock<IPropertySearchRepository>(MockBehavior.Strict);
var linkIdHash = new PropertyIdLinkIdHash() { CountryCode = "AU", LinkIdHash = "4567890", PropertyId = "5234526" };
//Get details
_mockPropertySearchRepository.Setup(mq => mq.GetPropertyDetails(It.Is<PropertyIdLinkIdHash>(p => p == linkIdHash))).Returns(new Roots());
this.MockPropertySearchRepository = _mockPropertySearchRepository.Object;
}
/// <summary>
/// Testing object pattern
/// </summary>
[TestMethod]
public void Can_Get_Property_Details_AU()
{
var linkIdHash = new PropertyIdLinkIdHash() { CountryCode = "AU", LinkIdHash = "4567890", PropertyId = "5234526" };
var result = MockContext.GetPropertyDetails(linkIdHash);
Assert.IsNotNull(result);
}
}
由于
答案 0 :(得分:0)
[TestInitialize]
public void SetupMockRepository()
{
var linkIdHash = new PropertyIdLinkIdHash() { CountryCode = "AU", LinkIdHash = "4567890", PropertyId = "5234526" };
var root = GetPropertyDetails();
_mockPropertySearchRepository = new Mock<IPropertySearchRepository>(MockBehavior.Strict);
_mockPropertySearchRepository.Setup(c => c.GetPropertyDetails(It.IsAny<PropertyIdLinkIdHash>()))
.Callback<PropertyIdLinkIdHash>((obj) => linkIdHash = obj)
.Returns(new Roots()
{
}
);
this.MockPropertySearchRepository = _mockPropertySearchRepository.Object;
}