1

嗨,我正在编写集成测试。

我的方法是

public IList<string> GetUsersRecursively(string groupName)
    {
        using (var context = GetPrincipalContext())
        {
            using (var group = GroupPrincipal.FindByIdentity(context, groupName))
            {
                using (var users = group.GetMembers(true))
                {
                    return (from user in users 
                            orderby user.SamAccountName
                            select user.SamAccountName
                            ).ToList();
                }; // recursively enumerate
            }
        }
        //          return results;
    }

我写的测试是

    [Test]
    public void GetUsersRecursively()
    {
        // Arrange
        var target = this.provider;
        string groupName = "CAS_Branch_Manager";
        string expectedUsername = "test.branchmanager";

        // Act
        var result = this.provider.GetUsersRecursively(groupName);

        // Assert
        Assert.NotNull(result);
        CollectionAssert.Contains(result, expectedUsername);
    }

但是通过在 resharper 上运行它会显示错误

System.DirectoryServices.AccountManagement.PrincipalServerDownException :无法联系服务器。----> System.DirectoryServices.Protocols.LdapException : LDAP 服务器不可用。

然后处理我写的异常

            [Test]
    [ExpectedException(typeof(PrincipalServerDownException ))]
    public void GetUsersRecursively()
    {
        // Arrange
        var target = this.provider;
        string groupName = "CAS_Branch_Manager";
        string expectedUsername = "test.branchmanager";

        // Act
        var result = this.provider.GetUsersRecursively(groupName);

        // Assert
        Assert.NotNull(result);
        CollectionAssert.Contains(result, expectedUsername);
    }

但是现在 PrincipalServerDownException 显示错误,因为无法解析符号“PrincipalServerDownException”。如何解决?

4

1 回答 1

0

您可能必须在测试项目中添加对“System.DirectoryServices”程序集的引用。

于 2013-02-05T07:49:20.420 回答