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

questions about using Justmock

4 Answers 58 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Artiom
Top achievements
Rank 1
Artiom asked on 31 May 2013, 10:00 PM

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 :)

4 Answers, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 05 Jun 2013, 08:13 AM
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.
0
Artiom
Top achievements
Rank 1
answered on 01 Jul 2013, 02:36 PM
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");
0
Accepted
Stefan
Telerik team
answered on 03 Jul 2013, 06:56 AM
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.
0
Artiom
Top achievements
Rank 1
answered on 03 Jul 2013, 09:59 AM
This works now. thanks
Tags
General Discussions
Asked by
Artiom
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
Artiom
Top achievements
Rank 1
Stefan
Telerik team
Share this question
or