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
Hello. I have the following SharePoint code, that i want to fake.
SPList list = web.Lists[listId];
                SPListItem item = list.Items.Add();
                item["Title"] = listItemTitle;
                item.Update();


item["Title"] is an indexer:
// Microsoft.SharePoint.SPListItem
public override object this[string fieldName]
{
   get
   {
      return this.GetValue(fieldName);
   }
   set
   {
      this.SetValue(fieldName, value, !this.HasExternalDataSource);
   }
}

Question: How can i fake/ intercept the call to SetValue to verify that this method was called with the correct parameter?
I will do interactions testing.

Here is my Isolator code
SPWeb fakeWeb = Isolate.Fake.Instance<SPWeb>(Members.ReturnRecursiveFakes);
            Isolate.Swap.NextInstance<SPWeb>().With(fakeWeb);
            SPListItem fakeSpListItem = fakeWeb.Lists[Guid.Empty].Items.Add();
            Isolate.WhenCalled(() => fakeSpListItem["Title"]).WillReturn("test");
//Assert
Isolate.Verify.NonPublic.WasCalled(fakeSpListItem, "SetValue");

But get an error message:
ypeMock.VerifyException:
TypeMock Verification: Method Microsoft.SharePoint.SPListItem.SetValue() was expected but was not called
Result StackTrace:
asked by atomic (6.3k points)

5 Answers

0 votes
Hi Artiom,

Please try using this fake Item:


var fakeItem = Isolate.Fake.Instance<SPListItem>();
Isolate.Verify.WasCalledWithExactArguments(() => fakeItem["Title"] = "Artiom");



Hope I helped
answered by NofarC (4k points)
0 votes
I can't try this right now but I will try this tomorrow.
But will I work without these line of code?
Isolate.Swap.NextInstance<SPListItem>().With(fakeItem);


I'm new at Isolator and thought that with Isolate.Fake.Instance I create a fake object and with Isolate.Swap.NextInstance I change the call to the original object with my fake object.
answered by atomic (6.3k points)
0 votes
Hi,

Actually looking at your code again we should have been clearer.

Fake.NextInstance replaces the Swap for future objects (objects created by the code-under-test, using "new". So you don't need the swap. But SPWeb is not a future object. You usually get it from SPSite which is created in the code-under-test.
You can use Fake.NextInstance on SPSite and then get the SPWeb and work on it:

SPSite fakeSite = Isolate.Fake.NextInstance<SPSite>();
SPWeb fakeWeb = fakeSite.OpenWeb();
SPListItem fakeSpListItem = fakeWeb.Lists[Guid.Empty].Items.Add();Isolate.WhenCalled(() => fakeSpListItem["Title"]).WillReturn("test");


Let me know how it works for you.

Gil Zilberfeld
answered by gilz (14.5k points)
0 votes
Thanks NofarC and Gilz!

Now it works.
My Testcode is:
//Arrange
SharePointListCreator spListCreator = new SharePointListCreator();
SPWeb fakeWeb = Isolate.Fake.Instance<SPWeb>();
            SPListItem fakeSpListItem = fakeWeb.Lists[Guid.Empty].Items.Add();

            //Act
            spListCreator.AddSplistItemToList(fakeWeb, listId, "Test Item");

            //Assert
           Isolate.Verify.WasCalledWithExactArguments(() => fakeSpListItem["Title"] = "Test Item");

Just to be clear:
Isolate.Fake.Instance create a fake object
Isolate.Fake.NextInstance or AllInstances create a fake object and replace the next or allinstances. So I don't have to use Swap.NextInstance anymore?
answered by atomic (6.3k points)
0 votes
That is correct.
The Swap API is reserved for actually swapping objects, which is needed in rare situations.

Glad it works.

Gil
answered by gilz (14.5k points)
...