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

Mocking SPListItemCollection Error JustMock

1 Answer 19 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Top achievements
Rank 1
Gary asked on 24 Feb 2011, 12:45 AM
Hi all,

I have been researching what Mocking framework to use that will allow me to mock SharePoint classes, and as a result I am evaluating Telerik JustMock.

I am using SharePoint 2007 and Visual Studio 2008

I am attemting to Mock a SPList and a SPListItemCollection (see test method below) and I am getting the following error
"System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. --->  
System.MethodAccessException: Microsoft.SharePoint.SPListItemCollection..ctor().

 when attempting to execute the bold line below;

        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()));

I was wondering if the error was in the trial version and not in the full blown version but as you can imagine, I am not keen to purchase JustMock if I am going to come across the same issue.

I would appreciate any help on this matter.

Many thanks

Gary Hoggart
Senior IT Specialist

1 Answer, 1 is accepted

Sort by
Telerik team
answered on 24 Feb 2011, 08:19 AM
Hi Gary,

Thanks again for making the post. Unfortunately it is occurring due to some changes in the Q3 build. Good news is that we already identified and fixed the problem.  In that regard, please open up a support ticket (referencing this post) and we will attach an internal build for you.

However, the build will be available in the coming Q1 release. Please let us know if the above mentioned solution works for you as well.

P.S. Opening up a support ticket is just a policy for providing internal builds to fast address customer exceptions.

Kind Regards,
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
General Discussions
Asked by
Top achievements
Rank 1
Answers by
Telerik team
Share this question