2
我想寫一個單元測試使用JustMock忽略事件。試圖單元測試一個事件與JustMock
我做不是想測試事件本身,因爲它調用各種內部函數,這將需要大量的模擬工作。
下面是示例代碼的快速位:
public class Sample
{
public delegate void OneParameterFunction(int someVar);
public event OneParameterFunction TheEvent;
public Sample()
{
TheEvent += CallMe;
}
public void CallMe(int someVar)
{
Debug.WriteLine("CallMe was fired with parameter: " + someVar);
}
public void FireEvent()
{
// do stuff, business logic here...
if (TheEvent != null)
TheEvent(3);
}
}
這裏是測試我會愛來寫,但不能:
[TestClass]
class EventMocking
{
[TestMethod]
public void DoNothingOnEvent()
{
var s = new Sample();
Mock.Arrange(() => s.TheEvent(Arg.AnyInt))
.DoNothing();
Mock.Arrange(() => s.CallMe(Arg.AnyInt))
.OccursNever();
s.FireEvent();
Mock.Assert(() => s.CallMe(Arg.AnyInt));
}
}
但我收到以下編譯器錯誤:
Error 1 The event 'Sample.TheEvent' can only appear on the left hand side of += or -= (except when used from within the type 'Sample') C:\BizObjectTests\EventMocking.cs
有沒有人有任何建議如何阻止事件傳播?我也不想Mock.Create<T>
由於多種原因,其中一個是我再次必須設置更多的測試數據/對象。