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
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
=-=-=-=-=-=-=-=
//Arrange
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);
Isolate.Swap.NextInstance<SPListItem>().With(fakeListItem);

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

RegistrationRepository repository = new RegistrationRepository();

SPListItem listItemAdded = repository.AddNewRegistrationEntry(registration, fakeWeb);
//Assert
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 by burtfin (1.1k points)

1 Answer

0 votes
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())
 .WillReturn(fakeSpList);
answered by royo (2k points)
...