Where is the MOQ documentation?

C#.NetTestingMockingMoq

C# Problem Overview


Where can I find comprehensive documentation for MOQ? I'm just starting with mocking and am having difficulty getting my head around it. I've read through all the links at http://code.google.com/p/moq/wiki/QuickStart but can't seem to find a tutorial or gentle introduction.

I have also looked briefly at Rhino Mocks but found it very confusing.


Yes - I read Stephen Walthers article - very helpful. I also went through the links. I can't seem to watch the video at http://www.bestechvideos.com/2008/06/08/dimecasts-net-introduction-to-mocking-with-moq</strike> [broken link]

Specifically I am trying to determine whether an event was raised from the mocked class. I can't get the example for events on the QuickStarts page to compile. On the google groups, Daniel explained that CreateEventHandler can only handle an event of type EventHandler<TEventArgs>, but even then I can't get the code to compile.

More specifically I have a class that implements 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 .....    
}

How do I mock the class to test whether the PropertyChanged event was fired? I can't rewrite the event to public event EventHandler<PropertyChangedEventArgs> becuase I get this error:

> 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'.

C# Solutions


Solution 1 - C#

Moq's latest documentation is now available at github wiki page:

https://github.com/Moq/moq4/wiki/Quickstart

Previously they were on Google Code. As well as the wiki and other online resources, there's full documentation in Windows .CHM help-file format included in the Moq binary download linked from the Moq homepage.

Solution 2 - C#

Have you watched Introduction to Mocking with Moq? It's an introductory overview of using Moq and is intended for those who are new to either mocking in general, or the Moq framework itself.

Solution 3 - C#

Have you read the linked pages at https://github.com/Moq/moq4/wiki/Quickstart ? for example this one (probably moved to stephen walthers personal blog)

Solution 4 - C#

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

Are you? Or are you trying to determine if the Id property was set? Remember, by default a mock has no behavior. It's not raising notification events.

I'd do:

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

This assumes that Entity implements an interface; one example:

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

That said, if Entity is a POCO with no interesting behavior I would neither implement an interface (other than INotifyChanged) nor mock it. Test with an actual Entity instance (just don't use a database). Reserve mocking for services and complex dependencies.

For more Moq features, see

Old style imperative mocks vs moq functional specifications and Mock.Of - how to specify behavior? (thread). I also posted my own example of Moq v4 functional specifications.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJeremy HoltView Question on Stackoverflow
Solution 1 - C#Dylan BeattieView Answer on Stackoverflow
Solution 2 - C#Bill the LizardView Answer on Stackoverflow
Solution 3 - C#Brian J CardiffView Answer on Stackoverflow
Solution 4 - C#TrueWillView Answer on Stackoverflow