C# 如何集成测试这种方法?

C# 如何集成测试这种方法?,c#,asp.net-mvc-3,integration-testing,C#,Asp.net Mvc 3,Integration Testing,嗨,我正在写集成测试 我的方法是 public IList<string> GetUsersRecursively(string groupName) { using (var context = GetPrincipalContext()) { using (var group = GroupPrincipal.FindByIdentity(context, groupName)) {

嗨,我正在写集成测试

我的方法是

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;
    }
但通过在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。如何解决此问题?

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

插入模拟主体,并使用它而不是真实主体。您想编写单元测试还是集成测试?对于单元测试,方法是包装GroupPrincipal,以便您可以模拟它。我只想将exception PrincipalServerDownException作为预期的异常处理,但我面临着resharper抛出的错误。如何解决它?是的,我添加了引用,但仍然PrincipalServerDownException显示错误。实际上它在System.DirectoryServices.AccountManagement程序集中。
            [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);
    }