Fake list and list item, query

10/10/2012 12:03

///

/// VideoWidth is not set, taken from config list, list exists, item exists and has value.

/// Should return item value.

///

[TestMethod()]

public void VideoWidthIsNotSetListItemHasValue()

{

ExternalVideoWP target = new ExternalVideoWP();

target.VideoWidth = string.Empty;

 

//setting value of private field, target is class where 'configListItem' is defined

var fakeItem = Isolate.Fake.Instance<SPListItem>();

ObjectState.SetField(target, "configListItem", fakeItem);

Isolate.WhenCalled(() => fakeItem["DefaultWidth"]).WillReturn("widthValue");

 

var fakeList = Isolate.Fake.Instance<SPList>();

Isolate.WhenCalled(() => SPContext.Current.Web.Lists.TryGetList("Configuration List")).WillRetur(fakeList);

 

Assert.AreEqual("widthValue", target.VideoWidth);

}

 

//set item field values

SPListItem item = Isolate.Fake.Instance<SPListItem>(Members.ReturnRecursiveFakes);

item["DefaultWidth"] = 100;

item["DefaultHight"] = 200;

item["DefaultColor"] = "red";

 

SPList fakeList = Isolate.Fake.Instance<SPList>();

Isolate.WhenCalled(() => SPContext.Current.Web.Lists.TryGetList("Configuration List")).WillRetur(fakeList);

 

//fake executing query

SPQuery fakeQuery = Isolate.Fake.Instance<SPQuery>();

SPListItemCollection itemCollection = Isolate.Fake.Instance<SPListItemCollection>();

Isolate.WhenCalled(() => itemCollection.Count).WillReturn(1);

Isolate.WhenCalled(() => itemCollection[0]).WillReturn(item);

Isolate.WhenCalled(() => fakeList.GetItems(fakeQuery)).WillReturn(itemCollection);