[英]Mocking GetEnumerator using Moq
我試圖在Microsoft.Office.Interop.Word程序集中模擬Variables接口
var variables = new Mock<Variables>();
variables.Setup(x => x.Count).Returns(2);
variables.Setup(x => x.GetEnumerator()).Returns(TagCollection);
private IEnumerator TagCollection()
{
var tag1 = new Mock<Variable>();
tag1.Setup(x => x.Name).Returns("Foo");
tag1.Setup(x => x.Value).Returns("Bar");
var tag2 = new Mock<Variable>();
tag2.Setup(x => x.Name).Returns("Baz");
tag2.Setup(x => x.Value).Returns("Qux");
yield return tag1.Object;
yield return tag2.Object;
}
我的代碼如下所示:
// _variables is an instance of Variables interface
var tags = from variable in _variables.OfType<Variable>()
where variable.Name == "Foo"
select variable.Value;
var result = tags.ToList();
上面代碼中的最后一行拋出NullReferenceException。 如果我使用foreach循環迭代_variables集合,我可以毫無問題地訪問Variable的模擬對象。 我在這做錯了什么?
嘗試:
variables
.As<IEnumerable>()
.Setup(x => x.GetEnumerator()).Returns(TagCollection);
有兩種不同的方法,一種在基本接口中聲明,另一種在Variables
聲明。
直接進行foreach
,會調用后者,因為該方法會隱藏基類型中相同的成員。 foreach
在存在時調用public
方法,在這種情況下, IEnumerable
無關緊要。
當您調用.OfType<Variable>()
Linq擴展時,引用將轉換為IEnumerable
接口,並且名稱隱藏不再存在。 調用基接口上的方法。
這就像之間的區別:
_variables.GetEnumerator();
和:
((IEnumerable)_variables).GetEnumerator();
您可以將模擬Moq視為:
public class TheTypeMoqMakes : Variables
{
Enumerator Variables.GetEnumerator()
{
// Use return value from after
// expression tree you provided with 'Setup' without 'As'.
// If you did not provide one, just return null.
}
Enumerator IEnumerable.GetEnumerator()
{
// Use return value from after
// expression tree you provided with 'Setup' with 'As<IEnumerable>'.
// If you did not provide one, just return null.
}
// other methods and properties
}
如果成員不是Setup
,Moq返回null的原因是你有MockBehavior.Loose
。 總是考慮使用MockBehavior.Strict
。
我無法理解為什么Variables
接口的作者在這種情況下選擇使用方法隱藏 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.