一天中的大部分时间,我一直在为这个测试而苦苦挣扎。我在另一篇文章中使用相同的代码解决了其他问题。我的问题是 int pageIndex=1;
如果 pageIndex 设置为“0”,则测试将失败,但它将计算 MembershipUserCollection 中的用户名。如果如果 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);
}
}