[英]shim a sealed class singleton method and with MS Fakes
我有一個密封類單例Foo及其方法:
public string GetFolderPath(FooFolder folder)
{
IBoo boo = this.GetBoo();
return boo.GetFolderPath(folder);
}
並想要使用GetFolderPath方法為名為FindFile的方法編寫測試,如下所示:
[TestMethod]
public void FindFile()
{
string expectedPath = Path.Combine(Path.GetTempPath(), "TestPath");
using (ShimsContext.Create())
{
Fakes.ShimFoo.AllInstances.GetFolderPathFolder
= () => { return "C:\\...\\Temp"; };
}
string actualPath = WorkflowHelper.FindFile("TestPath");
Assert.AreEqual(expectedPath, actualPath);
}
問題是我遇到以下編譯錯誤:
委托不接受0個參數
在類似的問題中, 如何對OpenFileDialog.ShowDialog方法進行補片,該問題的解決方法如下:
[TestMethod]
public void SomeTest()
{
using (var context = ShimsContext.Create())
{
Nullable<bool> b2 = true;
ShimCommonDialog.AllInstances.ShowDialog = (x) => b2;
var sut = new Sut();
var r = sut.SomeMethod();
Assert.IsTrue(r);
}
}
所以我嘗試了同樣的方法...根據事實,GetFolderPath是帶有1個參數的方法...
下一個問題是我遇到以下編譯錯誤:
委托不接受1個參數
所以我的問題是:
是否可以對密封班特別是單身人士進行墊料? 如果是這樣,我的錯誤是什么?
謝謝你的期待
請注意,在ShimsContext
之后,將銷毀在ShimsContext
的生存ShimsContext
分配的所有ShimsContext
。
在您的示例中,您在綁定了ShimsContext
生存期的using塊之外調用WorkflowHelper.FindFile
,因此Foo.GetFolderPath
定義不再有效,並且對FindFile的調用將使用原始方法定義。
只需將您的方法調用移到using
塊內,它將起作用:
using (ShimsContext.Create())
{
Fakes.ShimFoo.AllInstances.GetFolderPathFolder = ...; // your lambda expression
string actualPath = WorkflowHelper.FindFile("TestPath");
}
Assert.AreEqual(expectedPath, actualPath);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.