测试asp.net会员提供商有2个问题

时间:2013-05-22 19:22:01

标签: c# unit-testing asp.net-membership

我创建了一个单元测试来按名称搜索用户。我有两个问题我无法弄清楚。

  1. Assert.IsTrue(coll.Count == 2)不会对用户进行计数。应该有2个用户,它只有1个。

  2. Assert.IsTrue(usernames.Contains("testUser"));找不到部分用户名字符串。有人能告诉我我做错了吗?

  3. 这是我的代码:

    [TestMethod]
    public void TestFindUsersByName()
    {
        //set test to crate user 
        AsaMembershipProvider prov = this.GetMembershipProvider();
        MembershipCreateStatus status;
        //creates users
        MembershipUser user1 = prov.CreateUser("testUser1", "12345", "test.User1@abc.com", "", "", true, null, out status);
        MembershipUser user2 = prov.CreateUser("testUser2", "12345", "test.User2@abc.com", "", "", true, null, out status);
    
        //gets users
        user1 = prov.GetUser("testUser1", false); //not checking if user is online. Argument should be false. Not yet implemented 
        user2 = prov.GetUser("testUser2", false);
    
        int pageSize = 5;
        int pageIndex = 0;
        int totalRecords = 2;
    
        MembershipUserCollection coll = prov.FindUsersByName("testUser1", pageIndex, pageSize, out totalRecords);
    
        Assert.IsTrue(coll.Count == 2);
    
        List<string> usernames = new List<string>();
    
        foreach (MembershipUser user in coll)
        {
            usernames.Add(user.UserName);
        }
    
        Assert.AreNotEqual(usernames, null);
        Assert.IsTrue(usernames.Contains("testUser"));
    
        //Deletes Users
        prov.DeleteUser("testUser1", true);
        prov.DeleteUser("testUser2", true);
    
        //Tries to get users again
        user1 = prov.GetUser("testUser1", false);
        user2 = prov.GetUser("testUser2", false);
    
        //test that no users are returned
        Assert.AreEqual(null, user1);
        Assert.AreEqual(null, user2);
    
        }
    

2 个答案:

答案 0 :(得分:0)

  1. prov.FindUsersByName(“testUser1”... - 应该是“testUser”?
  2. usernames.ToArray()== string [] {“testUser1”,“testUser2”} - 它不包含“testUser”

答案 1 :(得分:0)

问题1:  您正在两个用户的集合中寻找“testUser1”。你得到1而不是2,因为只有一个名为“testUser1”的用户

问题2:   您正在搜索“testUser”而不是部分。