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

Replace Moq for NSubstitute #370

Merged
merged 2 commits into from
Aug 11, 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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't wait for them to fix my issue to get rid of this damn file 😂

"dotnet.defaultSolution": "AwsWatchman.sln"
}
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="NSubstitute" Version="5.0.0" />
<PackageVersion Include="NUnit" Version="3.13.3" />
<PackageVersion Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageVersion Include="Moq" Version="4.18.4" />
<PackageVersion Include="StructureMap" Version="4.7.1" />
<PackageVersion Include="System.Configuration.ConfigurationManager" Version="6.0.1" />
</ItemGroup>
Expand Down
38 changes: 19 additions & 19 deletions Watchman.AwsResources.Tests/Services/Alb/AlbSourceTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Amazon.ElasticLoadBalancingV2;
using Amazon.ElasticLoadBalancingV2.Model;
using Moq;
using NSubstitute;
using NUnit.Framework;
using Watchman.AwsResources.Services.Alb;

Expand Down Expand Up @@ -51,28 +51,28 @@ public void Setup()
}
};

var elbMock = new Mock<IAmazonElasticLoadBalancingV2>();
elbMock.Setup(s => s.DescribeLoadBalancersAsync(
It.Is<DescribeLoadBalancersRequest>(r => r.Marker == null),
It.IsAny<CancellationToken>()
)).ReturnsAsync(_firstPage);
var elbMock = Substitute.For<IAmazonElasticLoadBalancingV2>();
elbMock.DescribeLoadBalancersAsync(
Arg.Is<DescribeLoadBalancersRequest>(r => r.Marker == null),
Arg.Any<CancellationToken>()
).Returns(_firstPage);

elbMock.Setup(s => s.DescribeLoadBalancersAsync(
It.Is<DescribeLoadBalancersRequest>(r => r.Marker == "token-1"),
It.IsAny<CancellationToken>()
)).ReturnsAsync(_secondPage);
elbMock.DescribeLoadBalancersAsync(
Arg.Is<DescribeLoadBalancersRequest>(r => r.Marker == "token-1"),
Arg.Any<CancellationToken>()
).Returns(_secondPage);

elbMock.Setup(s => s.DescribeLoadBalancersAsync(
It.Is<DescribeLoadBalancersRequest>(r => r.Marker == "token-2"),
It.IsAny<CancellationToken>()
)).ReturnsAsync(_thirdPage);
elbMock.DescribeLoadBalancersAsync(
Arg.Is<DescribeLoadBalancersRequest>(r => r.Marker == "token-2"),
Arg.Any<CancellationToken>()
).Returns(_thirdPage);

elbMock.Setup(s => s.DescribeLoadBalancersAsync(
It.Is<DescribeLoadBalancersRequest>(r => r.Marker == "token-3"),
It.IsAny<CancellationToken>()
)).ReturnsAsync(_fourthPage);
elbMock.DescribeLoadBalancersAsync(
Arg.Is<DescribeLoadBalancersRequest>(r => r.Marker == "token-3"),
Arg.Any<CancellationToken>()
).Returns(_fourthPage);

_albSource = new AlbSource(elbMock.Object);
_albSource = new AlbSource(elbMock);
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Amazon.AutoScaling;
using Amazon.AutoScaling.Model;
using Moq;
using NSubstitute;
using NUnit.Framework;
using Watchman.AwsResources.Services.AutoScaling;

Expand All @@ -11,7 +11,7 @@ public class AutoScalingGroupSourceTests
{
private IAmazonAutoScaling CreateAutoScalingClientStub(params DescribeAutoScalingGroupsResponse[] pages)
{
var clientStub = new Mock<IAmazonAutoScaling>();
var clientStub = Substitute.For<IAmazonAutoScaling>();

var tokens = Enumerable
.Range(10000, pages.Length - 1)
Expand All @@ -30,14 +30,14 @@ private IAmazonAutoScaling CreateAutoScalingClientStub(params DescribeAutoScalin
nextToken = page.Token;

clientStub
.Setup(x => x.DescribeAutoScalingGroupsAsync(
It.Is<DescribeAutoScalingGroupsRequest>(r => r.NextToken == currentPageToken),
It.IsAny<CancellationToken>())
.DescribeAutoScalingGroupsAsync(
Arg.Is<DescribeAutoScalingGroupsRequest>(r => r.NextToken == currentPageToken),
Arg.Any<CancellationToken>()
)
.Returns(Task.FromResult(page.Result));
.Returns(page.Result);
}

return clientStub.Object;
return clientStub;
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Amazon.CloudFront;
using Amazon.CloudFront.Model;
using Moq;
using NSubstitute;
using NUnit.Framework;
using Watchman.AwsResources.Services.CloudFront;

Expand All @@ -17,28 +17,28 @@ public class CloudFrontSourceTests
[SetUp]
public void SetUp()
{
var stepCloudFrontMock = new Mock<IAmazonCloudFront>();
var stepCloudFrontMock = Substitute.For<IAmazonCloudFront>();

_firstPage = BuildListResponse(1);
_secondPage = BuildListResponse(2);
_thirdPage = BuildListResponse(null);

stepCloudFrontMock.Setup(c => c.ListDistributionsAsync(
It.Is<ListDistributionsRequest>(r => r.Marker == null),
It.IsAny<CancellationToken>()))
.ReturnsAsync(_firstPage);
stepCloudFrontMock.ListDistributionsAsync(
Arg.Is<ListDistributionsRequest>(r => r.Marker == null),
Arg.Any<CancellationToken>())
.Returns(_firstPage);

stepCloudFrontMock.Setup(c => c.ListDistributionsAsync(
It.Is<ListDistributionsRequest>(r => r.Marker == "token-1"),
It.IsAny<CancellationToken>()))
.ReturnsAsync(_secondPage);
stepCloudFrontMock.ListDistributionsAsync(
Arg.Is<ListDistributionsRequest>(r => r.Marker == "token-1"),
Arg.Any<CancellationToken>())
.Returns(_secondPage);

stepCloudFrontMock.Setup(c => c.ListDistributionsAsync(
It.Is<ListDistributionsRequest>(r => r.Marker == "token-2"),
It.IsAny<CancellationToken>()))
.ReturnsAsync(_thirdPage);
stepCloudFrontMock.ListDistributionsAsync(
Arg.Is<ListDistributionsRequest>(r => r.Marker == "token-2"),
Arg.Any<CancellationToken>())
.Returns(_thirdPage);

_source = new CloudFrontSource(stepCloudFrontMock.Object);
_source = new CloudFrontSource(stepCloudFrontMock);
}

[Test]
Expand Down
33 changes: 17 additions & 16 deletions Watchman.AwsResources.Tests/Services/Dax/DaxSourceTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Amazon.DAX;
using System.Linq.Expressions;
using Amazon.DAX;
using Amazon.DAX.Model;
using Moq;
using NSubstitute;
using NUnit.Framework;
using Watchman.AwsResources.Services.Dax;

Expand All @@ -17,7 +18,7 @@ public class DaxSourceTests
[SetUp]
public void SetUp()
{
var stepClusterMock = new Mock<IAmazonDAX>();
var stepClusterMock = Substitute.For<IAmazonDAX>();

_firstPage = new DescribeClustersResponse
{
Expand Down Expand Up @@ -55,22 +56,22 @@ public void SetUp()
}
};

stepClusterMock.Setup(c => c.DescribeClustersAsync(
It.Is<DescribeClustersRequest>(r => r.NextToken == null)
, It.IsAny<CancellationToken>()))
.ReturnsAsync(_firstPage);
stepClusterMock.DescribeClustersAsync(
Arg.Is<DescribeClustersRequest>(r => r.NextToken == null)
, Arg.Any<CancellationToken>())
.Returns(_firstPage);

stepClusterMock.Setup(c => c.DescribeClustersAsync(
It.Is<DescribeClustersRequest>(r => r.NextToken == "token-1")
, It.IsAny<CancellationToken>()))
.ReturnsAsync(_secondPage);
stepClusterMock.DescribeClustersAsync(
Arg.Is<DescribeClustersRequest>(r => r.NextToken == "token-1")
, Arg.Any<CancellationToken>())
.Returns(_secondPage);

stepClusterMock.Setup(c => c.DescribeClustersAsync(
It.Is<DescribeClustersRequest>(r => r.NextToken == "token-2")
, It.IsAny<CancellationToken>()))
.ReturnsAsync(_thirdPage);
stepClusterMock.DescribeClustersAsync(
Arg.Is<DescribeClustersRequest>(r => r.NextToken == "token-2")
, Arg.Any<CancellationToken>())
.Returns(_thirdPage);

_source = new DaxSource(stepClusterMock.Object);
_source = new DaxSource(stepClusterMock);
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.Model;
using Moq;
using NSubstitute;
using NSubstitute.ExceptionExtensions;
using NUnit.Framework;
using Watchman.AwsResources.Services.DynamoDb;

Expand Down Expand Up @@ -49,26 +50,26 @@ private TableDescriptionSource SetupPagingTest()
}
};

var dynamoDbMock = new Mock<IAmazonDynamoDB>();
dynamoDbMock.Setup(s => s.ListTablesAsync(
It.Is<string>(r => r == null), It.IsAny<CancellationToken>()
)).ReturnsAsync(_firstPage);
var dynamoDbMock = Substitute.For<IAmazonDynamoDB>();
dynamoDbMock.ListTablesAsync(
Arg.Is<string>(r => r == null), Arg.Any<CancellationToken>()
).Returns(_firstPage);

dynamoDbMock.Setup(s => s.ListTablesAsync(
It.Is<string>(r => r == firstTableName),
It.IsAny<CancellationToken>()
)).ReturnsAsync(_secondPage);
dynamoDbMock.ListTablesAsync(
Arg.Is<string>(r => r == firstTableName),
Arg.Any<CancellationToken>()
).Returns(_secondPage);

dynamoDbMock.Setup(s => s.ListTablesAsync(
It.Is<string>(r => r == secondTableName),
It.IsAny<CancellationToken>()
)).ReturnsAsync(_thirdPage);
dynamoDbMock.ListTablesAsync(
Arg.Is<string>(r => r == secondTableName),
Arg.Any<CancellationToken>()
).Returns(_thirdPage);

dynamoDbMock.Setup(s => s.DescribeTableAsync(It.Is<string>(r => r == secondTableName),
It.IsAny<CancellationToken>()))
.ReturnsAsync(describeSecondTableResponse);
dynamoDbMock.DescribeTableAsync(Arg.Is<string>(r => r == secondTableName),
Arg.Any<CancellationToken>())
.Returns(describeSecondTableResponse);

return new TableDescriptionSource(dynamoDbMock.Object);
return new TableDescriptionSource(dynamoDbMock);
}

[Test]
Expand Down Expand Up @@ -145,28 +146,27 @@ public async Task GetResourceAsync_ReturnsNullIfNotInList()
[Test]
public async Task GetResourceAsync_ReturnsNullIfSdkThrowsNotFound()
{
var dynamoDbFake = new Mock<IAmazonDynamoDB>();
var dynamoDbFake = Substitute.For<IAmazonDynamoDB>();

dynamoDbFake
.Setup(s => s.ListTablesAsync(
It.Is<string>(r => r == null), It.IsAny<CancellationToken>()
))
.ReturnsAsync(new ListTablesResponse()
.ListTablesAsync(
Arg.Is<string>(r => r == null), Arg.Any<CancellationToken>()
)
.Returns(new ListTablesResponse()
{
TableNames = new List<string>() { "banana" }
});

dynamoDbFake
.Setup(s => s.DescribeTableAsync("banana", It.IsAny<CancellationToken>()))
.Throws(new ResourceNotFoundException("Table not found"))
//so we know we actually threw this error
.Verifiable();
.DescribeTableAsync("banana", Arg.Any<CancellationToken>()).Throws(new ResourceNotFoundException("Table not found"));

var sut = new TableDescriptionSource(dynamoDbFake.Object);
var sut = new TableDescriptionSource(dynamoDbFake);

var result = await sut.GetResourceAsync("banana");

dynamoDbFake.Verify();
await dynamoDbFake
.Received()
.DescribeTableAsync("banana", Arg.Any<CancellationToken>());

Assert.Null(result);
}
Expand Down
30 changes: 15 additions & 15 deletions Watchman.AwsResources.Tests/Services/Elb/ElbSourceTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Amazon.ElasticLoadBalancing;
using Amazon.ElasticLoadBalancing.Model;
using Moq;
using NSubstitute;
using NUnit.Framework;
using Watchman.AwsResources.Services.Elb;

Expand Down Expand Up @@ -42,23 +42,23 @@ public void Setup()
}
};

var elbMock = new Mock<IAmazonElasticLoadBalancing>();
elbMock.Setup(s => s.DescribeLoadBalancersAsync(
It.Is<DescribeLoadBalancersRequest>(r => r.Marker == null),
It.IsAny<CancellationToken>()
)).ReturnsAsync(_firstPage);
var elbMock = Substitute.For<IAmazonElasticLoadBalancing>();
elbMock.DescribeLoadBalancersAsync(
Arg.Is<DescribeLoadBalancersRequest>(r => r.Marker == null),
Arg.Any<CancellationToken>()
).Returns(_firstPage);

elbMock.Setup(s => s.DescribeLoadBalancersAsync(
It.Is<DescribeLoadBalancersRequest>(r => r.Marker == "token-1"),
It.IsAny<CancellationToken>()
)).ReturnsAsync(_secondPage);
elbMock.DescribeLoadBalancersAsync(
Arg.Is<DescribeLoadBalancersRequest>(r => r.Marker == "token-1"),
Arg.Any<CancellationToken>()
).Returns(_secondPage);

elbMock.Setup(s => s.DescribeLoadBalancersAsync(
It.Is<DescribeLoadBalancersRequest>(r => r.Marker == "token-2"),
It.IsAny<CancellationToken>()
)).ReturnsAsync(_thirdPage);
elbMock.DescribeLoadBalancersAsync(
Arg.Is<DescribeLoadBalancersRequest>(r => r.Marker == "token-2"),
Arg.Any<CancellationToken>()
).Returns(_thirdPage);

_elbSource = new ElbSource(elbMock.Object);
_elbSource = new ElbSource(elbMock);
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Amazon.CloudWatch;
using Amazon.CloudWatch.Model;
using Moq;
using NSubstitute;
using NUnit.Framework;
using Watchman.AwsResources.Services.Kinesis;

Expand Down Expand Up @@ -75,23 +75,23 @@ public void Setup()
}
};

var cloudWatchMock = new Mock<IAmazonCloudWatch>();
cloudWatchMock.Setup(s => s.ListMetricsAsync(
It.Is<ListMetricsRequest>(r => r.MetricName == "GetRecords.IteratorAgeMilliseconds" && r.NextToken == null),
It.IsAny<CancellationToken>()))
.ReturnsAsync(_firstPage);
var cloudWatchMock = Substitute.For<IAmazonCloudWatch>();
cloudWatchMock.ListMetricsAsync(
Arg.Is<ListMetricsRequest>(r => r.MetricName == "GetRecords.IteratorAgeMilliseconds" && r.NextToken == null),
Arg.Any<CancellationToken>())
.Returns(_firstPage);

cloudWatchMock.Setup(s => s.ListMetricsAsync(
It.Is<ListMetricsRequest>(r => r.MetricName == "GetRecords.IteratorAgeMilliseconds" && r.NextToken == "token-1"),
It.IsAny<CancellationToken>()))
.ReturnsAsync(_secondPage);
cloudWatchMock.ListMetricsAsync(
Arg.Is<ListMetricsRequest>(r => r.MetricName == "GetRecords.IteratorAgeMilliseconds" && r.NextToken == "token-1"),
Arg.Any<CancellationToken>())
.Returns(_secondPage);

cloudWatchMock.Setup(s => s.ListMetricsAsync(
It.Is<ListMetricsRequest>(r => r.MetricName == "GetRecords.IteratorAgeMilliseconds" && r.NextToken == "token-2"),
It.IsAny<CancellationToken>()))
.ReturnsAsync(_thirdPage);
cloudWatchMock.ListMetricsAsync(
Arg.Is<ListMetricsRequest>(r => r.MetricName == "GetRecords.IteratorAgeMilliseconds" && r.NextToken == "token-2"),
Arg.Any<CancellationToken>())
.Returns(_thirdPage);

_streamSource = new KinesisStreamSource(cloudWatchMock.Object);
_streamSource = new KinesisStreamSource(cloudWatchMock);
}

[Test]
Expand Down
Loading