如何整合测试这种方法?

时间:2013-02-05 07:42:37

标签: c# asp.net-mvc-3 integration-testing

您好我正在编写集成测试。

我的方法是

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:无法联系服务器。   ----&GT; 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”。你能解决吗?

1 个答案:

答案 0 :(得分:0)

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