Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

139
Views
Testing Ldapconnection Sendrequest

I am using Ldapconnection.Sendrequest because I am running on Linux, so I can only use classes from the System.DirectoryServices.Protocols namespace.

The code works perfectly well against a live Active Directory, but here is an example anyway.

// retrieves the distinguishedname for all groups, starting from dc=test,dc=local
var request = new SearchRequest("dc=test,dc=local", "(Objectclass=group)", "distinguishedname");
var searchResponse = ldapConnection.SendRequest(request) as SearchResponse;

My question is, how can this be unit tested? I would like to unit test it because a different searchResponse will cause a different path to be taken through code.

I have tried mocking SearchResponse: (How to create an instance of SearchResponse class (which has no public constructors)?)

var ctors = typeof (SearchResponse).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance);
        var neededCtor = ctors.First(
            ctor =>
                ctor.GetParameters().Count() == 5);
        SearchResponse response = neededCtor.Invoke(new object[]
        {
            "distinguishedName",
            null, // System.DirectoryServices.Protocols.DirectoryControl[]
            null, // System.DirectoryServices.Protocols.ResultCode
            errorMessage,
            null // System.Uri[]
        }) as SearchResponse;
        return response;

But I cannot find a way to mock SearchResponse.Entries, as SearchResultEntryCollection and SearchResultEntry are (seemingly) unmockable. It's as though the dotnet team has gone out of its way to make this area completely unmockable.

I'm new to using LdapConnection (previously I could use the AccountManagement class, as I was running in Windows), so maybe I shouldn't be using LdapConnection in the first place?

Equally, I'd be open to running the AD tests in memory, as I would for database access, but I cannot find a way to do so.

Many thanks for you help, Dan

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!