Question

I had a question a couple of days ago about asking "What is the simplest way to count the number of usernames that starts with "john" my test finds in the MembershipUserCollection? (the test should find 2 usernames, johnUser1 and johnUser2)

I receive the following comment form Garrison Neely

"Two points: 1) FindUsersByName is a method in the .NET framework. I don't think a unit test testing the functionality of a non-custom method is necessary 2) if you really must test this method, add the users like you are doing, call GetAllUsers, then loop through or use LINQ to search for all user names that .StartsWith("john")."

How do I call GetAllUsers, and then use LINQ to search for all user names that .StartsWith("john")?

[TestMethod]
public void TestFindUsersByName()
{
    try
    {

        //set test to create user 
        MembershipProvider prov = this.GetMembershipProvider();
        MembershipCreateStatus status;
        //creates users
        MembershipUser user1 = prov.CreateUser("johnUser1", "12345", "johnUser1@asc.edu", "", "", true, null, out status);
        MembershipUser user2 = prov.CreateUser("johnUser2", "12345", "johnUser2@asc.edu", "", "", true, null, out status);

        //gets users
        user1 = prov.GetUser("johnUser1", false); //not checking if user is online. Argument should be false. Not yet implemented 
        user2 = prov.GetUser("johnUser2", false);

        int pageSize = 1;
        int pageIndex=1;
        int totalRecords = 5;

        MembershipUserCollection coll = prov.FindUsersByName("john",pageIndex, pageSize, out totalRecords);


        List<string> UserName = new List<string>();

        foreach (MembershipUser user in coll)
        {

            UserName.Add(user.UserName);
        }
        Assert.AreEqual(2, coll.Count);
        Assert.IsNotNull(UserName);


        Assert.IsTrue(UserName.Contains("johnUser1"));


        //Deletes Users
        prov.DeleteUser("johnUser1", true);
        prov.DeleteUser("johnUser2", true);


        //Tries to get users again
        user1 = prov.GetUser("johnUser1", false);
        user2 = prov.GetUser("johnUser2", false);


        //test that no users are returned
        Assert.AreEqual(null, user1);
        Assert.AreEqual(null, user2);

        }

    catch (Exception ex)
    {
        LogMessage(ex);
        Assert.Fail(ex.Message);
    }
}
Was it helpful?

Solution

var allUsers = coll.GetAllUsers();

I'm not sure if LINQ will be implemented fully for this, so here's the LINQ I'd try:

var johns = allUsers.Where(user => user.UserName.ToLower().StartsWith("john");

Then you can check johns.Count(), or iterate through it for whatever reason.

If that code doesn't work, try

var johns=new List<MembershipUser>();
foreach(var membershipUser in allUsers)
{
    if(membershipUser.UserName.ToLower().StartsWith("john"))
    {
        johns.Add(membershipUser);
    }
}

Then johns will be filled with the applicable john data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top