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
Hi, I am trying to unit test a method which picks the items from the list using CAML query. Now, even if i add mock items in the list, my query still returns 0 results. Anyone has ever used Typemockislotor with CAML queries?

[TestMethod]
[Isolated]
public void TestGetLinksForUser()
{
var fakeSite = Isolate.Fake.Instance<SPSite>(Members.ReturnRecursiveFakes);
Isolate.Swap.NextInstance<SPSite>().With(fakeSite);

var fakeMyFavouritesList = fakeSite.OpenWeb().Lists["My Favourite Links"];
Isolate.WhenCalled(() => fakeMyFavouritesList.Items[0]["Author"]).WillReturn("mockUser");
Isolate.WhenCalled(() => fakeMyFavouritesList.Items[0]["DisplayOrder"]).WillReturn("1");
Isolate.WhenCalled(() => fakeMyFavouritesList.Items[0]["URL"]).WillReturn("dummyURL,1");
Isolate.WhenCalled(() => fakeMyFavouritesList.Items[1]["Author"]).WillReturn("mockUser");
Isolate.WhenCalled(() => fakeMyFavouritesList.Items[1]["DisplayOrder"]).WillReturn("1");
Isolate.WhenCalled(() => fakeMyFavouritesList.Items[1]["URL"]).WillReturn("dummyURL,1");
Isolate.WhenCalled(() => fakeMyFavouritesList.Items[2]["Author"]).WillReturn("mockUser");
Isolate.WhenCalled(() => fakeMyFavouritesList.Items[2]["URL"]).WillReturn("dummyURL,1");
Isolate.WhenCalled(() => fakeMyFavouritesList.Items[2]["DisplayOrder"]).WillReturn("2");
Isolate.WhenCalled(() => fakeMyFavouritesList.Items[3]["Author"]).WillReturn("mockUser");
Isolate.WhenCalled(() => fakeMyFavouritesList.Items[3]["URL"]).WillReturn("dummyURL,1");
Isolate.WhenCalled(() => fakeMyFavouritesList.Items[3]["DisplayOrder"]).WillReturn("3");
Isolate.WhenCalled(() => fakeMyFavouritesList.DefaultViewUrl).WillReturn("mockUrl");
Isolate.WhenCalled(() => fakeSite.OpenWeb().Properties["Max_Personal_Links"]).WillReturn("10");
//fakeSite.OpenWeb().AllProperties.Add("Max_Personal_Links","10");

List<Link0> myFavorites = new List<Link0>();
MyFavouritesUserControl myfav = new MyFavouritesUserControl();
//classUnderTest.Dummymethod();
string manageLinksUrl = MyFavouritesUserControl.GetLinksForUser(myFavorites, "mockUser","mockurl");

Assert.AreEqual(4, myFavorites.Count);
Assert.AreEqual("mockUrl", manageLinksUrl);
}

The method details are:

public static string GetLinksForUser(List<Link0> myFavorites, string userName, string hostUrl)
{
string manageLinksUrl;
//Get handle of umbrella site as favourite links will be global and only managed in umbrella site
using (SPSite hostSite = new SPSite(hostUrl))
{
using (SPWeb hostWeb = hostSite.OpenWeb())
{
if (hostWeb.Properties[MaxPersonalLinks] == null)
{
// Add the properties for storing maximum display limit on the number of links
AddRootWebProperty(hostWeb);
}
// Get the links which have been created by the current user sorted by DisplayOrder column

SPList myFavouritesList = hostWeb.Lists["My Favourite Links"];
SPQuery query = new SPQuery();
query.RowLimit = Convert.ToUInt32(hostWeb.Properties[MaxPersonalLinks]);
query.Query = "<Where><Contains><FieldRef><Value>" +
userName +
"</Value></Contains></Where><OrderBy><FieldRef><FieldRef></OrderBy>";
foreach (SPListItem link in myFavouritesList.GetItems(query))
{
Link0 currentLink = new Link0();
currentLink.URL = link["URL"].ToString();
myFavorites.Add(currentLink);
}
manageLinksUrl = myFavouritesList.DefaultViewUrl;
}
}
return manageLinksUrl;
}
asked by sandhya.bakshi (1.1k points)

1 Answer

0 votes
Hi, and sorry for the late reply.

The problem here is that the underlying SPList object fakeMyFavouritesList will not properly respond to running a CAML query on it, as it is faked. It is not enough to populate it as you did in the test code as it will fake out the GetItems() query.

I will work to produce a suggestion on how to test this properly in this thread.

Doron
Typemock Support
answered by doron (17.2k points)
...