2013-10-30 53 views
3

我用假貨很容易爲我的對象生成假貨。現在我測試一下僞造的對象的某些方法在哪裏調用。事情是我想要每個單元測試重置假的狀態回到原來的狀態。FakeItEasy重置對象的呼叫

我需要這個,因爲我爲每個單元測試類生成一次假貨,所以我需要以某種方式重置呼叫狀態。

這是我的代碼看起來像:

[Test] 
    public void Test_Process_SampleMessageNEW_Should_Call_ISampleMessageConverter_GetRouteObject() 
    { 
     //Arrange 
     //should reset the state of the fakes, _sampleRouteConvertor and _orderDalcMock are fakes 
     _sampleRouteConvertor.BackToRecord(); 
     _orderDalcMock.BackToRecord(); 
     A.CallTo(() => _orderDalcMock.GetOrder(A<IRQOrderIDCriteria>.Ignored)) 
      .Returns(new OrderDTO() {Id = nextId++}); 
     SampleMessage sampleMessage = SampleMessageTestHelpers.GetSavedSampleMessage(); 
     sampleMessage.MaterialId = CacheMaterials.GetMaterials()[0].Code; 
     sampleMessage.MessageType = "NEW"; 

     //Act 
     SampleMessageProcessor sampleMessageProcessor = new SampleMessageProcessor(sampleMessage); 
     sampleMessageProcessor.Process(); 

     //Assert 
     A.CallTo(() => _sampleRouteConvertor.GetRouteObject(A<RoutineSample>.Ignored, A<List<ICacheTrackArea>>.Ignored)) 
      .MustHaveHappened(Repeated.Exactly.Once); 
    } 

回答

5

爲什麼不爲每個測試建立新的正版正貨?這將解決問題。

如果每次設置新假都是複雜的代碼塊,請編寫一個輔助函數來設置新假。

+1

您還有設置([TestInitialize])和拆除([TestCleanup])方法。 –

+0

+1這是我會寫的答案 –