我有一個單元測試,我嘲笑(與moq)一個對象,並讓它驗證它是否正確執行一個方法。此方法正在我創建的SUT(被測系統)中創建的線程中執行。當我想在Mock上執行VerifyAll()時,可能會發生線程仍在運行並且尚未執行該方法 - 未通過測試。單元測試結果,由處理線程檢索
有沒有辦法在正確的事情上解決這個問題?例如讓VerifyAll等待什麼?因爲現在,測試是不可靠的。
這是測試:
[Test]
public void TryToExecute_SubjectNotYetBeingProcessed_ProcessesSubject()
{
var subject = new Subject();
var rule = new Mock<IBusinessRule>();
rule.Setup(x => x.RunChildren(subject)); //RunChildren will be called in a seperate Thread
InBuffer.TryToExecute(subject, rule.Object);
rule.VerifyAll(); //It could be possible that the Thread is still running and that RunChildren() isn't invoked yet, thus failing the test.
}
public void TryToExecute(Subject subject, IBusinessRule rule){
var thread = new Thread(x =>
{
SetCurrentAsProcessing(subject);
rule.RunChildren(subject) // This is where it executes
RemoveFromProcess(subject);
});
thread.Start(); // Start the Thread
}
請問您可以粘貼TryToExecute方法嗎? – Grzenio 2010-06-22 12:24:45
當然,發佈它。不知道爲什麼你需要它,因爲問題的根源是明確的。線程仍可能正在執行,驗證將有可能失敗。 – Bas 2010-06-22 12:29:56
如何檢測安裝是否在實際應用中完成? 我認爲你需要類似processComplete事件或狀態對象。 – 2010-06-22 12:35:45