Hi,
The problem here is that you don't need to swap the future instances of SPWeb and SPList.
You can specify the using Isolate.WhenCalled API like this:
[TestMethod]
public void AddItemToList_ValidParamaters_UpdateCalled()
{
//pass valid paramaters, confirm that the Update method is called
//Method being tested: AddItemToList(string siteUrl, string listTitle, string titleValue)
string siteUrl = "http://testdemo.aptillon.com";
string listTitle = "List1";
string titleValue = "My New Value";
//Arrange
var fakeSite = Isolate.Fake.Instance<SPSite>(Members.ReturnRecursiveFakes);
Isolate.Swap.NextInstance<SPSite>().With(fakeSite);
var fakeWeb = Isolate.Fake.Instance<SPWeb>(Members.ReturnRecursiveFakes);
Isolate.WhenCalled(() => fakeSite.OpenWeb()).WillReturn(fakeWeb);
var fakeList = Isolate.Fake.Instance<SPList>(Members.ReturnRecursiveFakes);
var fakeItem = Isolate.Fake.Instance<SPListItem>(Members.ReturnRecursiveFakes);
Isolate.WhenCalled(() => fakeWeb.Lists.TryGetList("")).WillReturn(fakeList);
Isolate.WhenCalled(() => fakeList.AddItem()).WillReturn(fakeItem);
//Act
BizLogic biz = new BizLogic();
try
{
biz.AddItemToList(siteUrl, listTitle, titleValue);
}
catch
{
Assert.Fail("we should never get here...");
}
Isolate.Verify.WasCalledWithAnyArguments(() => fakeItem.Update());
}
Additionally you can write a shorter test :)
In the example below you only fake the top level object in the object model (SPSite)
then you only need to get an instance of SPListItem to do the verification with this line:
SPListItem fakeItem = fakeSite.OpenWeb().Lists.TryGetList(string.Empty).AddItem();
[TestMethod]
public void AddItemToList_ValidParamaters_UpdateCalled2()
{
string siteUrl = "http://testdemo.aptillon.com";
string listTitle = "List1";
string titleValue = "My New Value";
//Arrange
var fakeSite = Isolate.Fake.Instance<SPSite>(Members.ReturnRecursiveFakes);
Isolate.Swap.NextInstance<SPSite>().With(fakeSite);
SPListItem fakeItem = fakeSite.OpenWeb().Lists.TryGetList(string.Empty).AddItem();
//Act
BizLogic biz = new BizLogic();
try
{
biz.AddItemToList(siteUrl, listTitle, titleValue);
}
catch
{
Assert.Fail("we should never get here...");
}
Isolate.Verify.WasCalledWithAnyArguments(() => fakeItem.Update());
}
Please let me know if it helps.