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

Mocking SharePoint

3 Answers 193 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Johan Kartiwa
Top achievements
Rank 2
Johan Kartiwa asked on 15 Apr 2010, 08:45 AM
Hello.

I'm currently trying out JustMock to make a SharePoint Unit test.
However I have difficulties mocking SPSite and SPWeb objects.
Any advice?

The method that I'm trying to mock is roughly as follows:
SPSite site = SPContext.Current.Site; 
SPSecurity.RunWithElevatedPrivileges(delegate() { 
    using (SPSite mysite = new SPSite(site.ID)) { 
        using (SPWeb myweb = mysite.OpenWeb()) { 
            SPList = myweb.Lists["List_Name"]; 
            // do something else 
        } 
    } 
}); 

3 Answers, 1 is accepted

Sort by
0
Mike
Top achievements
Rank 1
answered on 16 Apr 2010, 09:50 PM
I too am looking into the ability to fake various SharePoint objects.

I have been playing with Typemock, and with that framework I would do something like

// we fake SPContext and all of its classes 
Isolate.Fake.StaticMethods<SPContext>(Members.ReturnRecursiveFakes); 
 
// create a fake SPSite, and make sure all of it's properties also have fake objects 
var fakeSite = Isolate.Fake.Instance<SPSite>(Members.ReturnRecursiveFakes); 
 
// the next time a new instance of SPSite is created, use our fake one instead 
Isolate.Swap.NextInstance<SPSite>().With(fakeSite); 

The ReturnRecursiveFakes is a super handy feature, and I'd love to see something similar in JustMock - hint hint Telerik :)

When I tried to do something similar in JustMock, I first enabled JustMock through the menu in Visual Studio since we will be creating mocks on static properties and sealed classes. I then tried the following code in my unit test

// first we create a fake  
var fakeContext = Mock.Create<SPContext>(); 
 
// make sure our fake SPContext is used 
Mock.Arrange(() => SPContext.Current).Returns(fakeContext); 
 
Assert.IsNotNull(SPContext.Current, "The current SPContext shouldn't be null");

This works, but all of the SPContext properties are null since we don't have the ability to recursively fake its objects. So I then tried to manually setup the other SP objects we need and ran into a couple of problems. I first tried to create a fake SPSite as follows:

var fakeSite = Mock.Create<SPSite>(); 

and I received the following error and stack trace:

Test method UnitTests.CreateSeriousIncidentReportWebPartJustMockTests.MyTest threw exception:  System.Reflection.AmbiguousMatchException: Ambiguous match found.. 
 
System.DefaultBinder.BindToMethod(BindingFlags bindingAttr, MethodBase[] canidates, Object[]& args, ParameterModifier[] modifiers, CultureInfo cultureInfo, String[] names, Object& state) 
System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) 
System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) 
System.Activator.CreateInstance(Type type, Object[] args) 
Telerik.DynamicProxy.Proxy.Create(Type target, Boolean includeObjectOverrides, Object[] args) in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\DynamicProxy\Proxy.cs: line 60 
Telerik.DynamicProxy.ProxyFactory.Create() in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\DynamicProxy\ProxyFactory.cs: line 83 
Telerik.DynamicProxy.Fluent.FluentProxy.NewInstance() in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\DynamicProxy\Fluent\FluentProxy.cs: line 64 
Telerik.DynamicProxy.Proxy.Create(Type target, Action`1 action) in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\DynamicProxy\Proxy.cs: line 85 
ctor(Type target, BehaviorMode mode, Object[] args) in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\MockObject.cs: line 41 
Telerik.JustMock.Mock.Create(Type target, BehaviorMode mode, Object[] args) in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\Mock.cs: line 290 
Telerik.JustMock.Mock.Create(Type target, Object[] args) in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\Mock.cs: line 275 
Telerik.JustMock.Mock.Create[T]() in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\Mock.cs: line 264 
UnitTests.CreateSeriousIncidentReportWebPartJustMockTests.MyTest() in C:\workspaces\SD36.CriticalIncidents\Main\Source\UnitTests\CreateSeriousIncidentReportWebPartJustMockTests.cs: line 70 
 

I am guessing this is because SPSite doesn't have a default constructor ... although I would like to be able to create a fake SPSite without passing it any information. So I then tried the following code:

Guid siteId = Guid.NewGuid(); 
var fakeSite = Mock.Create<SPSite>(siteId); 

and received the following error:

Initialization method UnitTests.CreateSeriousIncidentReportWebPartJustMockTests.MyTestInitialize threw exception. System.IO.FileNotFoundException:  System.IO.FileNotFoundException: Could not load file or assembly 'Telerik.CodeWeaver.Hook.Delegate_2c4a8880f1314fbf925ee4c0c6e1fc34, Version=1.0.0.0, Culture=neutral, PublicKeyToken=db35614751eb4716' or one of its dependencies. The system cannot find the file specified. --->  System.IO.FileNotFoundException: Could not load file or assembly 'Telerik.CodeWeaver.Hook.Delegate_2c4a8880f1314fbf925ee4c0c6e1fc34, Version=1.0.0.0, Culture=neutral, PublicKeyToken=db35614751eb4716' or one of its dependencies. The system cannot find the file specified.WRN: Assembly binding logging is turned OFF. 
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1. 
Note: There is some performance penalty associated with assembly bind failure logging. 
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog]. 

My test project is referencing the following assemblies:
- Telerik.JustMock.dll
- Telerik.JustMock.AddIn.dll
- Telerik.CodeWeaver.Hook.dll

Is this a bug in JustMock, or am I missing something?

Cheers.
Mike
0
Mike
Top achievements
Rank 1
answered on 16 Apr 2010, 10:06 PM
I forgot to mention that I am running Visual Studio 2008 SP1 on Windows 7 32-bit. Hopefully that will help diagnose the issue.

Cheers.
Mike
0
Ricky
Telerik team
answered on 18 Apr 2010, 08:59 PM
Hi Mike,

Thanks for the detailed steps for reproducing the issue. I wil try it out and get back to you shortly.

Regards,
Mehfuz
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
General Discussions
Asked by
Johan Kartiwa
Top achievements
Rank 2
Answers by
Mike
Top achievements
Rank 1
Ricky
Telerik team
Share this question
or