Last active
October 7, 2021 04:13
-
-
Save dsolovay/7f31747b15c50d883c418fc28a1b899f to your computer and use it in GitHub Desktop.
Illustrates zero-friction TDD ideas, use of Sitecore base classes, and tuple deconstructors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using NSubstitute; | |
using Sitecore.Abstractions; | |
namespace UnitTestingDemo | |
{ | |
internal static class CacheFixture | |
{ | |
public static (CustomCacheFactory, BaseCacheManager, BaseSettings) CreateCache() | |
{ | |
BaseCacheManager cacheManager = Substitute.For<BaseCacheManager>(); | |
BaseSettings settings = Substitute.For<BaseSettings>(); | |
return (new CustomCacheFactory(cacheManager, settings), cacheManager, settings); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Sitecore.Abstractions; | |
using Sitecore.Caching; | |
namespace UnitTestingDemo | |
{ | |
internal class CustomCacheFactory | |
{ | |
private readonly BaseCacheManager _baseCacheManager; | |
private readonly BaseSettings _baseSettings; | |
public CustomCacheFactory(BaseCacheManager baseCacheManager, BaseSettings baseSettings) | |
{ | |
_baseCacheManager = baseCacheManager; | |
_baseSettings = baseSettings; | |
} | |
public static string DefaultSize => "10 MB"; | |
public ICache CreateCache() | |
{ | |
string sizeString = _baseSettings.GetSetting("CustomCacheSize", DefaultSize); | |
long size = Sitecore.StringUtil.ParseSizeString(sizeString); | |
return _baseCacheManager.GetNamedInstance("OurCustomCache", size, true); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using FluentAssertions; | |
using NSubstitute; | |
using Sitecore.Abstractions; | |
using Xunit; | |
namespace UnitTestingDemo | |
{ | |
public class MyCacheFactoryShould | |
{ | |
[Fact] | |
public void ReturnAnObject() | |
{ | |
(CustomCacheFactory sut, _ , _) = CacheFixture.CreateCache(); | |
sut.CreateCache().Should().NotBeNull(); | |
} | |
[Fact] | |
public void GetCacheFromSitecore() | |
{ | |
(CustomCacheFactory sut, BaseCacheManager manager, _) = CacheFixture.CreateCache(); | |
sut.CreateCache(); | |
manager.Received().GetNamedInstance("OurCustomCache", Arg.Any<long>(), Arg.Any<bool>()); | |
} | |
[Fact] | |
public void GetSizeFromSettings() | |
{ | |
(CustomCacheFactory sut, BaseCacheManager manager, BaseSettings settings) = CacheFixture.CreateCache(); | |
string someSize = "1 MB"; | |
long expected = Sitecore.StringUtil.ParseSizeString(someSize); | |
settings.GetSetting("CustomCacheSize", Arg.Any<string>()).Returns(someSize); | |
sut.CreateCache(); | |
manager.Received().GetNamedInstance(Arg.Any<string>(), expected, Arg.Any<bool>()); | |
} | |
[Fact] | |
public void GetDefaultSize() | |
{ | |
(CustomCacheFactory sut, BaseCacheManager manager, BaseSettings settings) = CacheFixture.CreateCache(); | |
settings.GetSetting( Arg.Any<string>(), CustomCacheFactory.DefaultSize).Returns(CustomCacheFactory.DefaultSize); | |
long expected = Sitecore.StringUtil.ParseSizeString(CustomCacheFactory.DefaultSize); | |
sut.CreateCache(); | |
manager.Received().GetNamedInstance(Arg.Any<string>(), expected, Arg.Any<bool>()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See https://blog.ploeh.dk/2009/01/28/Zero-FrictionTDD/