chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
I'm wondering if it is possible to mock a generic abstract type. The generic type of the abstract class is actually the inheriting type. Here is a sample declaration:

ContentRating<TContentRating> : IComparable<TContentRating> where TContentRating: ContentRating<TContentRating>{}

I dont know if this is possible or if I am just not understanding the expression I need to use to create a mock of this.

Thanks for any help.
asked by jnapier (9.6k points)

1 Answer

0 votes
This is a complex example as this Type is both abstract and that recursivley defines the generic parameter as itself.

Here is a small workaround:
Create a dummy abstract class that extends your type. (I will name it the same just to confuse our readers)
 public abstract class ContentRating : ContentRating<ContentRating>

Now you can do the following:
MockObject mock = MockManager.MockObject(typeof(ContentRating<ContentRating>));
// get the actual object
ContentRating<ContentRating> mockedObject = (ContentRating<ContentRating>)mock.Object;
answered by scott (32k points)