Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Moq #531

Merged
merged 2 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<GenerateDocumentationFile>false</GenerateDocumentationFile>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>latest</LangVersion>
<LangVersion>10</LangVersion>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
<NeutralLanguage>en-US</NeutralLanguage>
<Nullable>enable</Nullable>
Expand Down
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageVersion Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
<PackageVersion Include="Moq" Version="4.20.0" />
<PackageVersion Include="NSubstitute" Version="5.0.0" />
<PackageVersion Include="ReportGenerator" Version="5.1.23" />
<PackageVersion Include="Shouldly" Version="4.2.1" />
<PackageVersion Include="xunit" Version="2.5.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void Timing(long duration, double sampleRate, string bucket, Dictionary<s

private void SendMessage(double sampleRate, in StatsDMessage msg)
{
bool shouldSendMessage = (sampleRate >= DefaultSampleRate || sampleRate > Random.NextDouble()) && msg.StatBucket != null;
bool shouldSendMessage = (sampleRate >= DefaultSampleRate || sampleRate > Random.NextDouble()) && msg.StatBucket != null && msg.StatBucket.Length > 0;

if (!shouldSendMessage)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Moq;
using NSubstitute;

namespace JustEat.StatsD.Buffered;

Expand All @@ -9,30 +9,30 @@ public static void Increment_Is_Noop_If_Bucket_Is_Null()
{
// Arrange
var configuration = new StatsDConfiguration();
var transport = new Mock<IStatsDTransport>();
var transport = Substitute.For<IStatsDTransport>();

var publisher = new BufferBasedStatsDPublisher(configuration, transport.Object);
var publisher = new BufferBasedStatsDPublisher(configuration, transport);

// Act
publisher.Increment(1, 1, null!, null);

// Assert
transport.Verify((p) => p.Send(It.Ref<ArraySegment<byte>>.IsAny), Times.Never());
transport.DidNotReceiveWithAnyArgs().Send(default);
}

[Fact]
public static void Increment_Sends_If_Default_Buffer_Is_Too_Small()
{
// Arrange
var configuration = new StatsDConfiguration() { Prefix = new string('a', 513) };
var transport = new Mock<IStatsDTransport>();
var transport = Substitute.For<IStatsDTransport>();

var publisher = new BufferBasedStatsDPublisher(configuration, transport.Object);
var publisher = new BufferBasedStatsDPublisher(configuration, transport);

// Act
publisher.Increment(1, 1, "foo");

// Assert
transport.Verify((p) => p.Send(It.Ref<ArraySegment<byte>>.IsAny), Times.Once());
transport.ReceivedWithAnyArgs(1).Send(default);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Net;
using Moq;
using NSubstitute;

namespace JustEat.StatsD.EndpointLookups;

Expand All @@ -8,25 +8,25 @@ public static class CachedEndpointSourceTests
[Fact]
public static void CachedValueIsReturnedFromInner()
{
var mockInner = new Mock<IEndPointSource>();
mockInner.Setup(x => x.GetEndpoint()).Returns(MakeTestIpEndPoint());
var inner = Substitute.For<IEndPointSource>();
inner.GetEndpoint().Returns(MakeTestIpEndPoint());

var cachedEndpoint = new CachedEndpointSource(mockInner.Object, TimeSpan.FromMinutes(5));
var cachedEndpoint = new CachedEndpointSource(inner, TimeSpan.FromMinutes(5));

var value = cachedEndpoint.GetEndpoint();
value.ShouldNotBeNull();
value.ShouldBe(MakeTestIpEndPoint());

mockInner.Verify(x => x.GetEndpoint(), Times.Exactly(1));
inner.Received(1).GetEndpoint();
}

[Fact]
public static void CachedValueIsReturnedOnce()
{
var mockInner = new Mock<IEndPointSource>();
mockInner.Setup(x => x.GetEndpoint()).Returns(MakeTestIpEndPoint());
var inner = Substitute.For<IEndPointSource>();
inner.GetEndpoint().Returns(MakeTestIpEndPoint());

var cachedEndpoint = new CachedEndpointSource(mockInner.Object, TimeSpan.FromMinutes(5));
var cachedEndpoint = new CachedEndpointSource(inner, TimeSpan.FromMinutes(5));

var value1 = cachedEndpoint.GetEndpoint();
var value2 = cachedEndpoint.GetEndpoint();
Expand All @@ -36,26 +36,26 @@ public static void CachedValueIsReturnedOnce()
value1.ShouldBe(value2);
value1.ShouldBe(value3);

mockInner.Verify(x => x.GetEndpoint(), Times.Exactly(1));
inner.Received(1).GetEndpoint();
}

[Fact]
public static async Task CachedValueIsReturnedAgainAfterExpiry()
{
var mockInner = new Mock<IEndPointSource>();
mockInner.Setup(x => x.GetEndpoint()).Returns(MakeTestIpEndPoint());
var inner = Substitute.For<IEndPointSource>();
inner.GetEndpoint().Returns(MakeTestIpEndPoint());

var cachedEndpoint = new CachedEndpointSource(mockInner.Object, TimeSpan.FromSeconds(1));
var cachedEndpoint = new CachedEndpointSource(inner, TimeSpan.FromSeconds(1));

var value1 = cachedEndpoint.GetEndpoint();
var value2 = cachedEndpoint.GetEndpoint();
cachedEndpoint.GetEndpoint();
cachedEndpoint.GetEndpoint();

await Task.Delay(1500);

var value3 = cachedEndpoint.GetEndpoint();
var value4 = cachedEndpoint.GetEndpoint();
cachedEndpoint.GetEndpoint();
cachedEndpoint.GetEndpoint();

mockInner.Verify(x => x.GetEndpoint(), Times.Exactly(2));
inner.Received(2).GetEndpoint();
}

[Theory]
Expand All @@ -64,7 +64,7 @@ public static async Task CachedValueIsReturnedAgainAfterExpiry()
public static void ConstructorThrowsIfCacheDurationIsInvalid(long ticks)
{
// Arrange
var inner = Mock.Of<IEndPointSource>();
var inner = Substitute.For<IEndPointSource>();
var cacheDuration = TimeSpan.FromTicks(ticks);

// Act and Assert
Expand Down
18 changes: 9 additions & 9 deletions tests/JustEat.StatsD.Tests/IStatsDTransportExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Moq;
using NSubstitute;

namespace JustEat.StatsD;

Expand Down Expand Up @@ -30,7 +30,7 @@ public static void SendThrowsIfTransportIsNullMetrics()
public static void SendThrowsIfTransportIfMetricIsNull()
{
// Arrange
var transport = Mock.Of<IStatsDTransport>();
var transport = Substitute.For<IStatsDTransport>();
string? metric = null;

// Act and Assert
Expand All @@ -41,7 +41,7 @@ public static void SendThrowsIfTransportIfMetricIsNull()
public static void SendThrowsIfMetricsIsNull()
{
// Arrange
var transport = Mock.Of<IStatsDTransport>();
var transport = Substitute.For<IStatsDTransport>();
IEnumerable<string>? metrics = null;

// Act and Assert
Expand All @@ -52,27 +52,27 @@ public static void SendThrowsIfMetricsIsNull()
public static void SendSendsStringMetric()
{
// Arrange
var transport = new Mock<IStatsDTransport>();
var transport = Substitute.For<IStatsDTransport>();
string metric = "metric";

// Act
transport.Object.Send(metric);
transport.Send(metric);

// Assert
transport.Verify((p) => p.Send(It.Ref<ArraySegment<byte>>.IsAny), Times.Once());
transport.ReceivedWithAnyArgs(1).Send(default);
}

[Fact]
public static void SendSendsStringMetrics()
{
// Arrange
var transport = new Mock<IStatsDTransport>();
var transport = Substitute.For<IStatsDTransport>();
var metrics = new[] { "a", "b", "c" };

// Act
transport.Object.Send(metrics);
transport.Send(metrics);

// Assert
transport.Verify((p) => p.Send(It.Ref<ArraySegment<byte>>.IsAny), Times.Exactly(3));
transport.ReceivedWithAnyArgs(3).Send(default);
}
}
2 changes: 1 addition & 1 deletion tests/JustEat.StatsD.Tests/JustEat.StatsD.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Moq" />
<PackageReference Include="NSubstitute" />
<PackageReference Include="Shouldly" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" />
Expand Down
5 changes: 3 additions & 2 deletions tests/JustEat.StatsD.Tests/SocketTransportTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Net;
using JustEat.StatsD.EndpointLookups;
using Moq;
using NSubstitute;

namespace JustEat.StatsD;

Expand Down Expand Up @@ -68,7 +68,8 @@ public static void SocketTransportIsNoopForEmptyArray()
[Fact]
public static void SocketTransportIsNoopForNullEndpoint()
{
var endpointSource = Mock.Of<IEndPointSource>();
var endpointSource = Substitute.For<IEndPointSource>();
endpointSource.GetEndpoint().Returns(null as EndPoint);

using var transport = new SocketTransport(endpointSource, SocketProtocol.IP);
transport.Send("teststat:1|c");
Expand Down
36 changes: 18 additions & 18 deletions tests/JustEat.StatsD.Tests/StatsDPublisherTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Moq;
using NSubstitute;

namespace JustEat.StatsD;

Expand All @@ -8,14 +8,14 @@ public static class StatsDPublisherTests
public static void Decrement_Sends_Multiple_Metrics()
{
// Arrange
var mock = new Mock<IStatsDTransport>();
var transport = Substitute.For<IStatsDTransport>();

var config = new StatsDConfiguration
{
Prefix = "red",
};

using (var publisher = new StatsDPublisher(config, mock.Object))
using (var publisher = new StatsDPublisher(config, transport))
{
// Act
publisher.Decrement(10, "black");
Expand All @@ -27,21 +27,21 @@ public static void Decrement_Sends_Multiple_Metrics()
}

// Assert
mock.Verify((p) => p.Send(It.Ref<ArraySegment<byte>>.IsAny), Times.Exactly(8));
transport.ReceivedWithAnyArgs(8).Send(default);
}

[Fact]
public static void Increment_Sends_Multiple_Metrics()
{
// Arrange
var mock = new Mock<IStatsDTransport>();
var transport = Substitute.For<IStatsDTransport>();

var config = new StatsDConfiguration
{
Prefix = "red",
};

using (var publisher = new StatsDPublisher(config, mock.Object))
using (var publisher = new StatsDPublisher(config, transport))
{
// Act
publisher.Increment(10, "black");
Expand All @@ -53,21 +53,21 @@ public static void Increment_Sends_Multiple_Metrics()
}

// Assert
mock.Verify((p) => p.Send(It.Ref<ArraySegment<byte>>.IsAny), Times.Exactly(8));
transport.ReceivedWithAnyArgs(8).Send(default);
}

[Fact]
public static void Metrics_Sent_If_Tags_Are_Null()
{
// Arrange
var mock = new Mock<IStatsDTransport>();
var transport = Substitute.For<IStatsDTransport>();

var config = new StatsDConfiguration
{
Prefix = "red",
};

using (var publisher = new StatsDPublisher(config, mock.Object))
using (var publisher = new StatsDPublisher(config, transport))
{
// Act
publisher.Increment(10, 1.0, "black");
Expand All @@ -76,14 +76,14 @@ public static void Metrics_Sent_If_Tags_Are_Null()
}

// Assert
mock.Verify((p) => p.Send(It.Ref<ArraySegment<byte>>.IsAny), Times.Exactly(3));
transport.ReceivedWithAnyArgs(3).Send(default);
}

[Fact]
public static void Metrics_Not_Sent_If_Array_Is_Null_Or_Empty()
{
// Arrange
var mock = new Mock<IStatsDTransport>();
var transport = Substitute.For<IStatsDTransport>();
var config = new StatsDConfiguration();
var anyValidTags = new Dictionary<string, string?>
{
Expand All @@ -92,7 +92,7 @@ public static void Metrics_Not_Sent_If_Array_Is_Null_Or_Empty()
["lorem"] = "ipsum",
};

using (var publisher = new StatsDPublisher(config, mock.Object))
using (var publisher = new StatsDPublisher(config, transport))
{
// Act
#nullable disable
Expand Down Expand Up @@ -120,33 +120,33 @@ public static void Metrics_Not_Sent_If_Array_Is_Null_Or_Empty()
}

// Assert
mock.Verify((p) => p.Send(It.IsAny<ArraySegment<byte>>()), Times.Never());
transport.DidNotReceiveWithAnyArgs().Send(default);
}

[Fact]
public static void Metrics_Not_Sent_If_No_Metrics()
{
// Arrange
var mock = new Mock<IStatsDTransport>();
var transport = Substitute.For<IStatsDTransport>();
var config = new StatsDConfiguration();

using (var publisher = new StatsDPublisher(config, mock.Object))
using (var publisher = new StatsDPublisher(config, transport))
{
// Act
publisher.Decrement(1, 0, new[] { "foo" });
publisher.Increment(1, 0, new[] { "bar" });
}

// Assert
mock.Verify((p) => p.Send(It.IsAny<ArraySegment<byte>>()), Times.Never());
transport.DidNotReceiveWithAnyArgs().Send(default);
}

[Fact]
public static void Constructor_Throws_If_Configuration_Is_Null()
{
// Arrange
StatsDConfiguration? configuration = null;
var transport = Mock.Of<IStatsDTransport>();
var transport = Substitute.For<IStatsDTransport>();

// Act and Assert
Assert.Throws<ArgumentNullException>(
Expand Down Expand Up @@ -175,7 +175,7 @@ public static void Constructor_Does_Not_Throw_If_Tags_Formatter_Is_Null()
{
TagsFormatter = null!,
};
var transport = Mock.Of<IStatsDTransport>();
var transport = Substitute.For<IStatsDTransport>();

// Act
using var publisher = new StatsDPublisher(configuration, transport);
Expand Down
Loading