Exception trying to mock SharePoint 2010 UserProfileManager with JustMock

269 Views Asked by At

I'm trying to create a mock version of UserProfileManager in order to effectively unit test a Sharepoint 2010 Web Part Every time I run this code, I get the the exception being thrown below. I've verified that the feature works when I debug the code that calls UserProfileManager on the Sharepoint machine - two different Sharepoint machines, in fact. Here is the code in question:

var _mockServiceContext = Mock.Create<SPServiceContext>();
var _mockUserProfileManager = Mock.Create<UserProfileManager>(new object[] {_mockServiceContext});

Unable to create instance of class SharePoint.MyWebPartTest. Error: Microsoft.SharePoint.SPException: The trial period for this product has expired..
    at Microsoft.Office.Server.UserProfiles.ProfileManagerBase.ValidateLicensing()
   at Microsoft.Office.Server.UserProfiles.ProfileManagerBase..ctor(SPServiceContext serviceContext)
   at Microsoft.Office.Server.UserProfiles.ProfileManagerBase..ctor(SPServiceContext serviceContext, Boolean ignorePrivacy)
   at Microsoft.Office.Server.UserProfiles.UserProfileManager..ctor(SPServiceContext serviceContext, Boolean IgnoreUserPrivacy, Boolean backwardCompatible)
   at Microsoft.Office.Server.UserProfiles.UserProfileManager..ctor(SPServiceContext serviceContext)
   at Castle.Proxies.UserProfileManagerProxy..ctor(IEventsMixin, IMockReplicator, IMockMixin, IInterceptor[], SPServiceContext)
   at DynamicMethod_6f2d60ffa4f94cb4ba57556150461ef9(Object[])
   at ...()
   at ..[](Func`1 )
   at ..(Type , Object[] )
   at ..(Type , List`1 , Type , Object[] )
   at ..(Type , Type[] , ProxyGenerationOptions , Object[] , IInterceptor[] )
   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(Object[] args)

Has anyone else seen this issuue?

1

There are 1 best solutions below

0
On

Full disclosure: I'm a developer on the JustMock team.

When you call the Mock.Create<T>(object[] args) overload, you essentially say that you want a loose mock but you still want the constructor of the mocked type to run. From the stack trace I see that it is in fact that constructor that throws the exception. Is that what you actually want? Because that's rarely useful.

If you need a true loose mock it is best that you bypass its constructor. Basically, use the Mock.Create<T>() overload, like so:

var _mockUserProfileManager = Mock.Create<UserProfileManager>();