我遇到了与此处或此处提出的问题类似的问题,但解决方案似乎不适用于我的情况。
考虑以下类:
public interface IProductsWebService
{
ICObjectLang[] GetClassLevel(getClassLevelLang criteria);
}
// the rest of this class was automaticaly generated as web service reference
public partial class getClassLevelLang : IEquatable<getClassLevelLang>
{
public override bool Equals(object obj)
{
var other = obj as getClassLevelLang;
return Equals(other);
}
public bool Equals(getClassLevelLang other)
{
if (this == other)
{
return true;
}
if (other == null)
{
return false;
}
return CID == other.CID &&
accessCode == other.accessCode &&
classCode == other.classCode &&
depth == other.depth &&
language == other.language &&
partyCode == other.partyCode &&
refId == other.refId;
}
public override int GetHashCode()
{
var hash = 531;
hash += CID != null ? CID.GetHashCode() : 1;
hash += accessCode != null ? accessCode.GetHashCode() : 1;
hash += classCode != null ? classCode.GetHashCode() : 1;
hash += depth != null ? depth.GetHashCode() : 1;
hash += language != null ? language.GetHashCode() : 1;
hash += partyCode != null ? partyCode.GetHashCode() : 1;
hash += refId != null ? refId.GetHashCode() : 1;
return hash;
}
public override string ToString()
{
var str = new StringBuilder();
str.AppendFormat("CID: {0}, ", CID);
str.AppendFormat("accessCode: {0}, ", accessCode);
str.AppendFormat("classCode: {0}, ", classCode);
str.AppendFormat("depth: {0}, ", depth);
str.AppendFormat("language: {0}, ", language);
str.AppendFormat("partyCode: {0}, ", partyCode);
str.AppendFormat("refId: {0}", refId);
return str.ToString();
}
}
以下代码用于测试(calls
列表仅用于调试):
private Mock<IProductsWebService> productsService;
private IProductsImportProcess target;
private List<getClassLevelLang> calls = new List<getClassLevelLang>();
[TestMethod]
public void RecursiveDownloadTest()
{
//other mocks (configuration, log, categoryService and configurationService) set here
target = new ProductsImportProcess(
configuration.Object, log.Object, categoryService.Object,
productsService.Object, configurationService.Object);
productsService = new Mock<IProductsWebService>(MockBehavior.Strict);
var root1 = new Category
{
CategoryID = "root1",
SubCategories = new List<Category>
{
new Category
{
CategoryID = "cat1.1",
SubCategories = new List<Category>
{
new Category
{CategoryID = "cat1.1.1"}
}
},
new Category
{
CategoryID = "cat1.2"
}
}
};
var root2 = new Category { CategoryID = "root2" };
setupProductsServiceCall("tree1", root1, true);
setupProductsServiceCall("tree1", root1.SubCategories.First().SubCategories.First());
setupProductsServiceCall("tree2", root2, true);
target.Execute();
productsService.VerifyAll();
}
private void setupProductsServiceCall(string treeCode, Category category, bool isRoot = false)
{
var config = configuration.Object;
var criteria = new getClassLevelLang
{
CID = isRoot ? null : category.CategoryID,
language = "all",
partyCode = "something",
depth = "1",
classCode = treeCode,
accessCode = "xyz"
};
var productsNode = TestUtils.CreateAbbProductsNode(category);
productsService.Setup(mock => mock.GetClassLevel(It.Is<getClassLevelLang>(c => c.Equals(criteria)))).
Callback<getClassLevelLang>(c => calls.Add(c)). //added for debuging purposes only
Returns(productsNode.objectAttributes).
Verifiable();
}
上述测试失败,但有以下异常:
Test method ProductsImport.Console.Test.ProductsImportProcessUnitTest.RecursiveDownloadTest threw exception:
Moq.MockVerificationException: The following setups were not matched:
IProductsWebService mock => mock.GetClassLevel(It.Is<getClassLevelLang>(c => c.Equals(CID: , accessCode: xyz, classCode: tree1, depth: 1, language: all, partyCode: something, refId: )))
IProductsWebService mock => mock.GetClassLevel(It.Is<getClassLevelLang>(c => c.Equals(CID: cat1.1.1, accessCode: xyz, classCode: tree1, depth: 1, language: all, partyCode: something, refId: )))
IProductsWebService mock => mock.GetClassLevel(It.Is<getClassLevelLang>(c => c.Equals(CID: , accessCode: xyz, classCode: tree2, depth: 1, language: all, partyCode: something, refId: )))
Result StackTrace:
at Moq.Mock.VerifyAll()
我确定 ProductsWebService.GetClassLevel 被调用而getClassLevelLang.Equals
没有被调用(用调试器检查)。更奇怪的是,我calls
在调用VerifyAll()
list 之前检查了它的实际值,它是空的。