测试期间抛出异常,导致Item已经被添加到字典中

本文关键字:添加 字典 Item 抛出异常 导致 测试 | 更新日期: 2023-09-27 18:17:28

我试图执行这个测试方法,但它抛出了一个异常。当我逐步通过代码试图隔离异常时,它导致我:

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

测试期间抛出异常,导致Item已经被添加到字典中

不能在MembershipUserCollection中存储具有相同名称的多个用户。它是按用户名索引的,因此可以防止为每个用户名添加多个MembershipUsers。

问题在GetUserFromModel()方法中。它使用搜索名作为用户名,因为它正在寻找2个用户,他们的名字中有"john",它正在创建2个用户名为"john"的用户。它可能应该使用原始用户名,而不是用于搜索的部分用户名。

如果任何成员资格提供程序对象是静态的或共享引用,并且在多个测试中添加"john"用户,那么在并行/多线程中执行测试时就会出现竞争条件。为了解决这个问题,您必须配置一个测试设置/拆除,清除每个测试的数据结构,并依次执行它们,或者将这些对象包装在一个定制的对象中,编程成一个您可以模拟的接口。