太多的接口和包装器

本文关键字:包装 接口 太多 | 更新日期: 2023-09-27 18:09:40

我开始慢慢掌握单元测试和模拟的窍门,但这是一个缓慢的过程。我试过对这个活动目录代码进行单元测试。这个问题与AD没有严格的关系。

class ActiveDirectoryQueryer {    
   DirectorySearcher mSearcher;
   public ActiveDirectoryQueryer() {
      var searcher = new DirectorySearcher(...);
   }
   public void GetAllMailEntries() {
      MailEntries =
         mSearcher
         .FindAll()
         .Select(result => result.GetDirectoryEntry())
         .Select(BuildNewADUser)
         .ToList();
   }
   static ActiveDirectoryUser BuildNewADUser(DirectoryEntry pDirectoryEntry) {
      return ActiveDirectoryUser.Create(
         pDirectoryEntry.Guid,
         (pDirectoryEntry.Properties["name"].Value ?? "").ToString(),
         (pDirectoryEntry.Properties["mail"].Value ?? "").ToString()
      );
   }

所以,我想对GetAllMailEntries方法进行单元测试。为了使用MOQ实现这一点,我不得不为各种。net类型手动生成接口和包装器,并将上面的许多引用改为接口(如IDirectoryEntry)。下面的每个IXxxx接口都有一个相关的包装器类XxxxWrapper。我总共为这个测试添加了至少12个新的源文件。下面是我最后为单元测试编写的代码:

[TestMethod]
public void TestGetAllMailEntries() {
   var mockSearcher = new Mock<IDirectorySearcher>();
   var mockResultCollection = new Mock<ISearchResultCollection>();
   var mockSearchResult = new Mock<ISearchResult>();
   var mockDirectoryEntry = new Mock<IDirectoryEntry>();
   var mockPropertyCollection = new Mock<IPropertyCollection>();
   var nameMockPropertyValueCollection = new Mock<IPropertyValueCollection>();
   var mailMockPropertyValueCollection = new Mock<IPropertyValueCollection>();
   const string name = "SomeNameValue";
   const string mailAddress = "SomeMailAddress";
   nameMockPropertyValueCollection.SetupGet(pvc => pvc.Value).Returns(name);
   mailMockPropertyValueCollection.SetupGet(pvc => pvc.Value).Returns(mailAddress);
   mockPropertyCollection.SetupGet(pc => pc["name"]).Returns(nameMockPropertyValueCollection.Object);
   mockPropertyCollection.SetupGet(pc => pc["mail"]).Returns(mailMockPropertyValueCollection.Object);
   mockDirectoryEntry.SetupGet(de => de.Properties).Returns(mockPropertyCollection.Object);
   mockSearchResult.Setup(sr => sr.GetDirectoryEntry()).Returns(mockDirectoryEntry.Object);
   mockResultCollection.Setup(results => results.GetEnumerator()).Returns(new List<ISearchResult> { mockSearchResult.Object }.GetEnumerator());
   mockSearcher.Setup(searcher => searcher.FindAll()).Returns(mockResultCollection.Object);
   var queryer = new ActiveDirectoryQueryer(mockSearcher.Object);
   queryer.GetAllMailEntries();
   Assert.AreEqual(1, queryer.MailEntries.Count());
   var entry = queryer.MailEntries.Single();
   Assert.AreEqual(name, entry.Name);
   Assert.AreEqual(mailAddress, entry.EmailAddress);
}

有这么多接口和包装器类正常吗?(包装器是必要的,因为。net类型不能实现我的接口。)

太多的接口和包装器

我认为我的问题是过于紧密地镜像。net结构。我不应该把每一个。net类型都包装起来,直到我只得到原语。相反,我应该抓住第一个机会尽快删除所有依赖项。在本例中,它使用DirectorySearcher类和FindAll方法。

DirectorySearcher.FindAll返回一个SearchResultCollection,而不是认为我的"包装器"类只是一个适配器到。net类型,我应该更多地利用它。

忽略IDisposable的实现和其他不必要的代码,我的包装器看起来像这样:
public interface IDirectorySearcher : IDisposable {
   ISearchResultCollection FindAll();
}
class DirectorySearcherWrapper : IDirectorySearcher {
   DirectorySearcher mDirectorySearcher;
   DirectorySearcherWrapper(DirectorySearcher pDirectorySearcher) {
      mDirectorySearcher = pDirectorySearcher;
   }
   public static IDirectorySearcher Wrap(DirectorySearcher pDirectorySearcher) {
      return new DirectorySearcherWrapper(pDirectorySearcher);
   }
   public ISearchResultCollection FindAll() {
      return SearchResultCollectionWrapper.Wrap(mDirectorySearcher.FindAll());
   }
}

相反,我应该抓住机会在这里停止所有依赖。我不需要返回。net类型,甚至不需要返回。net类型的包装器,我现在可以使用这个接口返回我想要的任何东西。IE:如果我想从FindAll方法中得到的是一堆ActiveDirectoryUser s,那么就返回那个。

我的代码变成:

public interface IDirectorySearcher : IDisposable {
   IEnumerable<ActiveDirectoryUser> FindAll();
}
class DirectorySearcherWrapper : IDirectorySearcher {
   DirectorySearcher mDirectorySearcher;
   DirectorySearcherWrapper(DirectorySearcher pDirectorySearcher) {
      mDirectorySearcher = pDirectorySearcher;
   }
   public static IDirectorySearcher Wrap(DirectorySearcher pDirectorySearcher) {
      return new DirectorySearcherWrapper(pDirectorySearcher);
   }
   public IEnumerable<ActiveDirectoryUser> FindAll() {
      return
         mDirectorySearcher
         .FindAll()
         .Cast<SearchResult>()
         .Select(result => result.GetDirectoryEntry())
         .Select(/*BuildNewADUser*/)
         .ToList();
   }
}

GetAllMailEntries方法变得简单:

public void GetAllMailEntries() {
   MailEntries = mSearcher.FindAll();
}

单元测试变成:

[TestMethod]
public void TestGetAllMailEntries2() {
   var mockSearcher = new Mock<IDirectorySearcher>();
   mockSearcher
   .Setup(s => s.FindAll())
   .Returns(new[] {
      ActiveDirectoryUser.Create(new Guid(), "Name", "EmailAddress")
   });
   var queryer = new ActiveDirectoryQueryer(mockSearcher.Object);
   queryer.GetAllMailEntries();
   Assert.AreEqual(1, queryer.MailEntries.Count());
   var entry = queryer.MailEntries.Single();
   Assert.AreEqual("Name", entry.Name);
   Assert.AreEqual("EmailAddress", entry.EmailAddress);
}