This is a migrated thread and some comments may be shown as answers.

Mock.Create<SPServiceContext>() causes Serialization exception

9 Answers 154 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Nathanael
Top achievements
Rank 1
Nathanael asked on 10 May 2013, 09:25 PM
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?

9 Answers, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 13 May 2013, 11:04 AM
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.
0
Nathanael
Top achievements
Rank 1
answered on 19 Jul 2013, 04:38 PM
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?
0
Kaloyan
Telerik team
answered on 22 Jul 2013, 08:51 AM
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.
0
Nathanael
Top achievements
Rank 1
answered on 25 Jul 2013, 07:47 PM
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
0
Nathanael
Top achievements
Rank 1
answered on 26 Jul 2013, 03:49 PM
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.
0
Kaloyan
Telerik team
answered on 29 Jul 2013, 10:03 AM
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.
0
Nathanael
Top achievements
Rank 1
answered on 30 Jul 2013, 07:32 PM
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:

0
Kaloyan
Telerik team
answered on 31 Jul 2013, 12:56 PM
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.
0
Nathanael
Top achievements
Rank 1
answered on 13 Aug 2013, 05:30 PM
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 :)
Tags
General Discussions
Asked by
Nathanael
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
Nathanael
Top achievements
Rank 1
Share this question
or