chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
Hi all:

This is my first post in the Typemock forum. I'm completely new to Typemock, .Net and Sharepoint. Any help would be appreciated.

I have installed the Typemock for SharePoint and hooked it up with VS2008. I tried to run a very simple test, but I've been getting exception and could not get around it...

Below is the input, output and config (sorry about formatting):


using TypeMock.ArrangeActAssert;

[TestClass]
public class SomeTest
{
[TestMethod]
[Isolated]
Public void DummyTest()
{
SPWeb web = Isolator.Fake>Instance<SPWeb>(Members.ReturnRecursiveFakes);
Isolate.Swap.NextInstance<SPWeb>().With(web);
SPList fakedSPList = Isolate.Fake.Instance<SPList>();
Isolate.WhenCalled(() => web.Lists["List"]).WillReturn(fakedSPList);

}
}


I ran that, and got the following error:

System.NullReferenceException: Ob ject reference not set to an instance of an object..

Microsoft.SharePoint.SPWeb.SPWebConstructor(SPSite site, String url, Boolean bExactWebUrl, SPRequest request)
Microsoft.SharePoint.SPWeb..ctor(SPSite, site, String url)
eo.CreateFakeInstance[T](Members behavior, Constructor constructorFlag, Constructor baseConstructorFlag, Type baseType, Object[] ctorArgs)
eo.Instance[T](Members behavior)
(Points to the SPWeb web line as source of error)
TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodNAme, Object methodParameters, Boolean isInjected)
(Points to line 0 of my test class)


I have added TypeMock, Typemock.ArrangeActAssert and Typemock.CLI.Common into my Reference.

I have no ideas whats going on. If I leave the SPWeb code out, the SPList works properly (I can see a whole heap of stuff getting mocked in the trace). But I just couldn't mock the SPWeb object. Please help!!
asked by chrischan (1.1k points)

5 Answers

0 votes
Hi there.

Looking at the error stack:

Microsoft.SharePoint.SPWeb.SPWebConstructor(SPSite site, String url, Boolean bExactWebUrl, SPRequest request)
Microsoft.SharePoint.SPWeb..ctor(SPSite, site, String url)
eo.CreateFakeInstance[T](Members behavior, Constructor constructorFlag, Constructor baseConstructorFlag, Type baseType, Object[] ctorArgs)
eo.Instance[T](Members behavior)
(Points to the SPWeb web line as source of error)
TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodNAme, Object methodParameters, Boolean isInjected)
(Points to line 0 of my test class)


Notice that the constructor for SPWeb is expecting an SPSite object in it's parameters. Try faking an SPSite object before the SPWeb and do a swap on next instance on the SPSite. Maybe this will help.

Cheers.
Jas.
answered by MrClyfar (5.2k points)
0 votes
Hi there.

I've just had time to try out your code - here is my version:

[TestMethod]
        public void TestMethod1()
        {
            SPWeb web = Isolate.Fake.Instance<SPWeb>(Members.ReturnRecursiveFakes);

            Isolate.Swap.NextInstance<SPWeb>().With(web);
            SPList fakedSPList = Isolate.Fake.Instance<SPList>();
            Isolate.WhenCalled(() => web.Lists["List"]).WillReturn(fakedSPList); 
        }


I ran the above code and it worked fine, I did not get an exception. I'm using Typemock Isolator 5.3.5. Try the suggestion I made about SPSite as it might help, but it's odd that my code above does work without issue.

Cheers.
Jas.
answered by MrClyfar (5.2k points)
0 votes
Hi,

First MrClyFar - thanks for helping out!

Chrischan,

First, please make sure you're using the latest version.
Next, usually in SharePoint, you don't create SPWeb. It's created inside SPSite, which you create, and the get through OpenWeb.
What you should do is fake SPSite. When you use the ReturnRecursiveFakes argument (which is the default, by the way), SPWeb will be faked for you free of charge, and so every object in the tree, like SPLists.

You can read more about it here:
http://blog.typemock.com/2009/04/writing-shorter-tests-don-build-tree.html

Let me know if it works for you.
answered by gilz (14.5k points)
0 votes
Hi all:

Thanks for all the help!

I was running my code against Typemock 5.3.5 and it failed at the Isolate.Fake.Instance<SPWeb> line.

However since changing to Typemock 5.3.1, the code worked.

:?
answered by chrischan (1.1k points)
0 votes
Hi Chrischan,

Few things:
1. You don't need a reference to Typemock.CLI.Common.dll
You need only the references to Typemock.dll and Typemokc.ArrangeActAssert.dll

2. Have you tried what Gil suggested? (Fake SPSite and so you don't need to fake SPWeb explicitly)
If you tried this what are the results?

3. Do you have any setup code for this test? Do you get the error when you are running the test alone or only when when you run a whole test suit?
answered by ohad (35.4k points)
...