我试图执行这个测试方法,但它抛出了一个异常。当我逐步执行代码以尝试隔离异常时,它导致我:
public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
如下所示的测试方法。此处抛出异常并显示以下消息
"Item has already been added. Key in dictionary: 'john' Key being added: 'john'".
我不知道从这里去哪里。有人可以指出我正确的方向吗?另外我的测试方法还在工作中,所以如果你看到任何错误,除非错误直接导致异常被抛出,请忽略它们。
[TestMethod]
public void TestFindUsersByName()// filed a bug in TFS
{
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 = 5;
int pageIndex = 0;
int totalRecords = 5;
MembershipUserCollection coll = prov.FindUsersByName("john", pageIndex, pageSize, out totalRecords);
Assert.AreEqual(1, coll.Count);
List<string> usernames = new List<string>();
foreach (MembershipUser user in coll)
{
usernames.Add(user.UserName);
}
Assert.IsNotNull(usernames);
//Assert.IsTrue(usernames.Contains("john"));
//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);
}
}
// MembershipProvider.FindUsersByName
public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
{
MembershipUserCollection users = new MembershipUserCollection();
try
{
Profile.MembershipMapper memberMapper = new MembershipMapper();
List<Profile.Membership> recs = (List<Profile.Membership>)memberMapper.GetMembershipsByUsername(_memberUtil.GetApplicationId(), usernameToMatch, pageIndex, pageSize, out totalRecords);
foreach (Profile.Membership rec in recs)
{
users.Add(GetUserFromModel(rec, usernameToMatch));
}
}
catch (Exception ex)
{
Exception e = CheckEventLog(ex, "FindUsersByName");
throw e;
}
return users;
}
private MembershipUser GetUserFromModel(Profile.Membership membership, string username)
{
MembershipUser u = new MembershipUser(this.Name,
username,
membership.userId,
membership.email,
"",
"",
true,
false,
this.GetDate(membership.createDate),
this.GetDate(membership.lastLoginDate),
this.GetDate(null),
this.GetDate(membership.lastPasswordChangedDate),
this.GetDate(null));
return u;
}