locked
Moles for internal sealed classes RRS feed

  • Question

  • Hi,

    I tried to mole an internal sealed class however Moles (V 1.0.0.0 and Pex V 0.90.50303.0) did not create the MSealedClass. Am I missing something? 

     

    namespace StubsTutorial1
    {
        internal sealed class SealedClass
        {
            public SealedClass() { }
    
            public bool SealedClassFunction(System.Type t)
            {
                return true;
            }
        }
    }

    Cheers,

    Andrej

    Thursday, April 8, 2010 8:23 AM

Answers

  • Hi Andrej,

    You are right: version 0.90 did not have a good support for internal types, both in the code and the documentation. In the next version of Moles (0.91) we have fixed the code generation bug you reported and also beefed up the documentation on dealing with internal types.

    Thanks for the detailled bug reports though!


    Jonathan "Peli" de Halleux - Give us your input about Pex!
    • Proposed as answer by Nikolai Tillmann Monday, April 12, 2010 8:01 PM
    • Marked as answer by Andrej Pie Wednesday, April 14, 2010 8:13 AM
    Friday, April 9, 2010 6:10 AM

All replies

  • You need to give 'internals visible to' rights to the generated .Moles assembly. The next version of the documentation will have more information about this. Meanwhile, you take a look at this blog entry http://iworkonsoftware.blogspot.com/2010/04/moles-and-internalsvisibleto.html .


    Jonathan "Peli" de Halleux - Give us your input about Pex!
    Thursday, April 8, 2010 11:48 AM
  • Thanks for the hint. I modified my code accordingly:

    using System.Runtime.CompilerServices;
    [assembly: InternalsVisibleTo("StubsTutorial1.Moles")]
    namespace StubsTutorial1
    {
        internal sealed class SealedClass
        {
            public SealedClass() { }
    
            public bool SealedClassFunction(System.Type t)
            {
                return true;
            }
        }
    }
    

    However now Moles fails to generate the moled types due to a compiler error:

    StubsTutorial1.g.cs(108,55): error CS0571: 'StubsTutorial1.Moles.MSealedClass.Behavior.set': cannot explicitly call operator or accessor 

    Looking at the generated code:

     public static void BehaveAsNotImplemented()
            {
                global::StubsTutorial1.Moles.MSealedClass.set_Behavior(global::Microsoft.Moles.Framework.MoleBehaviors.NotImplemented);
            }

    Behavior is an property and set_Behavior should not be called: http://msdn.microsoft.com/en-us/library/z47a7kdw(v=VS.71).aspx

    Changing the code to:

            public static void BehaveAsNotImplemented()
            {
                global::StubsTutorial1.Moles.MSealedClass.Behavior = global::Microsoft.Moles.Framework.MoleBehaviors.NotImplemented;
            }
    

    satisfies the compiler. Still not the full solution because MSealed is declared internal. So we need to add the test dll as a friend.

    [assembly: global::System.Runtime.CompilerServices.InternalsVisibleTo("StubsTutorialTest")]

    Now we can compile our test. In our test we want to mock MSealedClass.SealedClassFunctionType the method from the SealedClass. This method is declared internal in MSealedClass.

    It seems that there are a number of bug in Moles. Should I write a CR?

    Cheers, Andrej

     

    Thursday, April 8, 2010 4:11 PM
  • Hi Andrej,

    You are right: version 0.90 did not have a good support for internal types, both in the code and the documentation. In the next version of Moles (0.91) we have fixed the code generation bug you reported and also beefed up the documentation on dealing with internal types.

    Thanks for the detailled bug reports though!


    Jonathan "Peli" de Halleux - Give us your input about Pex!
    • Proposed as answer by Nikolai Tillmann Monday, April 12, 2010 8:01 PM
    • Marked as answer by Andrej Pie Wednesday, April 14, 2010 8:13 AM
    Friday, April 9, 2010 6:10 AM
  • Hi Jonathan,

    do you have already an idea when 0.91 will be released on MSDN? I wanted to continue the evaluation of Pex.

    Cheers,

    Andrej

    Wednesday, April 14, 2010 8:13 AM
  • Hi Jonathan,

    do you have already an idea when 0.91 will be released on MSDN? I wanted to continue the evaluation of Pex.

    Cheers,

    Andrej

    Wednesday, April 14, 2010 8:13 AM
  • We are planning on a release this week or next week.
    Jonathan "Peli" de Halleux - Give us your input about Pex!
    Wednesday, April 14, 2010 2:20 PM
  • Hi Jonathan,

    now that 0.91 is out I was eager to give it another try. My focus was on internal classes.

    My idea is to write something like this, given the example above.

      [TestMethod]
      [HostType("Moles")]
      public void TestSealedClass()
      {
       
       MSealedClass.SealedClassFunctionType = delegate(Type t)
       {
        return false;
       };
      }

    However SealedClassFunctionType is declared private by Moles.

    Am I missing something?

    Cheers,

    Andrej

    Wednesday, April 28, 2010 4:30 PM
  • You need to give visibility rights to the Test project in the project under test. All visiliby rights in the project under test are copied to the compiled Moles assembly. This means that if you can see SealedClass in the test project, you should also see MSealedClass.

    Is that the issue you are seeing?


    Jonathan "Peli" de Halleux - Become a Fan of Pex and Moles!
    Wednesday, April 28, 2010 4:57 PM
  • This thread was previously closed. Please open a new one.
    Jonathan "Peli" de Halleux - Become a Fan of Pex and Moles!
    Wednesday, April 28, 2010 4:57 PM