Hello, I have some questions about unit testing.
I have this very simple method (Share Point 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;
}
My Isolator test code is:
public void CreateNewSite_SiteNotExists_SiteGuidNotEmptySiteIsCreated()
{
//Arrange
string webUrl = null;
string webTitle = null;
string webDescription = null;
uint nLCID = 0;
SPWebTemplate webTemplate = null;
bool useUniquePermissions = false;
bool bConvertIfThere = false;
SharePointListCreator spListCreator = new SharePointListCreator();
Guid webGuid = new Guid("371FCC0B-51A8-4DDC-884F-FD1A48CBCF52");
var fakeSite = Isolate.Fake.NextInstance<SPSite>();
Isolate.Swap.NextInstance<SPSite>().With(fakeSite);
Isolate.WhenCalled(() => fakeSite.AllWebs.Names).WillReturn(new string[] { "nonExistsWeb" });
Isolate.WhenCalled(() => fakeSite.AllWebs.Add(webUrl, webTitle, webDescription, nLCID, webTemplate, useUniquePermissions, bConvertIfThere).ID).WillReturn(webGuid);
//Act
Guid actual = spListCreator.CreateNewSite("http://local", "testWeb");
//Assert
Assert.AreEqual(webGuid, actual);
}
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 createt 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.
Why I must create/ initialize the parameters for this method?:
Isolate.WhenCalled(() => fakeSite.AllWebs.Add(webUrl, webTitle, webDescription, nLCID, webTemplate, useUniquePermissions, bConvertIfThere).ID).WillReturn(webGuid);
In Ms Fakes this example looks like this:
Guid webGuid = new Guid("371FCC0B-51A8-4DDC-884F-FD1A48CBCF52");
ShimSPSite.ConstructorString = (site, url) => { };
ShimSPSite.AllInstances.AllWebsGet = (site) => new ShimSPWebCollection()
{
AddStringStringStringUInt32StringBooleanBoolean =
(webUrl, webTitle, webDescription, nLCID, webTemplate, useUniquePermissions, bConvertIfThere) => new ShimSPWeb()
{
IDGet = () => webGuid
},
NamesGet = () => new string[] { "nonExistsWeb" }
};
These parameters (
webUrl, webTitle, webDescription, nLCID, webTemplate, useUniquePermissions, bConvertIfThere) are never declared or initialized. I think Fakes do this for me. But when the method AddStringStringStringUInt32StringBooleanBoolean is called, I can use these parameters for other things, like validate that this method was called with exact parameters I call the main method.
How can I do this in Isolator?
How can I unit test the void methods (interaction testing)?
I hope my post is not very confusing :)