Skip to content

test: drop moq #140

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

Merged
merged 2 commits into from
Aug 13, 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using Microsoft.Extensions.DependencyInjection;

namespace Cnblogs.Architecture.Ddd.EventBus.Abstractions;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.10" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="2.20.0" />
<PackageReference Include="MongoDB.Driver" Version="2.21.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<PackageReference Include="Cnblogs.Serilog.Extensions" Version="1.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.9" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.10" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Moq;
using NSubstitute;
using NSubstitute.ExceptionExtensions;

namespace Cnblogs.Architecture.IntegrationTests;

Expand All @@ -20,13 +21,13 @@ public async Task EventBus_PublishEvent_SuccessAsync()
// Arrange
const string data = "hello";
var builder = new WebApplicationFactory<Program>();
var eventBusMock = new Mock<IEventBusProvider>();
var eventBusMock = Substitute.For<IEventBusProvider>();
builder = builder.WithWebHostBuilder(
b => b.ConfigureServices(
services =>
{
services.RemoveAll<IEventBusProvider>();
services.AddScoped<IEventBusProvider>(_ => eventBusMock.Object);
services.AddScoped<IEventBusProvider>(_ => eventBusMock);
}));

// Act
Expand All @@ -39,9 +40,9 @@ public async Task EventBus_PublishEvent_SuccessAsync()
// Assert
response.Should().BeSuccessful();
content.Should().BeNullOrEmpty();
eventBusMock.Verify(
x => x.PublishAsync(It.IsAny<string>(), It.Is<TestIntegrationEvent>(t => t.Message == data)),
Times.Once);
await eventBusMock.Received(1).PublishAsync(
Arg.Any<string>(),
Arg.Is<TestIntegrationEvent>(t => t.Message == data));
}

[Fact]
Expand All @@ -50,21 +51,21 @@ public async Task EventBus_Downgrading_DowngradeAsync()
// Arrange
const string data = "hello";
var builder = new WebApplicationFactory<Program>();
var eventBusMock = new Mock<IEventBusProvider>();
var eventBusMock = Substitute.For<IEventBusProvider>();
builder = builder.WithWebHostBuilder(
b => b.ConfigureServices(
services =>
{
services.RemoveAll<IEventBusProvider>();
services.AddScoped<IEventBusProvider>(_ => eventBusMock.Object);
services.AddScoped<IEventBusProvider>(_ => eventBusMock);
services.Configure<EventBusOptions>(
o =>
{
o.FailureCountBeforeDowngrade = 1;
o.DowngradeInterval = 3000;
});
}));
eventBusMock.Setup(x => x.PublishAsync(It.IsAny<string>(), It.IsAny<IntegrationEvent>()))
eventBusMock.PublishAsync(Arg.Any<string>(), Arg.Any<IntegrationEvent>())
.ThrowsAsync(new InvalidOperationException());

// Act
Expand All @@ -77,9 +78,9 @@ public async Task EventBus_Downgrading_DowngradeAsync()
// Assert
response.Should().BeSuccessful();
content.Should().BeNullOrEmpty();
eventBusMock.Verify(
x => x.PublishAsync(It.IsAny<string>(), It.Is<TestIntegrationEvent>(t => t.Message == data)),
Times.Exactly(2));
await eventBusMock.Received(2).PublishAsync(
Arg.Any<string>(),
Arg.Is<TestIntegrationEvent>(t => t.Message == data));
}

[Fact]
Expand All @@ -88,13 +89,13 @@ public async Task EventBus_MaximumBufferSizeReached_ThrowAsync()
// Arrange
const string data = "hello";
var builder = new WebApplicationFactory<Program>();
var eventBusMock = new Mock<IEventBusProvider>();
var eventBusMock = Substitute.For<IEventBusProvider>();
builder = builder.WithWebHostBuilder(
b => b.ConfigureServices(
services =>
{
services.RemoveAll<IEventBusProvider>();
services.AddScoped<IEventBusProvider>(_ => eventBusMock.Object);
services.AddScoped<IEventBusProvider>(_ => eventBusMock);
services.Configure<EventBusOptions>(
o =>
{
Expand All @@ -103,7 +104,7 @@ public async Task EventBus_MaximumBufferSizeReached_ThrowAsync()
o.DowngradeInterval = 3000;
});
}));
eventBusMock.Setup(x => x.PublishAsync(It.IsAny<string>(), It.IsAny<IntegrationEvent>()))
eventBusMock.PublishAsync(Arg.Any<string>(), Arg.Any<IntegrationEvent>())
.ThrowsAsync(new InvalidOperationException());
var client = builder.CreateClient();
await client.PostAsJsonAsync(
Expand All @@ -123,13 +124,13 @@ public async Task EventBus_MaximumBatchSize_OneBatchAsync()
// Arrange
const string data = "hello";
var builder = new WebApplicationFactory<Program>();
var eventBusMock = new Mock<IEventBusProvider>();
var eventBusMock = Substitute.For<IEventBusProvider>();
builder = builder.WithWebHostBuilder(
b => b.ConfigureServices(
services =>
{
services.RemoveAll<IEventBusProvider>();
services.AddScoped<IEventBusProvider>(_ => eventBusMock.Object);
services.AddScoped<IEventBusProvider>(_ => eventBusMock);
services.Configure<EventBusOptions>(
o =>
{
Expand All @@ -149,7 +150,7 @@ public async Task EventBus_MaximumBatchSize_OneBatchAsync()
await Task.Delay(1000);

// Assert
eventBusMock.Verify(x => x.PublishAsync(It.IsAny<string>(), It.IsAny<IntegrationEvent>()), Times.Once);
await eventBusMock.Received(1).PublishAsync(Arg.Any<string>(), Arg.Any<IntegrationEvent>());
}

[Fact]
Expand All @@ -158,38 +159,38 @@ public async Task EventBus_DowngradeThenRecover_RecoverAsync()
// Arrange
const string data = "hello";
var builder = new WebApplicationFactory<Program>();
var eventBusMock = new Mock<IEventBusProvider>();
var eventBusMock = Substitute.For<IEventBusProvider>();
builder = builder.WithWebHostBuilder(
b => b.ConfigureServices(
services =>
{
services.RemoveAll<IEventBusProvider>();
services.AddScoped<IEventBusProvider>(_ => eventBusMock.Object);
services.AddScoped<IEventBusProvider>(_ => eventBusMock);
services.Configure<EventBusOptions>(
o =>
{
o.FailureCountBeforeDowngrade = 1;
o.DowngradeInterval = 4000;
});
}));
eventBusMock.Setup(x => x.PublishAsync(It.IsAny<string>(), It.IsAny<IntegrationEvent>()))
eventBusMock.PublishAsync(Arg.Any<string>(), Arg.Any<IntegrationEvent>())
.ThrowsAsync(new InvalidOperationException());
await builder.CreateClient().PostAsJsonAsync(
"/api/v1/strings",
new CreatePayload(false, data));
await Task.Delay(1000); // failed, now it is downgraded

// Act
eventBusMock.Reset();
eventBusMock.PublishAsync(Arg.Any<string>(), Arg.Any<IntegrationEvent>()).Returns(Task.CompletedTask);
eventBusMock.ClearReceivedCalls();
await Task.Delay(2000); // recover
await builder.CreateClient().PostAsJsonAsync(
"/api/v1/strings",
new CreatePayload(false, data));
await Task.Delay(1000);

// Assert
eventBusMock.Verify(
x => x.PublishAsync(It.IsAny<string>(), It.Is<TestIntegrationEvent>(t => t.Message == data)),
Times.Exactly(2));
await eventBusMock.Received(2)
.PublishAsync(Arg.Any<string>(), Arg.Is<TestIntegrationEvent>(t => t.Message == data));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.11.0" />
<PackageReference Include="Moq" Version="4.20.2" />
<PackageReference Include="NSubstitute" Version="5.0.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.10" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
Expand Down
Loading