Mocking a generic method (that uses TResult) with Microsoft Moles

Situation

There is a generic method that uses TResult which you want to mock using Microsoft Moles.

Problem

You can’t just assign a delegate like on a non-generic method.

Solution

  1. Create a delegate that behaves like the mocked method should. Define the concrete expected parameter types.
  2. Instantiate a new MolesDelegates.Func with the corresponding type-parameters and pass the delegate from step 1 as parameter.
  3. Call the mock and pass the MolesDelegates.Func from step 2 as parameter.

Example

Method to mock:

public static bool Save(TResult pData, string pFile, bool pOverwriteExistingFile)

Assign the mock:

MPersistence.SaveTResultStringBoolean(
  new MolesDelegates.Func(
    delegate(PlcData a, string s, bool b)
    {
      return false;
    }
  )
);

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.