Mock.Create<SPServiceContext>() causes Serialization exception

10 posts, 0 answers
  1. Nathanael
    Nathanael avatar
    9 posts
    Member since:
    Oct 2010

    Posted 10 May 2013 Link to this post

    I'm trying to write some unit tests for a Sharepoint 2010 Webpart that uses UserProfile manager.
    In order to mock UserProfileManager, I also need to mock SPServiceContext.

    When I try to assign my mock variable, like this:

    _mockServiceContext = Mock.Create<SPServiceContext>();

    The following exception is thrown:

    Type 'Telerik.JustMock.MockException' in assembly 'Telerik.JustMock, Version=2013.1.507.0, Culture=neutral, PublicKeyToken=721b6c5bc0326b3a' is not marked as serializable.

    Any idea what I'm doing wrong?
  2. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 13 May 2013 Link to this post

    Hi Nathanael,

    Thank you for reporting this issue.

    It appears this is a bug in JustMock 2013 Q1 SP2 which has already been fixed. The fix will be included in the upcoming release this month.

    I hope this information helps.

    P.S. For reporting this, we have granted you some Telerik points.

    All the best,
    Kaloyan
    the Telerik team
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
  3. DevCraft R3 2016 release webinar banner
  4. Nathanael
    Nathanael avatar
    9 posts
    Member since:
    Oct 2010

    Posted 19 Jul 2013 Link to this post

    I've had a chance to use the latest version of JustMock, and the same original line of code throws the following exception: Type is not resolved for member 'Telerik.JustMock.Core.ElevatedMockingException,Telerik.JustMock, Version=2013.2.611.0, Culture=neutral, PublicKeyToken=721b6c5bc0326b3a'. Anything else I can try?
  5. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 22 Jul 2013 Link to this post

    Hi Nathanael,

    I checked this once again. The following example works as expected since JustMock Q2 release:
    // Arrange
    var expectedGUID = new Guid();
     
    var _mockServiceContext = Mock.Create<SPServiceContext>();
    Mock.Arrange(() => _mockServiceContext.SiteSubscriptionId).Returns(new SPSiteSubscriptionIdentifier(expectedGUID));
     
    // Act
    var actual = _mockServiceContext.SiteSubscriptionId;
     
    // Assert
    Assert.AreEqual(expectedGUID, actual.Id);

    Please, make sure you have updated the JustMock references inside your project.


    I hope this solves the issue.

    Regards,
    Kaloyan
    Telerik
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
  6. Nathanael
    Nathanael avatar
    9 posts
    Member since:
    Oct 2010

    Posted 25 Jul 2013 Link to this post

    I just tried this again with a freshly installed Sharepoint 2010 VM.
    Can you verify what version of Unity you're using to run a successful test? I'm not able to download the latest version for this test project because it's built for .NET 3.5
  7. Nathanael
    Nathanael avatar
    9 posts
    Member since:
    Oct 2010

    Posted 26 Jul 2013 Link to this post

    This is the error coming in now, with a fresh rebuild of a Sharepoint VM. Does JustMock Lite not support mocking Sharepoint?

    Cannot mock 'Microsoft.SharePoint.SPServiceContext'. JustMock Lite can only mock interface members and virtual/abstract members in non-sealed classes on instances created with Mock.Create. For any other scenario you need to have the full version of JustMock.
  8. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 29 Jul 2013 Link to this post

    Hi Nathanael,

    This is exactly the case. As SPServiceContext is a sealed class, you will need the profiler in order to mock it. More about mocking sealed classes with JustMock can be found here.

    Let me know if there is anything else I could help you with.

    Regards,
    Kaloyan
    Telerik
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
  9. Nathanael
    Nathanael avatar
    9 posts
    Member since:
    Oct 2010

    Posted 30 Jul 2013 Link to this post

    I'm now using the full version of JustMock with a trial license. I'm running this line:
    _mockServiceContext = Mock.Create<SPServiceContext>();

    and I'm getting the following error:

    Telerik.JustMock.Core.ElevatedMockingException was unhandled by user code
      HResult=-2146233088
      Message=Cannot mock 'Microsoft.SharePoint.SPServiceContext'. The profiler must be enabled to mock, arrange or execute the specified target.
      Source=Telerik.JustMock
      StackTrace:
           at Telerik.JustMock.Core.MocksRepository.Create(Type   , Object[]   , IEnumerable`1    , IEnumerable`1    , IEnumerable`1    , Type[]    , Boolean    , Boolean    , IEnumerable`1    )
           at    .   .Create(MocksRepository    , Type   , Object[]    , Nullable`1    , Type[]    , Nullable`1    , IEnumerable`1    , List`1    , List`1    , List`1    )
           at Telerik.JustMock.Mock.   [  ]()
           at    .   .   [  ](Func`1    )
           at Telerik.JustMock.Mock.Create[T]()
           at Ctp.SharePoint.MyPhotosWebPart.Test.MyPhotosTest.InitMockObjects() in c:\work\CTP\IP Lab\Toolkits\SharePoint\v1.0\DEV\Ctp.SharePoint.MyPhotosWebPart.Test\MyPhotosTest.cs:line 48
           at Ctp.SharePoint.MyPhotosWebPart.Test.MyPhotosTest..ctor() in c:\work\CTP\IP Lab\Toolkits\SharePoint\v1.0\DEV\Ctp.SharePoint.MyPhotosWebPart.Test\MyPhotosTest.cs:line 28
      InnerException:

  10. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 31 Jul 2013 Link to this post

    Hi Nathanael,

    Please, make sure you have the JustMock profiler enabled when executing the test. To enable it, navigate to the JustMock menu inside Visual Studio.

    If the above is not the case, please send us the event logs with ID 1022. To do this:
    1. Fail your test once again (just to be sure the correct event log will be sent);
    2. Open Event Viewer and navigate to Windows Logs -> Application;
    3. Copy the latest event logs with an ID 1022;
    4. Send them with your next reply.

    You can also try and solve the issue by following the steps in this article.

    I hope this helps. Please let me know with the results.

    Regards,
    Kaloyan
    Telerik
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
  11. Nathanael
    Nathanael avatar
    9 posts
    Member since:
    Oct 2010

    Posted 13 Aug 2013 Link to this post

    Thank you for the help. I was able to get past that step, and now I'm on to the next error. I'll start a new thread for that though, since it's not about SPServiceContext anymore :)
Back to Top
DevCraft R3 2016 release webinar banner