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
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
And here I thought this was going to be easy... :-)

I'm trying to test a simple method that adds an SPListItem to a List:

public void AddItemToList(string siteUrl, string listTitle, string titleValue)
        {
            #region Contract
            if (string.IsNullOrEmpty(siteUrl)) throw new ArgumentException("siteUrl cannot be null or empty");
            if (string.IsNullOrEmpty(listTitle)) throw new ArgumentException("listTitle cannot be null or empty");
            if (null == titleValue) throw new ArgumentNullException("titleValue cannot be null");

            #endregion
            try
            {
                using (SPSite site = new SPSite(siteUrl))
                {
                    SPWeb web = site.OpenWeb();
                    SPList list = web.Lists.TryGetList(listTitle);
                    if (null != list)
                    {
                        SPListItem newItem = list.AddItem();
                        newItem["Title"] = titleValue;
                        newItem.Update();
                    }
                    else
                    {
                        throw new Exception("List does not exist");
                    }
                }
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }

So here's the test code I wrote:

[TestMethod]
        public void AddItemToList_ValidParamaters_UpdateCalled()
        {
            //pass valid paramaters, confirm that the Update method is called
            //Method being tested: AddItemToList(string siteUrl, string listTitle, string titleValue)
            string siteUrl = "http://testdemo.aptillon.com";
            string listTitle = "List1";
            string titleValue = "My New Value";

            //Arrange
            var fakeSite = Isolate.Fake.Instance<SPSite>(Members.ReturnRecursiveFakes);
            Isolate.Swap.NextInstance<SPSite>().With(fakeSite);

            var fakeWeb = Isolate.Fake.Instance<SPWeb>(Members.ReturnRecursiveFakes);
            Isolate.Swap.NextInstance<SPWeb>().With(fakeWeb);

            var fakeList = Isolate.Fake.Instance<SPList>(Members.ReturnRecursiveFakes);
            Isolate.Swap.NextInstance<SPList>().With(fakeList);

            var fakeItem = Isolate.Fake.Instance<SPListItem>(Members.ReturnRecursiveFakes);
           
            Isolate.WhenCalled(() => fakeList.AddItem()).WillReturn(fakeItem);

            //Act
            BizLogic biz = new BizLogic();
            try
            {
                biz.AddItemToList(siteUrl, listTitle, titleValue);
            }
            catch
            {
                Assert.Fail("we should never get here...");
            }
 
            Isolate.Verify.WasCalledWithAnyArguments(() => fakeItem.Update());

}


I can debug my code and it hits the Update method, but my Verify statement fails. At this point, I think I have a few extra fake objects in there from various things I've tried.

Any ideas?

Thanks.
asked by dmann (600 points)

2 Answers

0 votes
Hi,

From first look it looks like it should work. I will investigate the case more closely and get back to you as soon as possible.
answered by ohad (35.4k points)
0 votes
Hi,

The problem here is that you don't need to swap the future instances of SPWeb and SPList.
You can specify the using Isolate.WhenCalled API like this:
[TestMethod]
public void AddItemToList_ValidParamaters_UpdateCalled()
{
    //pass valid paramaters, confirm that the Update method is called
    //Method being tested: AddItemToList(string siteUrl, string listTitle, string titleValue)
    string siteUrl = "http://testdemo.aptillon.com";
    string listTitle = "List1";
    string titleValue = "My New Value";

    //Arrange
    var fakeSite = Isolate.Fake.Instance<SPSite>(Members.ReturnRecursiveFakes);
    Isolate.Swap.NextInstance<SPSite>().With(fakeSite);

    var fakeWeb = Isolate.Fake.Instance<SPWeb>(Members.ReturnRecursiveFakes);
    Isolate.WhenCalled(() => fakeSite.OpenWeb()).WillReturn(fakeWeb);

    var fakeList = Isolate.Fake.Instance<SPList>(Members.ReturnRecursiveFakes);
    var fakeItem = Isolate.Fake.Instance<SPListItem>(Members.ReturnRecursiveFakes);
    
    Isolate.WhenCalled(() => fakeWeb.Lists.TryGetList("")).WillReturn(fakeList);
    Isolate.WhenCalled(() => fakeList.AddItem()).WillReturn(fakeItem);
      
    //Act
    BizLogic biz = new BizLogic();
    try
    {
        biz.AddItemToList(siteUrl, listTitle, titleValue);
    }
    catch
    {
        Assert.Fail("we should never get here...");
    }

    Isolate.Verify.WasCalledWithAnyArguments(() => fakeItem.Update());
} 



Additionally you can write a shorter test :)
In the example below you only fake the top level object in the object model (SPSite)
then you only need to get an instance of SPListItem to do the verification with this line:
SPListItem fakeItem = fakeSite.OpenWeb().Lists.TryGetList(string.Empty).AddItem();

[TestMethod]
public void AddItemToList_ValidParamaters_UpdateCalled2()
{
    string siteUrl = "http://testdemo.aptillon.com";
    string listTitle = "List1";
    string titleValue = "My New Value";

    //Arrange
    var fakeSite = Isolate.Fake.Instance<SPSite>(Members.ReturnRecursiveFakes);
    Isolate.Swap.NextInstance<SPSite>().With(fakeSite);
    SPListItem fakeItem = fakeSite.OpenWeb().Lists.TryGetList(string.Empty).AddItem();
    
    //Act
    BizLogic biz = new BizLogic();
    try
    {
        biz.AddItemToList(siteUrl, listTitle, titleValue);
    }
    catch
    {
        Assert.Fail("we should never get here...");
    }

    Isolate.Verify.WasCalledWithAnyArguments(() => fakeItem.Update());
} 


Please let me know if it helps.
answered by ohad (35.4k points)
...