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,
We are facing issues in writing unit test cases with sharepoint.
Code looks like below :

public class testcalss

{
string membersEmail;
public string CheckEmail()
{
SPWeb mailWeb = SPContext.Current.Web;
SPGroupCollection siteGrps = mailWeb.SiteGroups;
membersEmail = "";
foreach (SPGroup group in siteGrps)
{
if (group.Name.Contains("Members"))
{
SPUserCollection siteMembers = group.Users;
foreach (SPUser user in siteMembers)
{
membersEmail = membersEmail + user.Email + "; ";
}
}
mailWeb.Close();
}
return membersEmail;
}
}


Any one please help me how to use typemock in writing unit test case for the above code.

Thanks in advance,
Shane
asked by Shane (1.2k points)

7 Answers

0 votes
Hi,

what you want to do is to mock the calls to:
SPContext.Current.Web and
mailWeb.SiteGroups

and freturn from the second call a fake value which will probably contain a single group "members" which has a single user with some email address that will be alter verifed.
the test will go something like this:
[Test]
public void CheckEmail_ReturnMembersEmail()
{
  //gere you will need to construct the fake group 
  //so it will contain hard coded data that will be later verifeid.
  // i.e. a singlle member group with a single user with
  // the "tom@gamil.com" address
  SPGroupCollection fakeGroups = new SPGroupCollection();
  ...
  ...
  //recording phase
  using (RecorderExpectations rec = new RecorderExpectations())
  {
    SPWeb mailWeb = SPContext.Current.Web; 
    rec.ReturnDefaultImplementation();
    SPGroupCollection siteGrps = mailWeb.SiteGroups;
    Rec.Return(fakeGroups);
  }

  //test execution
  testClass target  = new testClass()
  string actual = target.CheckEmail();
  
  //verification
  Assert.AreEqual("tom@gmail.com",actual);
  MockManager.Verify();
}


Of course more tests should be constructed to check the validity of the CheckEmail() method. probably test that will check for errors, what happens when there are no groups defined, when no users defines in member group and so on.

hope this help.
answered by lior (13.2k points)
0 votes
Hi lior,
Thanks for the information.
I am new to this scenario, and have a couple of doubts to be cleared.

1. How can I add my own values to the Group Collection?
2. How it will take the current Web ?

Thanks in advance ,
Shane
answered by Shane (1.2k points)
0 votes
1. to add values to the group collection you will need to use the group collection API. I'm guessing using the Add method will do the trick

2. don't worry about the current web the call
SPContext.Current.Web is being mocked and since the actual value returned is also mocked it does not really matter what it contains.
answered by lior (13.2k points)
0 votes
Hi lior,

Thanks once again for your relpy. Some more clarifications I need

1.Can you explain without site how can we add users to group collection ? If possible please post a sample code .

2.Is reflective mock possible for this scenario (for the code i posted above) or natural mock is the only possible way?

3.Do we need to run the test script in the sharepoint installed machine ?


Once again Thanks in Advance,
Shane
answered by Shane (1.2k points)
0 votes
1) which version are you using?

2) reflective mocks can be used to do alomst everything that natural mocks can.

3) the whole point of using mocks is to be able to run the test on the development machine in isolation without being dependant on the sharepoint installed machine.


Note that you can click the following link to learn more on How to use sharepoint with Isolator!
answered by lior (13.2k points)
0 votes
Hi lior,

I'm using TypeMock.Net version 4.1.0.0
I'm unable create fake Group collection.Without creating group collection how is it possible to test the above posted method?
Please Help

Thanks,
Shane
answered by Shane (1.2k points)
0 votes
Hi,

I dont see a way to test your code without mocking the GroupCollection.
However looking at the SharePoint API I can see your problem

The trick is to use the MockManager.MockObject API

Ill try posting an exact example on how to do that.
In the meantime please read the following article, I think that in there you will find what you are looking for.
http://dotnetforum.dk/blogs/mac/archive/2007/12/21/first-steps-with-typemock-and-the-sharepoint-api.aspx
answered by lior (13.2k points)
...