Mocking SPList.GetItems(SPQuery) in SharePoint

6 posts, 0 answers
  1. Rich Lasker
    Rich Lasker avatar
    11 posts
    Member since:
    May 2008

    Posted 23 Aug 2010 Link to this post

    It would really be helpful if you put up a few examples of using JustMock against SharePoint. Anyways, I am trying to Mock a call to the GetItems method of the SPList object but I keep getting a NullReference error. Any help would be appreciated. 

    Here is the code I am attempting to mock:

    var collection = web.Lists[listName].GetItems(query); 

     

     

     

    Here is my JustMock code

     

     

     

    private SPWeb mockSPWeb = Mock.Create<SPWeb>(); 
    private SPList mockSPList = Mock.Create<SPList>(); 
    private SPListCollection mockSPListCollection = Mock.Create<SPListCollection>(); 
    private SPListItemCollection mockSPListItemCollection = Mock.Create<SPListItemCollection>(); 
      
    Mock.Arrange(() => mockSPWeb.Lists).Returns(mockSPListCollection); 
    Mock.Arrange(() => mockSPListCollection[Arg.IsAny<string>()]).Returns(mockSPList); 
    Mock.Arrange(() => mockSPList.GetItems(new SPQuery())).Returns(mockSPListItemCollection); 
      
    var testEntityRepository = new TestEntityRepository(new ListItemRepository()); 
    var entity = testEntityRepository.GetASingleTestEntity(mockSPWeb);

    I've tried several different ways to Mock this all with the same error:

    Test method HealthDialog.SharePoint.Foundation.UnitTests.BaseEntityTests.TestBaseEntityRepository threw exception:  System.NullReferenceException: Object reference not set to an instance of an object..

     

     

  2. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 25 Aug 2010 Link to this post

    Hi Rich,

    Thanks for bringing up the issue. One of the issue, i see in the test code is that during  Mock.Arrange of  GetItems a specific instance of SPWeb is passed.

    Mock.Arrange(() => mockSPList.GetItems(new SPQuery())).Returns(mockSPListItemCollection);

    The problem here is that you are telling mokSPList.GetItems to return SpListItemCollection for a particular instance of SPQuery which is not the same as the one passed inside TestEntityRepository. Therefore, the mock finds no match and returns null..

    I tweaked the test using matcher and things are now working just fine.

    [TestMethod]
    public void Should_Assert_FakingOfSpList()
    {
        var spWeb = Mock.Create<SPWeb>();
        var spList = Mock.Create<SPList>();
        var spListCollection = Mock.Create<SPListCollection>();
        var spListItemCollection = Mock.Create<SPListItemCollection>();
        Mock.Arrange(() => spWeb.Lists).Returns(spListCollection);
        Mock.Arrange(() => spListCollection[Arg.AnyString]).Returns(spList);
        Mock.Arrange(() => spList.GetItems(Arg.IsAny<SPQuery>())).Returns(spListItemCollection);
        Assert.AreEqual(spListCollection, spWeb.Lists);
        Assert.AreEqual(spList, spWeb.Lists["myList"]);
        Assert.AreEqual(spListItemCollection, spWeb.Lists["myList"].GetItems(new SPQuery()));
    }

    Additionally, I tried it both with Q2 (2010.1.713) and SP1 (2010.1.810.4). It is working for both the releases. However, there were some issues in the original Q2 version for mocking items from multiple projects. Therefore, I would recommend you to upgrade to the SP1 version, if not already.


    Finally, I attached my test project, there are few other examples added as well. You might like to take a look at them as well.

    Hope this solves your issue.


    Kind regards,
    the Telerik team
     
    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
  3. DevCraft R3 2016 release webinar banner
  4. Rich Lasker
    Rich Lasker avatar
    11 posts
    Member since:
    May 2008

    Posted 25 Aug 2010 Link to this post

    Thanks so much for the response. I no longer get the null reference exception so thanks again. I'll see if it resolves my repository test but when I run your example I get this error:

    Assert.AreEqual failed. Expected:<SPListCollection_Proxy_b20643ca0ccf4f7c8f4ea3e1a04fe1e7 (SPListCollection_Proxy_b20643ca0ccf4f7c8f4ea3e1a04fe1e7)>. Actual:<Microsoft.SharePoint.SPListCollection (Microsoft.SharePoint.SPListCollection)>. 
  5. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 26 Aug 2010 Link to this post

    Hello Rich,

    It’s great to hear that things are working for you. Also, thanks for sending the issue regarding the sample. This time, I attached the tests with solution file (VS 2008) and test results to further mimic the environment. Also, my SharePoint DLL version number is 12.0.0.0 and JustMock version number is 2010.2.810.4. The best is to refresh the references and doing a rebuild.

    However, I found out that the error occurs when your JM profiler is disabled or not configured properly. The reason could be one or more from the following:

    1. The version of VS you are using, don’t' have JustMock addin installed, please make sure that you have checked the "Integrate with visual studio" option during installation.
    2. JustMock addin is installed but you have other profiler dependent tools like TypeMock that is overriding the profiler registration.
    3. Just to confirm the steps, please run the tests from %PROGRAM_FILES%\Telerik\JustMock\Examples and check if the elevated tests are failing.

    Please do not hesitate to ask, if you have further issue.


    Kind Regards,
    the Telerik team

    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
  6. Chris Chapman
    Chris Chapman avatar
    1 posts
    Member since:
    Sep 2005

    Posted 11 Feb 2011 Link to this post

    Hi;

    I just tried running the Mocking SPList class sample from the documentation and received the following error:

    Test method JustMockTestProject1.JustMockTestFixture.Should_Assert_FakingOfSpList threw 
    exception: <BR>System.Reflection.TargetInvocationException: Exception has been 
    thrown by the target of an invocation. ---> System.MethodAccessException: 
      
    Attempt by method 
    'SPListItemCollection_Proxy_b57bc25eee974d09b639d3e459fbcf69..ctor(Telerik.JustMock.DynamicProxy.Abstraction.IInterceptor[])' 
    to access method 'Microsoft.SharePoint.SPListItemCollection..ctor()' 
    failed.<BR><BR>

    I'm running my test using Visual Studio 2010 against SharePoint 2010 DLLs on an isolated workstation running Win7 (no SharePoint installed).  I'm using the 2010.3.1109.7 version of the JustMock assemblies.  Is there something undocumented or not fixed in this release that prevents successful mocking of the SPList class? 
  7. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 11 Feb 2011 Link to this post

    Hi Chris,

    Thanks again for reporting the issue. This exists in post Q2 SP1 builds after few related changes. However, this problem is already fixed. Should you need it urgently, please open a support ticket and I will attach one for you.

    Sorry for the inconvenience and do write us back for any further problems.

    Kind Regards,
    Ricky
    the Telerik team
    Browse the videos here>> to help you get started with JustMock
Back to Top
DevCraft R3 2016 release webinar banner