From 5f7744d722af988add593f1ad1e7ee8ce7aaaa4d Mon Sep 17 00:00:00 2001 From: Turnerj Date: Wed, 9 Aug 2023 22:25:05 +0930 Subject: [PATCH] Replace Moq with NSubstitute --- tests/CacheTower.Tests/CacheStackTests.cs | 20 ++-- .../CacheTower.Tests/CacheTower.Tests.csproj | 2 +- .../Extensions/AutoCleanupExtensionTests.cs | 15 +-- .../Extensions/ExtensionContainerTests.cs | 62 ++++------ .../Redis/RedisLockExtensionTests.cs | 24 ++-- .../RedisRemoteEvictionExtensionTests.cs | 112 ++++++++---------- .../Database/MongoDbCacheLayerTests.cs | 8 +- .../Providers/Redis/RedisCacheLayerTests.cs | 12 +- .../ServiceCollectionExtensionsTests.cs | 24 ++-- 9 files changed, 130 insertions(+), 149 deletions(-) diff --git a/tests/CacheTower.Tests/CacheStackTests.cs b/tests/CacheTower.Tests/CacheStackTests.cs index d2fe3077..e1d3f797 100644 --- a/tests/CacheTower.Tests/CacheStackTests.cs +++ b/tests/CacheTower.Tests/CacheStackTests.cs @@ -5,7 +5,7 @@ using System.Threading.Tasks; using CacheTower.Providers.Memory; using Microsoft.VisualStudio.TestTools.UnitTesting; -using Moq; +using NSubstitute; namespace CacheTower.Tests { @@ -83,13 +83,13 @@ public async Task Evict_EvictsAllTheLayers() [TestMethod] public async Task Evict_TriggersCacheChangeExtension() { - var mockExtension = new Mock(); - await using var cacheStack = new CacheStack(null, new(new[] { new MemoryCacheLayer() }) { Extensions = new[] { mockExtension.Object } }); + var mockExtension = Substitute.For(); + await using var cacheStack = new CacheStack(null, new(new[] { new MemoryCacheLayer() }) { Extensions = new[] { mockExtension } }); var cacheEntry = await cacheStack.SetAsync("Evict_TriggerCacheChangeExtension", 42, TimeSpan.FromDays(1)); await cacheStack.EvictAsync("Evict_TriggerCacheChangeExtension"); - mockExtension.Verify(e => e.OnCacheEvictionAsync("Evict_TriggerCacheChangeExtension"), Times.Once); + await mockExtension.Received(1).OnCacheEvictionAsync("Evict_TriggerCacheChangeExtension"); } [TestMethod, ExpectedException(typeof(ObjectDisposedException))] public async Task Evict_ThrowsOnUseAfterDisposal() @@ -122,12 +122,12 @@ public async Task Flush_FlushesAllTheLayers() [TestMethod] public async Task Flush_TriggersCacheChangeExtension() { - var mockExtension = new Mock(); - await using var cacheStack = new CacheStack(null, new(new[] { new MemoryCacheLayer() }) { Extensions = new[] { mockExtension.Object } }); + var mockExtension = Substitute.For(); + await using var cacheStack = new CacheStack(null, new(new[] { new MemoryCacheLayer() }) { Extensions = new[] { mockExtension } }); await cacheStack.FlushAsync(); - mockExtension.Verify(e => e.OnCacheFlushAsync(), Times.Once); + await mockExtension.Received(1).OnCacheFlushAsync(); } [TestMethod, ExpectedException(typeof(ObjectDisposedException))] public async Task Flush_ThrowsOnUseAfterDisposal() @@ -201,11 +201,11 @@ public async Task Set_SetsAllTheLayers() [TestMethod] public async Task Set_TriggersCacheChangeExtension() { - var mockExtension = new Mock(); - await using var cacheStack = new CacheStack(null, new(new[] { new MemoryCacheLayer() }) { Extensions = new[] { mockExtension.Object } }); + var mockExtension = Substitute.For(); + await using var cacheStack = new CacheStack(null, new(new[] { new MemoryCacheLayer() }) { Extensions = new[] { mockExtension } }); var cacheEntry = await cacheStack.SetAsync("Set_TriggersCacheChangeExtension", 42, TimeSpan.FromDays(1)); - mockExtension.Verify(e => e.OnCacheUpdateAsync("Set_TriggersCacheChangeExtension", cacheEntry.Expiry, CacheUpdateType.AddOrUpdateEntry), Times.Once); + await mockExtension.Received(1).OnCacheUpdateAsync("Set_TriggersCacheChangeExtension", cacheEntry.Expiry, CacheUpdateType.AddOrUpdateEntry); } [TestMethod, ExpectedException(typeof(ArgumentNullException))] diff --git a/tests/CacheTower.Tests/CacheTower.Tests.csproj b/tests/CacheTower.Tests/CacheTower.Tests.csproj index 0bcb55a4..01e8985a 100644 --- a/tests/CacheTower.Tests/CacheTower.Tests.csproj +++ b/tests/CacheTower.Tests/CacheTower.Tests.csproj @@ -9,7 +9,6 @@ - @@ -17,6 +16,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/tests/CacheTower.Tests/Extensions/AutoCleanupExtensionTests.cs b/tests/CacheTower.Tests/Extensions/AutoCleanupExtensionTests.cs index 389f349c..adb10e1f 100644 --- a/tests/CacheTower.Tests/Extensions/AutoCleanupExtensionTests.cs +++ b/tests/CacheTower.Tests/Extensions/AutoCleanupExtensionTests.cs @@ -7,7 +7,8 @@ using CacheTower.Extensions; using CacheTower.Providers.Memory; using Microsoft.VisualStudio.TestTools.UnitTesting; -using Moq; +using NSubstitute; +using NSubstitute.ReceivedExtensions; namespace CacheTower.Tests.Extensions { @@ -34,20 +35,20 @@ public async Task ThrowForRegisteringTwoCacheStacks() public async Task RunsBackgroundCleanup() { await using var extension = new AutoCleanupExtension(TimeSpan.FromMilliseconds(500)); - var cacheStackMock = new Mock(); - extension.Register(cacheStackMock.Object); + var cacheStackMock = Substitute.For(); + extension.Register(cacheStackMock); await Task.Delay(TimeSpan.FromSeconds(2)); - cacheStackMock.Verify(c => c.CleanupAsync(), Times.AtLeast(2)); + await cacheStackMock.Received(Quantity.Within(2, int.MaxValue)).CleanupAsync(); } [TestMethod] public async Task BackgroundCleanupObeysCancel() { await using var extension = new AutoCleanupExtension(TimeSpan.FromMilliseconds(500), new CancellationToken(true)); - var cacheStackMock = new Mock(); - extension.Register(cacheStackMock.Object); + var cacheStackMock = Substitute.For(); + extension.Register(cacheStackMock); await Task.Delay(TimeSpan.FromSeconds(2)); - cacheStackMock.Verify(c => c.CleanupAsync(), Times.Never); + await cacheStackMock.DidNotReceive().CleanupAsync(); } } } diff --git a/tests/CacheTower.Tests/Extensions/ExtensionContainerTests.cs b/tests/CacheTower.Tests/Extensions/ExtensionContainerTests.cs index 2f190794..830dbc93 100644 --- a/tests/CacheTower.Tests/Extensions/ExtensionContainerTests.cs +++ b/tests/CacheTower.Tests/Extensions/ExtensionContainerTests.cs @@ -1,11 +1,8 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; using CacheTower.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; -using Moq; +using NSubstitute; namespace CacheTower.Tests.Extensions { @@ -27,76 +24,67 @@ public async Task AcceptsEmptyExtensions() [TestMethod] public async Task DistributedLockExtension() { - var cacheStackMock = new Mock(); - var distributedLockMock = new Mock(); - await using var container = new ExtensionContainer(new[] { distributedLockMock.Object }); + var cacheStackMock = Substitute.For(); + var distributedLockMock = Substitute.For(); + await using var container = new ExtensionContainer(new[] { distributedLockMock }); - container.Register(cacheStackMock.Object); + container.Register(cacheStackMock); var distributedLock = await container.AwaitAccessAsync("DistributedLockCacheKey"); - distributedLockMock.Verify(e => e.Register(cacheStackMock.Object), Times.Once); - distributedLockMock.Verify(e => e.AwaitAccessAsync("DistributedLockCacheKey"), Times.Once); + distributedLockMock.Received(1).Register(cacheStackMock); + await distributedLockMock.Received(1).AwaitAccessAsync("DistributedLockCacheKey"); } [TestMethod] public async Task CacheChangeExtension_Update() { - var cacheStackMock = new Mock(); - var valueRefreshMock = new Mock(); - await using var container = new ExtensionContainer(new[] { valueRefreshMock.Object }); + var cacheStackMock = Substitute.For(); + var valueRefreshMock = Substitute.For(); + await using var container = new ExtensionContainer(new[] { valueRefreshMock }); - container.Register(cacheStackMock.Object); + container.Register(cacheStackMock); var expiry = DateTime.UtcNow.AddDays(1); await container.OnCacheUpdateAsync("CacheChangeKey", expiry, CacheUpdateType.AddEntry); - valueRefreshMock.Verify(e => e.Register(cacheStackMock.Object), Times.Once); - valueRefreshMock.Verify(e => - e.OnCacheUpdateAsync("CacheChangeKey", expiry, CacheUpdateType.AddEntry), - Times.Once - ); + valueRefreshMock.Received(1).Register(cacheStackMock); + await valueRefreshMock.Received(1).OnCacheUpdateAsync("CacheChangeKey", expiry, CacheUpdateType.AddEntry); } [TestMethod] public async Task CacheChangeExtension_Eviction() { - var cacheStackMock = new Mock(); - var valueRefreshMock = new Mock(); - await using var container = new ExtensionContainer(new[] { valueRefreshMock.Object }); + var cacheStackMock = Substitute.For(); + var valueRefreshMock = Substitute.For(); + await using var container = new ExtensionContainer(new[] { valueRefreshMock }); - container.Register(cacheStackMock.Object); + container.Register(cacheStackMock); var expiry = DateTime.UtcNow.AddDays(1); await container.OnCacheEvictionAsync("CacheChangeKey"); - valueRefreshMock.Verify(e => e.Register(cacheStackMock.Object), Times.Once); - valueRefreshMock.Verify(e => - e.OnCacheEvictionAsync("CacheChangeKey"), - Times.Once - ); + valueRefreshMock.Received(1).Register(cacheStackMock); + await valueRefreshMock.Received(1).OnCacheEvictionAsync("CacheChangeKey"); } [TestMethod] public async Task CacheChangeExtension_Flush() { - var cacheStackMock = new Mock(); - var valueRefreshMock = new Mock(); - await using var container = new ExtensionContainer(new[] { valueRefreshMock.Object }); + var cacheStackMock = Substitute.For(); + var valueRefreshMock = Substitute.For(); + await using var container = new ExtensionContainer(new[] { valueRefreshMock }); - container.Register(cacheStackMock.Object); + container.Register(cacheStackMock); var expiry = DateTime.UtcNow.AddDays(1); await container.OnCacheFlushAsync(); - valueRefreshMock.Verify(e => e.Register(cacheStackMock.Object), Times.Once); - valueRefreshMock.Verify(e => - e.OnCacheFlushAsync(), - Times.Once - ); + valueRefreshMock.Received(1).Register(cacheStackMock); + await valueRefreshMock.Received(1).OnCacheFlushAsync(); } } } diff --git a/tests/CacheTower.Tests/Extensions/Redis/RedisLockExtensionTests.cs b/tests/CacheTower.Tests/Extensions/Redis/RedisLockExtensionTests.cs index 9df148c2..819b47e6 100644 --- a/tests/CacheTower.Tests/Extensions/Redis/RedisLockExtensionTests.cs +++ b/tests/CacheTower.Tests/Extensions/Redis/RedisLockExtensionTests.cs @@ -3,7 +3,7 @@ using CacheTower.Extensions.Redis; using CacheTower.Tests.Utils; using Microsoft.VisualStudio.TestTools.UnitTesting; -using Moq; +using NSubstitute; using StackExchange.Redis; namespace CacheTower.Tests.Extensions.Redis; @@ -33,7 +33,7 @@ public void ThrowForNullChannel() public void ThrowForRegisteringTwoCacheStacks() { var extension = new RedisLockExtension(RedisHelper.GetConnection()); - var cacheStack = new Mock().Object; + var cacheStack = Substitute.For(); extension.Register(cacheStack); extension.Register(cacheStack); } @@ -68,9 +68,9 @@ public async Task RefreshValueNotifiesChannelSubscribers() var connection = RedisHelper.GetConnection(); - var cacheStackMock = new Mock(); + var cacheStackMock = Substitute.For(); var extension = new RedisLockExtension(connection, RedisLockOptions.Default); - extension.Register(cacheStackMock.Object); + extension.Register(cacheStackMock); var completionSource = new TaskCompletionSource(); @@ -112,9 +112,9 @@ public async Task ObservedLockSingle() var connection = RedisHelper.GetConnection(); - var cacheStackMock = new Mock(); + var cacheStackMock = Substitute.For(); var extension = new RedisLockExtension(connection, RedisLockOptions.Default); - extension.Register(cacheStackMock.Object); + extension.Register(cacheStackMock); //Establish lock await connection.GetDatabase().StringSetAsync("Lock:TestKey", RedisValue.EmptyString); @@ -143,9 +143,9 @@ public async Task ObservedLockMultiple() var connection = RedisHelper.GetConnection(); - var cacheStackMock = new Mock(); + var cacheStackMock = Substitute.For(); var extension = new RedisLockExtension(connection, RedisLockOptions.Default); - extension.Register(cacheStackMock.Object); + extension.Register(cacheStackMock); //Establish lock await connection.GetDatabase().StringSetAsync("Lock:TestKey", RedisValue.EmptyString); @@ -178,9 +178,9 @@ public async Task FailsafeOnSubscriberFailure() var connection = RedisHelper.GetConnection(); - var cacheStackMock = new Mock(); + var cacheStackMock = Substitute.For(); var extension = new RedisLockExtension(connection, RedisLockOptions.Default with { LockTimeout = TimeSpan.FromSeconds(1) }); - extension.Register(cacheStackMock.Object); + extension.Register(cacheStackMock); //Establish lock await connection.GetDatabase().StringSetAsync("Lock:TestKey", RedisValue.EmptyString); @@ -212,9 +212,9 @@ public async Task BusyLockCheckWorksWhenSubscriberFails() var connection = RedisHelper.GetConnection(); - var cacheStackMock = new Mock(); + var cacheStackMock = Substitute.For(); var extension = new RedisLockExtension(connection, RedisLockOptions.Default with { LockCheckStrategy = LockCheckStrategy.WithSpinLock(TimeSpan.FromMilliseconds(50)) }); - extension.Register(cacheStackMock.Object); + extension.Register(cacheStackMock); //Establish lock await connection.GetDatabase().StringSetAsync("Lock:TestKey", RedisValue.EmptyString); diff --git a/tests/CacheTower.Tests/Extensions/Redis/RedisRemoteEvictionExtensionTests.cs b/tests/CacheTower.Tests/Extensions/Redis/RedisRemoteEvictionExtensionTests.cs index f068a317..ef15f8cf 100644 --- a/tests/CacheTower.Tests/Extensions/Redis/RedisRemoteEvictionExtensionTests.cs +++ b/tests/CacheTower.Tests/Extensions/Redis/RedisRemoteEvictionExtensionTests.cs @@ -1,13 +1,9 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; using CacheTower.Extensions.Redis; -using CacheTower.Providers.Memory; using CacheTower.Tests.Utils; using Microsoft.VisualStudio.TestTools.UnitTesting; -using Moq; +using NSubstitute; using StackExchange.Redis; namespace CacheTower.Tests.Extensions.Redis @@ -37,9 +33,9 @@ public void ThrowForNullChannel() public void ThrowForRegisteringMoreThanOneCacheStack() { var extension = new RedisRemoteEvictionExtension(RedisHelper.GetConnection()); - var cacheStackMock = new Mock(); - extension.Register(cacheStackMock.Object); - extension.Register(cacheStackMock.Object); + var cacheStackMock = Substitute.For(); + extension.Register(cacheStackMock); + extension.Register(cacheStackMock); } [TestMethod] @@ -49,19 +45,19 @@ public async Task RemoteEvictionOccursOnRefresh() var connection = RedisHelper.GetConnection(); - var cacheStackMockOne = new Mock(); - var cacheLayerOne = new Mock(); - cacheStackMockOne.Setup(c => c.GetCacheLayers()) - .Returns(new[] { cacheLayerOne.Object }); + var cacheStackMockOne = Substitute.For(); + var cacheLayerOne = Substitute.For(); + cacheStackMockOne.GetCacheLayers() + .Returns(new[] { cacheLayerOne }); var extensionOne = new RedisRemoteEvictionExtension(connection); - extensionOne.Register(cacheStackMockOne.Object); + extensionOne.Register(cacheStackMockOne); - var cacheStackMockTwo = new Mock(); - var cacheLayerTwo = new Mock(); - cacheStackMockTwo.Setup(c => c.GetCacheLayers()) - .Returns(new[] { cacheLayerTwo.Object }); + var cacheStackMockTwo = Substitute.For(); + var cacheLayerTwo = Substitute.For(); + cacheStackMockTwo.GetCacheLayers() + .Returns(new[] { cacheLayerTwo }); var extensionTwo = new RedisRemoteEvictionExtension(connection); - extensionTwo.Register(cacheStackMockTwo.Object); + extensionTwo.Register(cacheStackMockTwo); var completionSource = new TaskCompletionSource(); connection.GetSubscriber().Subscribe("CacheTower.RemoteEviction").OnMessage(channelMessage => @@ -89,8 +85,8 @@ public async Task RemoteEvictionOccursOnRefresh() await Task.Delay(500); - cacheLayerOne.Verify(c => c.EvictAsync("TestKey"), Times.Never, "Eviction took place locally where it should have been skipped"); - cacheLayerTwo.Verify(c => c.EvictAsync("TestKey"), Times.Once, "Eviction was skipped where it should have taken place locally"); + await cacheLayerOne.DidNotReceive().EvictAsync("TestKey"); //else; Eviction took place locally where it should have been skipped + await cacheLayerTwo.Received(1).EvictAsync("TestKey"); //else; Eviction was skipped where it should have taken place locally } [TestMethod] @@ -100,19 +96,19 @@ public async Task RemoteEvictionOccursOnLocalEviction() var connection = RedisHelper.GetConnection(); - var cacheStackMockOne = new Mock(); - var cacheLayerOne = new Mock(); - cacheStackMockOne.Setup(c => c.GetCacheLayers()) - .Returns(new[] { cacheLayerOne.Object }); + var cacheStackMockOne = Substitute.For(); + var cacheLayerOne = Substitute.For(); + cacheStackMockOne.GetCacheLayers() + .Returns(new[] { cacheLayerOne }); var extensionOne = new RedisRemoteEvictionExtension(connection); - extensionOne.Register(cacheStackMockOne.Object); + extensionOne.Register(cacheStackMockOne); - var cacheStackMockTwo = new Mock(); - var cacheLayerTwo = new Mock(); - cacheStackMockTwo.Setup(c => c.GetCacheLayers()) - .Returns(new[] { cacheLayerTwo.Object }); + var cacheStackMockTwo = Substitute.For(); + var cacheLayerTwo = Substitute.For(); + cacheStackMockTwo.GetCacheLayers() + .Returns(new[] { cacheLayerTwo }); var extensionTwo = new RedisRemoteEvictionExtension(connection); - extensionTwo.Register(cacheStackMockTwo.Object); + extensionTwo.Register(cacheStackMockTwo); var completionSource = new TaskCompletionSource(); connection.GetSubscriber().Subscribe("CacheTower.RemoteEviction").OnMessage(channelMessage => @@ -144,8 +140,8 @@ public async Task RemoteEvictionOccursOnLocalEviction() await Task.Delay(500); - cacheLayerOne.Verify(c => c.EvictAsync("TestKey"), Times.Never, "Eviction took place locally where it should have been skipped"); - cacheLayerTwo.Verify(c => c.EvictAsync("TestKey"), Times.Once, "Eviction was skipped where it should have taken place locally"); + await cacheLayerOne.DidNotReceive().EvictAsync("TestKey"); //else; Eviction took place locally where it should have been skipped + await cacheLayerTwo.Received(1).EvictAsync("TestKey"); //else; Eviction was skipped where it should have taken place locally } [TestMethod] @@ -155,19 +151,19 @@ public async Task RemoteFlush() var connection = RedisHelper.GetConnection(); - var cacheStackMockOne = new Mock(); - var cacheLayerOne = new Mock(); - cacheStackMockOne.Setup(c => c.GetCacheLayers()) - .Returns(new[] { cacheLayerOne.Object }); + var cacheStackMockOne = Substitute.For(); + var cacheLayerOne = Substitute.For(); + cacheStackMockOne.GetCacheLayers() + .Returns(new[] { cacheLayerOne }); var extensionOne = new RedisRemoteEvictionExtension(connection); - extensionOne.Register(cacheStackMockOne.Object); + extensionOne.Register(cacheStackMockOne); - var cacheStackMockTwo = new Mock(); - var cacheLayerTwo = new Mock(); - cacheStackMockTwo.Setup(c => c.GetCacheLayers()) - .Returns(new[] { cacheLayerTwo.Object }); + var cacheStackMockTwo = Substitute.For(); + var cacheLayerTwo = Substitute.For(); + cacheStackMockTwo.GetCacheLayers() + .Returns(new[] { cacheLayerTwo }); var extensionTwo = new RedisRemoteEvictionExtension(connection); - extensionTwo.Register(cacheStackMockTwo.Object); + extensionTwo.Register(cacheStackMockTwo); var completionSource = new TaskCompletionSource(); connection.GetSubscriber().Subscribe("CacheTower.RemoteFlush").OnMessage(channelMessage => @@ -199,8 +195,8 @@ public async Task RemoteFlush() await Task.Delay(500); - cacheLayerOne.Verify(c => c.FlushAsync(), Times.Never, "Flush took place locally where it should have been skipped"); - cacheLayerTwo.Verify(c => c.FlushAsync(), Times.Once, "Flush was skipped where it should have taken place locally"); + await cacheLayerOne.DidNotReceive().FlushAsync(); //else; Flush took place locally where it should have been skipped + await cacheLayerTwo.Received(1).FlushAsync(); //else; Flush was skipped where it should have taken place locally } [TestMethod] @@ -210,27 +206,23 @@ public async Task NoEvictionOnNewEntries() var realConnection = RedisHelper.GetConnection(); - var connectionMock = new Mock(); - var subscriberMock = new Mock(); + var connectionMock = Substitute.For(); + var subscriberMock = Substitute.For(); - subscriberMock - .Setup(subscriber => subscriber.Subscribe(It.IsAny(), It.IsAny())) - .Returns(() => realConnection.GetSubscriber().Subscribe("DummyMessageQueue")); - connectionMock - .Setup(connection => connection.GetSubscriber(It.IsAny())) - .Returns(subscriberMock.Object); + subscriberMock.Subscribe(Arg.Any(), Arg.Any()) + .Returns(x => realConnection.GetSubscriber().Subscribe("DummyMessageQueue")); + subscriberMock.Subscribe(Arg.Any(), Arg.Any()) + .Returns(x => realConnection.GetSubscriber().Subscribe("DummyMessageQueue")); + connectionMock.GetSubscriber(Arg.Any()) + .Returns(subscriberMock); - var cacheLayerOne = new Mock(); - var extensionOne = new RedisRemoteEvictionExtension(connectionMock.Object); - var cacheStackOne = new CacheStack(null, new(new[] { cacheLayerOne.Object }) { Extensions = new[] { extensionOne } }); + var cacheLayerOne = Substitute.For(); + var extensionOne = new RedisRemoteEvictionExtension(connectionMock); + var cacheStackOne = new CacheStack(null, new(new[] { cacheLayerOne }) { Extensions = new[] { extensionOne } }); await cacheStackOne.GetOrSetAsync("NoEvictionOnNewEntries", _ => Task.FromResult(1), new CacheSettings(TimeSpan.FromMinutes(5))); - subscriberMock - .Verify( - subscriber => subscriber.PublishAsync("CacheTower.RemoteEviction", It.IsAny(), It.IsAny()), - Times.Never - ); + await subscriberMock.DidNotReceive().PublishAsync("CacheTower.RemoteEviction", Arg.Any(), Arg.Any()); } } } \ No newline at end of file diff --git a/tests/CacheTower.Tests/Providers/Database/MongoDbCacheLayerTests.cs b/tests/CacheTower.Tests/Providers/Database/MongoDbCacheLayerTests.cs index 5ebe81fc..89f08ca0 100644 --- a/tests/CacheTower.Tests/Providers/Database/MongoDbCacheLayerTests.cs +++ b/tests/CacheTower.Tests/Providers/Database/MongoDbCacheLayerTests.cs @@ -5,7 +5,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using MongoFramework; using MongoFramework.Infrastructure.Indexing; -using Moq; +using NSubstitute; namespace CacheTower.Tests.Providers.Database; @@ -37,11 +37,11 @@ public async Task IsCacheAvailable() await AssertCacheAvailabilityAsync(new MongoDbCacheLayer(MongoDbHelper.GetConnection()), true); - var connectionMock = new Mock(); - connectionMock.Setup(c => c.GetDatabase()).Throws(); + var connectionMock = Substitute.For(); + connectionMock.GetDatabase().Returns(x => throw new Exception()); EntityIndexWriter.ClearCache(); - await AssertCacheAvailabilityAsync(new MongoDbCacheLayer(connectionMock.Object), false); + await AssertCacheAvailabilityAsync(new MongoDbCacheLayer(connectionMock), false); } [TestMethod] diff --git a/tests/CacheTower.Tests/Providers/Redis/RedisCacheLayerTests.cs b/tests/CacheTower.Tests/Providers/Redis/RedisCacheLayerTests.cs index c71b398c..88aa5bb1 100644 --- a/tests/CacheTower.Tests/Providers/Redis/RedisCacheLayerTests.cs +++ b/tests/CacheTower.Tests/Providers/Redis/RedisCacheLayerTests.cs @@ -4,7 +4,7 @@ using CacheTower.Serializers.Protobuf; using CacheTower.Tests.Utils; using Microsoft.VisualStudio.TestTools.UnitTesting; -using Moq; +using NSubstitute; using StackExchange.Redis; namespace CacheTower.Tests.Providers.Redis @@ -29,12 +29,12 @@ public async Task IsCacheAvailable() { await AssertCacheAvailabilityAsync(new RedisCacheLayer(RedisHelper.GetConnection(), new RedisCacheLayerOptions(ProtobufCacheSerializer.Instance)), true); - var connectionMock = new Mock(); - var databaseMock = new Mock(); - connectionMock.Setup(cm => cm.GetDatabase(It.IsAny(), It.IsAny())).Returns(databaseMock.Object); - databaseMock.Setup(db => db.PingAsync(It.IsAny())).Throws(); + var connectionMock = Substitute.For(); + var databaseMock = Substitute.For(); + connectionMock.GetDatabase(Arg.Any(), Arg.Any()).Returns(databaseMock); + databaseMock.PingAsync(Arg.Any()).Returns(Task.FromException(new Exception())); - await AssertCacheAvailabilityAsync(new RedisCacheLayer(connectionMock.Object, new RedisCacheLayerOptions(ProtobufCacheSerializer.Instance)), false); + await AssertCacheAvailabilityAsync(new RedisCacheLayer(connectionMock, new RedisCacheLayerOptions(ProtobufCacheSerializer.Instance)), false); } [TestMethod] diff --git a/tests/CacheTower.Tests/ServiceCollectionExtensionsTests.cs b/tests/CacheTower.Tests/ServiceCollectionExtensionsTests.cs index a2b78699..8c2507fb 100644 --- a/tests/CacheTower.Tests/ServiceCollectionExtensionsTests.cs +++ b/tests/CacheTower.Tests/ServiceCollectionExtensionsTests.cs @@ -8,7 +8,7 @@ using CacheTower.Serializers.NewtonsoftJson; using Microsoft.Extensions.DependencyInjection; using Microsoft.VisualStudio.TestTools.UnitTesting; -using Moq; +using NSubstitute; namespace CacheTower.Tests; @@ -178,10 +178,10 @@ public void GenericCacheStack_NamedCacheStackBuilder_ValidName() public void GenericCacheStack_CacheStackBuilder_CustomCacheContextActivator() { var serviceCollection = new ServiceCollection(); - var contextActivatorMock = new Mock(); + var contextActivatorMock = Substitute.For(); var hasBuilderBeenCalled = false; - serviceCollection.AddCacheStack(contextActivatorMock.Object, builder => + serviceCollection.AddCacheStack(contextActivatorMock, builder => { hasBuilderBeenCalled = true; builder.AddMemoryCacheLayer(); @@ -200,14 +200,14 @@ public void GenericCacheStack_CacheStackBuilder_CustomCacheContextActivator() public void CacheStackBuilder_AddCacheLayers() { var cacheStackBuilder = new CacheStackBuilder(); - var mongoDbConnectionMock = new Mock(); - var redisConnectionMock = new Mock(); + var mongoDbConnectionMock = Substitute.For(); + var redisConnectionMock = Substitute.For(); cacheStackBuilder .AddMemoryCacheLayer() .AddFileCacheLayer(new FileCacheLayerOptions("./FileCacheLayer", NewtonsoftJsonCacheSerializer.Instance)) - .AddMongoDbCacheLayer(mongoDbConnectionMock.Object) - .AddRedisCacheLayer(redisConnectionMock.Object, new RedisCacheLayerOptions(NewtonsoftJsonCacheSerializer.Instance)); + .AddMongoDbCacheLayer(mongoDbConnectionMock) + .AddRedisCacheLayer(redisConnectionMock, new RedisCacheLayerOptions(NewtonsoftJsonCacheSerializer.Instance)); Assert.AreEqual(4, cacheStackBuilder.CacheLayers.Count); Assert.IsInstanceOfType(cacheStackBuilder.CacheLayers[0], typeof(MemoryCacheLayer)); @@ -220,14 +220,14 @@ public void CacheStackBuilder_AddCacheLayers() public void CacheStackBuilder_WithExtensions() { var cacheStackBuilder = new CacheStackBuilder(); - var redisConnectionMock = new Mock(); - redisConnectionMock.Setup(r => r.GetSubscriber(It.IsAny())) - .Returns(new Mock().Object); + var redisConnectionMock = Substitute.For(); + redisConnectionMock.GetSubscriber(Arg.Any()) + .Returns(Substitute.For()); cacheStackBuilder .WithCleanupFrequency(TimeSpan.FromSeconds(5)) - .WithRedisDistributedLocking(redisConnectionMock.Object) - .WithRedisRemoteEviction(redisConnectionMock.Object); + .WithRedisDistributedLocking(redisConnectionMock) + .WithRedisRemoteEviction(redisConnectionMock); Assert.AreEqual(3, cacheStackBuilder.Extensions.Count); Assert.IsInstanceOfType(cacheStackBuilder.Extensions[0], typeof(AutoCleanupExtension));