2008-10-23 87 views
53

我在哪裏可以找到MOQ的全面文檔?我只是開始嘲笑,難以理解我的頭腦。我已閱讀了所有鏈接http://code.google.com/p/moq/wiki/QuickStart,但似乎無法找到教程或溫柔的介紹。MOQ文檔在哪裏?

我也看了一下Rhino Mocks,但發現它很混亂。


是的 - 我讀了Stephen Walthers的文章 - 非常有幫助。我也通過鏈接。我似乎無法在 http://www.bestechvideos.com/2008/06/08/dimecasts-net-introduction-to-mocking-with-moq [無效鏈接]

觀看視頻具體來說我想,以確定事件是否是從嘲笑類提高。我無法獲得QuickStarts頁面上的事件示例來編譯。在谷歌組上,丹尼爾解釋說,CreateEventHandler只能處理類型爲EventHandler<TEventArgs>的事件,但即使如此,我也無法獲取代碼進行編譯。

更具體地說,我有一個類實現INotifyChanged

public class Entity : INotifyChanged 
{ 
    public event PropertyChangingEventHandler PropertyChanging; 

    public int Id 
     { 
      get {return _id;} 
      set { 
       _id = value; 
       OnPropertyChanged("Id"); 
       } 
     } 

    protected void OnPropertyChanged(string property) 
     { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
etc .....  
} 

如何嘲笑類來測試PropertyChanged事件是否被解僱?

Error 1 'CoreServices.Notifier' does not implement interface member System.ComponentModel.INotifyPropertyChanged.PropertyChanged'. 'CoreServices.Notifier.PropertyChanged' cannot implement 'System.ComponentModel.INotifyPropertyChanged.PropertyChanged' because it does not have the matching return type of 'System.ComponentModel.PropertyChangedEventHandler'.

+0

顯示的錯誤是一個visual studio /編譯器錯誤。只需從bin文件夾中刪除所有內容並重新構建即可。 – CodingBarfield 2012-01-02 14:43:01

+0

在我的回答中,鏈接至*介紹使用Moq *進行嘲弄是固定的。 – 2012-04-20 13:32:17

回答

15

你看了Introduction to Mocking with Moq:監守我得到這個錯誤,我不能重寫事件public event EventHandler<PropertyChangedEventArgs>?這是使用Moq的介紹性概述,適用於那些對一般嘲笑或Moq框架本身不熟悉的人。

1

I am trying to determine whether an event was raised from the mocked class.

是嗎?或者您是否試圖確定Id屬性是否已設置?請記住,默認情況下,模擬沒有任何行爲。它不會引發通知事件。

我會怎麼做:

const int ExpectedId = 123; 
mockEntity.VerifySet(x => x.Id = ExpectedId); 

這假定實體實現的接口;一個例子:

public interface IKeyedEntity 
{ 
    int Id { get; set; } 
} 

也就是說,如果EntityPOCO沒有有趣的行爲我既不會實現(比INotifyChanged等)的接口,也沒有嘲笑它。用實際的Entity實例進行測試(只是不要使用數據庫)。嘲笑服務和複雜的依賴關係。

更多起訂量功能,請參閱

Old style imperative mocks vs moq functional specificationsMock.Of - how to specify behavior? (thread)。我還發布了我自己的Moq v4 functional specifications的示例。