No Detour RRS feed

  • Question

  • I am attempting to use Moles to instrument a sealed class in a closed-source library I am working with.  Here is an excerpt with the names changed to protect the guilty.

    namespace mysterious
      public sealed class MysteryClass : Object
        public static MysteryClass getInstance();
        public virtual bool isKnowable();

    I thought I should probably start my Moles adventure with something simple.  It looks something like this:


    using System;
    using Microsoft.Moles.Framework;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    [assembly: MoledType(typeof(MysteryClass))]
    namespace MysteryClass.Test
     public class MysteryClassTest
      public void ThisHasBeenATest()
       MMysteryClass.AllInstances.isKnowable = (mystery) => { return false; };
       //MMysteryClass.AllInstances.isKnowable = (mystery) => false; // Also tried this form
       MysteryClass deepMystery = MysteryClass.getInstance();


    Apparently the test host is not detouring the isKnowable method to my mock implementation because the assertion at the end of the test method always fails.  Am I doing anything obviously stupid?


    Thursday, January 6, 2011 9:40 PM

All replies

  • isKnowable is virtual. You need to mole the overloaded methods as well to make this work. Another approach is to replace getInstance to return a MMysteryClass who isKnowable method returns false (easier).
    Jonathan "Peli" de Halleux - Try Pex online at www.pexforfun.com!
    Monday, January 10, 2011 11:17 PM
  • Thank you.  That makes sense.
    Thursday, January 13, 2011 5:30 PM