0
我試圖編寫一個單元測試,將引發我的測試類綁定到一個模擬對象上的事件。如何測試如果引發事件導致一個方法被稱爲條件的參數值
我熱衷於測試,但是當我的測試類獲取其eventhandler調用時,它應該只調用eventhandler參數的某些值的方法。
即使我評論調用ProcessPriceUpdate(price)的代碼,我的測試似乎也會通過;
我在VS2005所以沒有lambda表達式請:(
所以......
public delegate void PriceUpdateEventHandler(decimal price);
public interface IPriceInterface{
event PriceUpdateEventHandler PriceUpdate;
}
public class TestClass
{
IPriceInterface priceInterface = null;
TestClass(IPriceInterface priceInterface)
{
this.priceInterface = priceInterface;
}
public void Init()
{
priceInterface.PriceUpdate += OnPriceUpdate;
}
public void OnPriceUpdate(decimal price)
{
if(price > 0)
ProcessPriceUpdate(price);
}
public void ProcessPriceUpdate(decimal price)
{
//do something with price
}
}
到目前爲止我的測試...:■
public void PriceUpdateEvent()
{
MockRepository mock = new MockRepository();
IPriceInterface pi = mock.DynamicMock<IPriceInterface>();
TestClass test = new TestClass(pi);
decimal prc = 1M;
IEventRaiser raiser;
using (mock.Record())
{
pi.PriceUpdate += null;
raiser = LastCall.IgnoreArguments().GetEventRaiser();
Expect.Call(delegate { test.ProcessPriceUpdate(prc); }).Repeat.Once();
}
using (mock.Playback())
{
test.Init();
raiser.Raise(prc);
}
}
有一點代碼可以說明。我想我明白你的意思,但是一小段代碼真的會有幫助。 :) – MattC 2010-03-30 09:28:56