我試圖對一個在單獨的線程上做一些事情的類運行單元測試,然後通知父母它已經完成了使用事件處理程序的任務。這是我的測試代碼:掛鉤到單元測試內的事件
[TestMethod()]
public void InitTest()
{
Controller target = new Controller();
target.FinishedCommand += delegate(bool success)
{
Assert.AreEqual(true, success);
};
target.Init();
while (true)
{
}
}
這是我測試的方法:
public delegate void ControllerEventHandler(bool success);
public class Controller
{
public event ControllerEventHandler FinishedCommand;
public void Init()
{
FinishedCommand(true);
}
}
我意識到的init()不使用一個新的線程,但我只是想確保測試正在運行。測試只是永遠停留在while()循環中,而匿名委託從不輸入。
活動連接不正確?在單元測試中可以像這樣使用事件嗎?
感謝。我的例子實際上並沒有使用單獨的線程,但我會實現這一點,並且您的答案很有幫助。 – user2445507