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
Hi
I've looked through some posts here regarding mocking SPLists, but I couldn't find quite what I was looking for.

I have this simple method I want to test with Typemock:

public SPList GetListByListname(SPWeb web, string listName)
        {
            foreach (SPList list in web.Lists)
                if (list.Title == listName)
                    return list;
            return null;
        }


I've tried a lot of different things to be able to write a simple unit test to this method, but my test always returns null.
Probably because I have not figured out how to mock the list properly.

 [TestMethod]
        public void GetListByListname_ListExist_ReturnTrue()
        {
            // Arrange
            var fakeWeb = Isolate.Fake.Instance<SPWeb>();
            fakeWeb.Lists.Add("testList", "", SPListTemplateType.GenericList);
            fakeWeb.Lists.Add("testList2", "", SPListTemplateType.GenericList);
            Isolate.Swap.NextInstance<SPWeb>().With(fakeWeb);
           

            // Act
            ProductOfferProvider provider = new ProductOfferProvider();
            var newSPList = provider.GetListByListname(fakeWeb, "testList");
 
            // Assert
            Assert.IsNotNull(newSPList);
        }


I've also tried to fake the list (e.g. fakeList = Isolate.Fake.Instance<SPList>(); ), but I cannot figure out how to connect this object to my fakeWeb.

Any help is appreciated

Thanks
asked by hah (680 points)

2 Answers

0 votes
Hi,

What happens is that because your SPWeb is faked, when you add items to it the Add function is ignored. What you need to do to fake the contents of the spWeb.Lists collection. This code will populate the Lists collection with fake SPList objects and give them appropriate names:
var fakeWeb = Isolate.Fake.Instance<SPWeb>();
Isolate.WhenCalled(() => fakeWeb.Lists[0].Name).WillReturn("TestList");
Isolate.WhenCalled(() => fakeWeb.Lists[1].Name).WillReturn("TestList2");


You can now pass the fake SPWeb into the method under test (the Swap.NextInstance() call in your test is unnecessary - use that when you need to swap a dependency created by a call to 'new' in the code under test).

Please let me know if this helps,
Doron
Typemock Support
answered by doron (17.2k points)
0 votes
Ah, thank you so much!
It was even easier than I thought.

I did actually try something similar yesterday, but I messed it up.

If anyone else wonders, here is the working test:

 [TestMethod]
        public void GetListByListname_ListExist_ReturnSPList()
        {
            // Arrange
            var fakeWeb = Isolate.Fake.Instance<SPWeb>();
            Isolate.WhenCalled(() => fakeWeb.Lists[0].Title).WillReturn("TestList1");
            Isolate.WhenCalled(() => fakeWeb.Lists[1].Title).WillReturn("TestList2");
            Isolate.WhenCalled(() => fakeWeb.Lists[2].Title).WillReturn("TestList3");

            // Act
            MyProvider provider = new MyProvider();
            var newSPList = provider.GetListByListname(fakeWeb, "TestList2");
 
            // Assert
            Assert.IsNotNull(newSPList);
        }
answered by hah (680 points)
...