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