Welcome to Typemock Answers. Here you can ask and receive answers from other community members. And if you liked or disliked an answer or thread: react with an up- or downvote Enjoy!

Swap not working as expected

0 votes
I know I must be doing something wrong. This is a very simple test, but I'm having issues get the returned SPListItem Guid to be populated with the expected value. The UniqueId property for the returned SPListItem is always {000000...

Test code
Guid testGuid = Guid.NewGuid();

SPListItem fakeListItem = Isolate.Fake.Instance<SPListItem>(Members.ReturnRecursiveFakes);
SPWeb fakeWeb = Isolate.Fake.Instance<SPWeb>(Members.ReturnRecursiveFakes);
Isolate.WhenCalled(() => fakeListItem.UniqueId).WillReturn(testGuid);

Registration registration = new Registration();
registration.Title = "Title";
registration.Status = "Status";

RegistrationRepository repository = new RegistrationRepository();

SPListItem listItemAdded = repository.AddNewRegistrationEntry(registration, fakeWeb);
Assert.AreEqual(testGuid, listItemAdded.UniqueId);

Code Under Test
public class RegistrationRepository
    private readonly string listName = "RegistrationRepository";

    public SPListItem AddNewRegistrationEntry(Registration registration, SPWeb web)
        return AddListItem(registration, web);

    private SPListItem AddListItem(Registration registration, SPWeb web)
        if (web == null) throw new ArgumentNullException("web", "SPWeb instance cannot be null");

        SPListItem item = web.Lists[listName].Items.Add();

        item["Title"] = registration.Title;
        item["Status"] = registration.Status;

        return item;

I'm very new TypeMock, so I realize that I'm probably missing something obvious. Any help is appreciated.
asked Jul 2, 2009 by burtfin (140 points)

1 Answer

+1 vote
you have a Fake Web that is being called with [items].Add() which already returns a fake SPList (just not with the GUID you need).
there is no "new" happening since it's all fake already.

instead you can just have the call to Items.Add() return your fake item:

Isolate.WhenCalled(()=> web.Lists[listName].Items.Add())
answered Jul 3, 2009 by royo (2,170 points)