cannot specify both a constraint class and the 'class' or 'struct' constraint --- Moles bug RRS feed

  • Question

  • Moles generates an invalid stub that will not compile for the following

    public interface IBaseInterface<T>  where T: class
            IEnumerable<TChild> GetAllOfType<TChild>(TChild child) where TChild : class, T;

        public interface IAnotherInterface : IBaseInterface<MyClass>{}

        public class MyClass{}


    Let me know if you have a work around


    Thursday, January 20, 2011 9:56 PM

All replies

  • Did you give up?
    Friday, February 11, 2011 9:26 PM
  • The work around for bug is starting cause quiet a mess inside my solution. To reproduce the error you can cut paste those 6 lines to any file and receive the error.  I am not getting the feeling that it will be resolved anytime soon. Guess I have to switch back to Moq. 
    Thursday, March 10, 2011 9:34 PM
  • The current workaround is to filter out the generation for the type that creates an issue. For that, you will need to edit the .moles file that controls the generation of the stub for the assembly that contains those types. It should look like something like this (more details in the reference manual):

    <Moles ...>
      <Assembly ... />
          <Remove TypeName="IBaseInterface!" />

    Jonathan "Peli" de Halleux - Try Pex online at www.pexforfun.com!
    Tuesday, March 15, 2011 5:38 AM
  • This is a very interresting corner case: I suspect it is not possible to implement IAnotherInterface in C#. It might be possible if IBaseInterface would be instantiated with an interface.

    We will make sure that the next version of Moles does not generate such invalid code.

    Jonathan "Peli" de Halleux - Try Pex online at www.pexforfun.com!
    Tuesday, March 15, 2011 6:03 AM
  • Thanks for the update. What do you mean not possible to implement in c#? Here is an example that is much closer to my actual case. It complies but cannot be moled. I have a repository that needs to handle entities in a general since. I need to mole IAnimalRepository for testing. I didn't think it was too much of a corner case. How close do you think you are to a release with a fix?


     public interface IRepository<T> where T : class
            IEnumerable<TChild> GetAllOfType<TChild>() where TChild : class, T;
        public class BaseRepository<T> : IRepository<T> where T : class
            public IEnumerable<TChild> GetAllOfType<TChild>() where TChild : class, T
                //do some generalized restore stuff here
                return null;
        public interface IAnimalRepository : IRepository<Animal> { }
        public class AnimalRepository : BaseRepository<Animal>, IAnimalRepository

        public class Animal { }
        public class Dog:Animal{}

        public class SampleServiceLayerCode
            public IEnumerable GetDogs()
                var repository = new AnimalRepository();
                return repository.GetAllOfType<Dog>();
    Monday, April 4, 2011 9:30 PM