Bullying Property DirectoryEntry Properties

I am trying to use unit test some Active Directory code, almost the same as in this question:

Create an instance of DirectoryEntry for use in the test.

The accepted answer suggests implementing a wrapper / adapter for the class DirectoryEntrythat I have:

public interface IDirectoryEntry : IDisposable
{
    PropertyCollection Properties { get; }
}

public class DirectoryEntryWrapper : DirectoryEntry, IDirectoryEntry
{
}

The problem is that the Properties property in my layout is IDirectoryEntrynot initialized. Trying to set the layout like this:

this._directoryEntryMock = new Mock<IDirectoryEntry>();
this._directoryEntryMock.Setup(m => m.Properties)
                        .Returns(new PropertyCollection());

Results with the following error:

The type 'System.DirectoryServices.PropertyCollection' does not contain constructors

As I understand it, this error occurs when trying to create an instance of a class with only internal constructors:

Type '...' does not contain constructors

/ PropertyCollection, , .

, / "" DirectoryEntry ?

+2
2

, PropertyCollection. , , - , DirectoryEntry , . , :

  • Properties , PropertyCollection (IDictionary, ICollection IEnumerable), .
  • - PropertyCollection , directoryEntry.Properties , Properties
  • IDirectoryEntry/DirectoryEntryWrapper, , , Properties

1 , . 2 , PropertiesCollection , , . ( ) - 3.

+3

( 1):

public interface IDirectoryEntry : IDisposable
{
    IDictionary Properties { get; }
}

public class DirectoryEntryWrapper : IDirectoryEntry
{
    private readonly DirectoryEntry _entry;

    public DirectoryEntryWrapper(DirectoryEntry entry)
    {
        _entry = entry;
        Properties = _entry.Properties;
    }

    public void Dispose()
    {
        if (_entry != null)
        {
            _entry.Dispose();
        }
    }

    public IDictionary Properties { get; private set; }
}

:

this._directoryEntryMock = new Mock<IDirectoryEntry>();
this._directoryEntryMock
        .Setup(m => m.Properties)
        .Returns(new Hashtable()
        {
            { "PasswordExpirationDate", SystemTime.Now().AddMinutes(-1) }
        });
+6

All Articles