I'm currently attempting to implement the humble object design pattern. The only resource I can find regarding this is from the xunit testing web site. Unfortunately, I don't really understand it despite reading it several times. Is there perhaps another name this pattern goes by?
Here is what I have thus far.
public interface IHumbleExchangeWebService { GetItemResult BindToItems(IEnumerable<string> itemIds); SyncFolderItemsResult Sync(); void Subscribe(); } public class ExchangeWebServiceAdapter { private readonly IHumbleExchangeWebService _humbleExchangeWebService; public ExchangeWebServiceAdapter( IHumbleExchangeWebService humbleExchangeWebService) { _humbleExchangeWebService = humbleExchangeWebService; Start(); } private void Start() { SyncFolderItemsResult syncFolderItemsResults; while(true) { syncFolderItemsResults = _humbleExchangeWebService.Sync(); if(syncFolderItemsResults != null) break; } ProcessSyncResults(syncFolderItemsResults); LastSyncDateTime = Time.Now; _humbleExchangeWebService.Subscribe(); } private void ProcessSyncResults(SyncFolderItemsResult syncFolderItemsResults) { if (syncFolderItemsResults.Count <= 0) return; var missedItemIds = syncFolderItemsResults.ItemChange .ToList() .Select(x => x.ExchangeItem.ItemId); _humbleExchangeWebService.BindToItems(missedItemIds); } public DateTime LastSyncDateTime { get; private set; } } And as for the testing:
[TestFixture] public class Tests { private Mock<IHumbleExchangeWebService> _humbleExchangeWebServiceMock; [SetUp] public void SetUp() { _humbleExchangeWebServiceMock = new Mock<IHumbleExchangeWebService>(); } [Test] public void OnInitialiseWillSyncBeforeSubscribing() { var callOrder = 0; _humbleExchangeWebServiceMock .Setup(x => x.Sync()) .Returns(() => new SyncFolderItemsResult(0, false, String.Empty, GetExchangeItemChangeList())) .Callback(() => Assert.That(callOrder++, Is.EqualTo(0))); _humbleExchangeWebServiceMock .Setup(x => x.Subscribe()) .Callback(() => Assert.That(callOrder++, Is.EqualTo(1))); var service = GetConstructedService(); _humbleExchangeWebServiceMock.Verify(x => x.Sync(), Times.Once()); _humbleExchangeWebServiceMock.Verify(x => x.Subscribe(), Times.Once()); } [Test] public void WhenSyncingIsCompleteWillProcessMissingItem() { _humbleExchangeWebServiceMock .Setup(x => x.Sync()) .Returns(new SyncFolderItemsResult(1, false, It.IsAny<string>(), GetExchangeItemChangeList())); var service = GetConstructedService(); _humbleExchangeWebServiceMock.Verify(x => x.BindToItems(It.IsAny<IEnumerable<string>>())); } [Test] public void BindingItemsWillProcess() { _humbleExchangeWebServiceMock .Setup(x => x.Sync()) .Returns(new SyncFolderItemsResult(1, false, It.IsAny<string>(), GetExchangeItemChangeList())); var service = GetConstructedService(); _humbleExchangeWebServiceMock .Verify(x => x.BindToItems(new []{"AAA", "BBB", "CCC", "DDD"}), Times.Once()); _humbleExchangeWebServiceMock.VerifyAll(); } private ExchangeWebServiceAdapter GetConstructedService() { return new ExchangeWebServiceAdapter(_humbleExchangeWebServiceMock.Object); } private IEnumerable<ExchangeItemChange> GetExchangeItemChangeList() { yield return new ExchangeItemChange(ChangeType.Create, new ExchangeItem("AAA")); yield return new ExchangeItemChange(ChangeType.Create, new ExchangeItem("BBB")); yield return new ExchangeItemChange(ChangeType.Create, new ExchangeItem("CCC")); yield return new ExchangeItemChange(ChangeType.Create, new ExchangeItem("DDD")); } } Essentially, I'm simply wondering if I'm on the right track of making the humble object dumb. What I mean is that is it the proper way to extract all conditions that I can easily unit test into a wrapper class (e.g. ExchangeWebServiceAdapter).