pageIndex = 1不会计算MembershipUserCollection中的用户名

时间:2013-06-07 20:25:25

标签: c# collections count asp.net-membership

在一天的大部分时间里,我一直在努力进行这项测试。我有其他问题已在另一篇文章中使用相同的代码进行了解决。我的问题是int pageIndex = 1;

如果pageIndex设置为“0”,则测试将失败,但它将计算MembershipUserCollection中的用户名。如果If pageIndex设置为任何其他数字,则测试将通过,但不允许它计算集合中的用户名。

解决此问题的最简单方法是什么,以便我的测试将计算它在MembershipUserCollection中找到的用户名数量? (测试应该找到2个用户名,johnUser1和johnUser2)

    [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);
        }
    }

0 个答案:

没有答案