questions about using Justmock

5 posts, 1 answers
  1. Artiom
    Artiom avatar
    11 posts
    Member since:
    May 2013

    Posted 31 May 2013 Link to this post

    Hello, I have some questions about unit testing with Justmock.

    I have this very simple method (Share Point code):

    Code:

    public Guid CreateNewSite(String siteUrl, string newSite)
           {
               if (!(string.IsNullOrEmpty(newSite) || string.IsNullOrEmpty(siteUrl)))
               {
                   using (SPSite site = new SPSite(siteUrl))
                   {
                       if (!site.AllWebs.Names.Contains(newSite))
                       {
                           return site.AllWebs.Add(newSite, newSite, "New Website", 1033, "STS#1", true, false).ID;
                       }
                   }
               }
               return Guid.Empty;
           }

    1 question.
     Is it ok, that I test the methods return (Guid). I mean I test the contract of the method "CreateNewSite". If the site that should be created not exist, and the parameters are not empty, the new site will be created and a Guid != Guid.Empty will be returned. Otherwise return Guid.Emtpy.

    Or should I test, that the method site.AllWebs.Add(newSite, newSite, "New Website", 1033, "STS#1", true, false).ID; was called with the right parameters?

    2 question.
    How can I unit test the void methods (interaction testing) with Justmock.
    For example this code:
    public void AddSplistItemToList(SPWeb web, Guid listId, String listItemTitle)
            {
                if (!(listId == Guid.Empty || listId == Guid.Empty || string.IsNullOrEmpty(listItemTitle)))
                {
                    SPList list = web.Lists[listId];
                    SPListItem item = list.Items.Add();
                    item["Title"] = listItemTitle;
                    item.Update();
                }
            }
    Because the  AddSplistItemToList is void, I want to test, that item["Title"] would set correctly (value from listItemTitle parameter). How I can test and fake this with Justmock?

    I hope my questions are not too confused :)
    
    
  2. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 05 Jun 2013 Link to this post

    Hello Atomic,

    Thank you for the questions.

    About your first question, I can say that you should cover everything your method is supposed to do. For example:
    • Your method is called "CreateNewSite" and it returns a GUID. In this case it is recommended to test if the returned GUID is correct, according to the arguments passed and further it is also good to check if the inner called method "site.AllWebs.Add" has been called with the correct arguments. However, which one is better or more suitable in your case will depend on what is the exact purpose of the "CreateNewSite" and what side effects you want to cover.

    About your second question, I will say testing void methods can be tricky. For further information, I suggest checking this thread. However, to test if item["Title"] has been set correctly you may need to extract the SPListItem item as a class property, like this:
    public SPListItem item;
     
    public void AddSplistItemToList(SPWeb web, Guid listId, String listItemTitle)
    {
        if (!(listId == Guid.Empty || listId == Guid.Empty || string.IsNullOrEmpty(listItemTitle)))
        {
            SPList list = web.Lists[listId];
            item = list.Items.Add();
            item["Title"] = listItemTitle;
            item.Update();
        }
    }
    Having it like that, you will be able to assert against its value. In other words, you will be able to do something similar to this:
    // Arrange
    var someSPWeb = Mock.Create<SPWeb>();
    var anyGUID = new Guid("123");
    var anyListItemTitle = "Test";
     
    var myClass = new MyClass();
     
    // Act
    myClass.AddSplistItemToList(someSPWeb, anyGUID, anyListItemTitle);
     
    // Assert
    Assert.AreEqual(myClass.item["Title"], "Test");

    I hope my answers help.

    Regards,
    Kaloyan
    Telerik
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
  3. DevCraft R3 2016 release webinar banner
  4. Artiom
    Artiom avatar
    11 posts
    Member since:
    May 2013

    Posted 01 Jul 2013 Link to this post

    Thanks for your reply.

    But how I can do this, without the public property?

    With Typemock I can do it that way:
    SPWeb fakeWeb = Isolate.Fake.Instance<SPWeb>();
                SPListItem fakeListItem = fakeWeb.Lists[Guid.Empty].Items.Add();
    Isolate.Verify.WasCalledWithExactArguments(() => fakeListItem["Title"] = "Test Item");
  5. Answer
    Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 03 Jul 2013 Link to this post

    Hello Artiom,

    Here's how you could do it with JustMock:

    var guid = Guid.NewGuid();
     
    // Arrange
    var web = Mock.Create<SPWeb>();
    var item = Mock.Create<SPListItem>();
    Mock.Arrange(() => web.Lists[guid].Items.Add()).Returns(item);
     
    // Act
    AddSplistItemToList(web, guid, "Roadside picnic");
     
    // Assert
    Mock.AssertSet(() => item["Title"] = "Roadside picnic", Occurs.Once());
    Mock.Assert(() => item.Update(), Occurs.Once());


    Regards,
    Stefan
    Telerik
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
  6. Artiom
    Artiom avatar
    11 posts
    Member since:
    May 2013

    Posted 03 Jul 2013 Link to this post

    This works now. thanks
Back to Top
DevCraft R3 2016 release webinar banner