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 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 :)
asked by atomic (1.3k points)

4 Answers

0 votes
Hi Artiom,

Regarding question 1:
You should test that the method site.AllWebs.Add().ID was called with the right parameters, and you can use WasCalledWithExactArguments as explained here.


Regarding question 2:
You don't need to create/ initialize the parameters, you can use empty strings instead.

Please let me know if it helps,
answered by NofarC (4k points)
0 votes
Hello NofarC and thnks for your reply!

@1 I allready thought about to verify the parameters but I thing if you have the possibility (non void method) it is better to test the api contract. Because otherwise if the internal method logic changes, my test will fail. Correct me if I think in the wrong way.

@2. Why do I need these parameters, even if I use empty strings. In MS Fakes a can use these parameters as value for example to my fake object. It is clear, that a fake a method that originaly take these parameters, but what can I do with these parameters in Isolator?

Ps. How do you know my name? :) Gil it's you?
answered by atomic (1.3k points)
0 votes
Hi Artiom

@1 - When you have the option, you should use state-based testing. This means asserting on return or exposed values. If you don't have this option, you'll need to go with interaction-based testing, meaning to verify method calls (usually on dependencies).

@2 - Basically, it's just syntax issues (also for MS). With Isolator, when using WhenCalled, the default is to apply the behavior regardless of the arguments, but you need to specify them to make sure the correct overload is called. The same syntax is applied when you want to apply conditional behavior (i.e. apply the behavior only when the parameter values at run-time match the set values). Read more here about controlling behavior here:[url]http://docs.typemock.com/Isolator/##Ref.chm/Documentation/SettingBehaviorAAA.html[/url]

I hope this helps.

Now it is Gil, by the way :)

Gil Zilberfeld
answered by gilz (14.5k points)
0 votes
And here again. :D
Thanks you both
answered by atomic (1.3k points)
...