简体   繁体   中英

How to integrate test this method?

Hi I am writing integration tests.

My method is

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

And Test I have written is

    [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);
    }

But by running it on resharper it shows error that

System.DirectoryServices.AccountManagement.PrincipalServerDownException : The server could not be contacted. ----> System.DirectoryServices.Protocols.LdapException : The LDAP server is unavailable.

Then to handle exception I have written as

            [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);
    }

But now PrincipalServerDownException showing error as cannot resolve symbol "PrincipalServerDownException". HOw to solve it ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM