好吧,我想教我自己使用模擬框架進行測試,我在VB.NET工作,我是新的lambda表達式和我以前的所有應用程序都寫在版本2005或更早版本。我現在有2010年。 所以我試過Rhino.Mocks,但發現很難得到我的頭,主要是因爲舊的語法。因爲,現在似乎沒有人在VB.NET中成爲博客,我一直在研究C#示例並試圖弄清楚發生了什麼。FakeItEasy VB.NET問題與參數
所以我有一種情況,我將一個接口傳遞給一個類的構造函數並保存該接口的引用。當在對象上調用一個方法並且引發事件時,應該由實現該接口的類來處理。
我遇到了麻煩,所以我嘗試在C#中創建一個簡單的版本並重復vb.net中的步驟。
所以我的界面:
public interface IBroadcastClient
{
void MessageReceivedHandler(string msg);
}
引發此事件類:
public class Broadcaster
{
public Broadcaster(IBroadcastClient c)
{
_client= c;
this.SendMessage += new MessageReceived(_client.MessageReceivedHandler);
}
private IBroadcastClient _client;
public event MessageReceived SendMessage;
public void SendMessageNow()
{
string _Message;
if (SendMessage != null)
{
_Message = @"Yay!";
SendMessage(_Message);
}
}
}
測試:
[TestMethod]
public void TestSendMessageWithIgnoreParameter()
{
//string msg = @"Yay!";
var client = A.Fake<IBroadcastClient>();
Broadcaster b = new Broadcaster(client);
b.SendMessageNow();
A.CallTo(() => client.MessageReceivedHandler(A<string>.Ignored)).MustHaveHappened();
}
此經過,至今沒有任何問題。
現在要在vb.net中嘗試相同的操作; 相同的接口和廣播類,只是在vb.net而不是C#,最初在單元測試之後。
<TestMethod()>
Public Sub TestMethod1()
Dim client = A.Fake(Of IBroadcastClient)()
Dim b As New Broadcaster(client)
b.SendMessageNow()
NextCall.To(client).MustHaveHappened()
client.MessageReceivedHandler(A(Of String).Ignored)
End Sub
失敗,並顯示以下錯誤消息; 「的呼籲中 預計至少一次找到它,但發現它#0次斷言失敗以下電話: TestFakeItEasyVB.IBroadcastClient.MessageReceivedHandler(MSG): 1:TestFakeItEasyVB.IBroadcastClient.MessageReceivedHandler(MSG:」耶!「)」
有趣的是這樣寫的;
<TestMethod()>
Public Sub TestMethod3()
Dim client = A.Fake(Of IBroadcastClient)()
Dim b As New Broadcaster(client)
b.SendMessageNow()
A.CallTo(Sub() client.MessageReceivedHandler(A(Of String).Ignored)).MustNotHaveHappened()
End Sub
也會失敗並顯示相同的錯誤消息,但此版本的測試通過。
<TestMethod()>
Public Sub TestMethod2()
Dim client = A.Fake(Of IBroadcastClient)()
Dim b As New Broadcaster(client)
b.SendMessageNow()
NextCall.To(client).MustHaveHappened()
client.MessageReceivedHandler("Yay!")
End Sub
這種變化也通過在C#中,我是奎德里什麼我做錯了讓測試忽略傳遞給造假事件處理函數的參數?
在你TestMethod3您指定「 MustNotHaveHappened「,但在其他測試中你有」MustHaveHappened「,我想這是錯誤的權利? – 2011-04-16 13:21:11