0

I have a method like this:

public List<string> GetAllDomains()
{
       List<string> domains = new List<string>();

      DirectoryContext directoryContext = new DirectoryContext(DirectoryContextType.Domain);

      using (Domain currentDomain = Domain.GetDomain(directoryContext))
      using (Forest forest = currentDomain.Forest)
      {
          DomainCollection addDomainsInForest = forest.Domains;
          foreach (Domain domain in addDomainsInForest)
          {
              domains.Add(domain.Name);
          }
      }

      return domains; 

}

How can I write an unit test for this method? Thanks.

2
  • what do you want to test? what cases? Commented Oct 28, 2015 at 1:09
  • What unit test framework? Is your code designed with testing in mind? Does it use dependency injection? What are you going to use to mock your dependencies? Commented Oct 28, 2015 at 1:16

1 Answer 1

1

Since your method speaks with Active Directory, then you cannot create a unit test for it. You can create an integration test. Integration tests are tests that test how your code integrates with the environment (or with some other code). In your case, you want to test how your code integrates with Active Directory.

In this case, the integration test can only work in a specific environment which you know the names of domains that it will contain.

Having said that, if your code contains a good amount of logic, you can create an abstraction by hiding active directory code behind some interface/interfaces and then you can unit test that logic by mocking these interfaces.

In your case however, I cannot see that your method contains much logic. Its a simple consumption of the Active Directory API.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.