0

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

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

2 回答 2

1

您不能在 MembershipUserCollection 中存储多个同名用户。它按用户名编制索引,因此可以防止您为每个名称添加多个 MembershipUser。

问题出在 GetUserFromModel() 方法中。它使用搜索名称作为用户的名称,因为它正在查找 2 个名称中包含“john”的用户,它正在创建 2 个用户名为“john”的用户。它可能应该使用他们的原始用户名,而不是用于搜索的部分用户名。

于 2013-06-07T14:52:04.237 回答
0

如果您的任何成员资格提供程序对象是静态的或共享引用,并且您在多个测试中添加“john”用户,那么您有一个竞争条件,因为测试是并行/多线程执行的。要解决此问题,您必须配置一个测试设置/拆卸,以清除每个测试的数据结构,并按顺序执行它们,或者将这些对象包装在一个自定义对象中,该对象已编程到您可以模拟的接口。

于 2013-06-07T14:37:19.713 回答