Mocking SPContentType

2 posts, 0 answers
  1. Resu
    Resu avatar
    1 posts
    Member since:
    Nov 2011

    Posted 15 Dec 2011 Link to this post

    Hello together

    I'm just trying to mock the SPContentType class out of the SharePoint assembly but I'm facing several problems because of its nested constructors. I've already searched for a solution but cannot find any information about the mocking of this class.

    When simply trying to mock this class:

    SPContentType mockedContentType = Mock.Create<SPContentType>();

    I receive the following error:

    Initialization method X.KeywordFinderTests.Initialize threw exception. System.MissingMethodException: System.MissingMethodException: Constructor on type 'Microsoft.SharePoint.SPContentType' not found..

    After that I had a look at the SPContentType class using dotPeek; but it has an internal empty constructor. Well it has many internal calls so I tried the following code to avoid them:

    SPContentType mockedContentType = Mock.Create<SPContentType>(SPContentTypeId.Empty);
    SPContentType mockedContentType = Mock.Create<SPContentType>(new SPContentTypeId("0x01"));

    SPContentType mockedContentType = Mock.Create<SPContentType>(Constructor.Mocked);

    But those constructor calls fail with the same error message. Even trying to create a mock using any of the other public constructor always leads to an error message.

    SPContentType mockedContentType = new SPContentType(new ContentTypeId("0x01"), mockedContentTypeCollection, "My Content Type");
    Initialization method X.KeywordFinderTests.Initialize threw exception. System.NullReferenceException: System.NullReferenceException: Object reference not set to an instance of an object..

    Can some one help me to mock this class?

  2. Ricky
    Ricky avatar
    467 posts

    Posted 20 Dec 2011 Link to this post

    Hi Resu,
    Thanks again for reporting the issue.  There was a bug in resolving internal default constructor that we recently fixed. Should you need the internal build that contains the fix then please create a support ticket where I will send it over.

    Once you have updated to the build, you will be able to do the following as expected:

    SPContentType mockedContentType = Mock.Create<SPContentType>();

    Kind Regards,
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft R3 2016 release webinar banner
Back to Top