Skip to content

Commit

Permalink
fix: improved TrackUserLoginCommandHandler test
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianKuesters committed Jul 25, 2024
1 parent ff19007 commit 914266a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/core/Wemogy.CQRS.UnitTests/DependencyInjectionTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -25,16 +26,15 @@ public async Task CallingAddCQRSMultipleTimesInDifferentAssembliesShouldWork()
var commands = serviceProvider.GetRequiredService<ICommands>();
var helloAssemblyACommand = new PrintHelloAssemblyACommand();
var helloAssemblyBCommand = new PrintHelloAssemblyBCommand();
var trackUserLoginCommand = new TrackUserLoginCommand("test-user-id");
TrackUserLoginCommandHandler.ResetCallCount();
var trackUserLoginCommand = new TrackUserLoginCommand(Guid.NewGuid().ToString());

// Act
var trackUserLoginCommandException = await Record.ExceptionAsync(() => commands.RunAsync(trackUserLoginCommand));
var helloAssemblyACommandException = await Record.ExceptionAsync(() => commands.RunAsync(helloAssemblyACommand));
var helloAssemblyBCommandException = await Record.ExceptionAsync(() => commands.RunAsync(helloAssemblyBCommand));

// Assert
TrackUserLoginCommandHandler.CallCount.Should().Be(1);
TrackUserLoginCommandHandler.ExecutedCount[trackUserLoginCommand.UserId].Should().Be(1);
trackUserLoginCommandException.Should().BeNull();
PrintHelloAssemblyACommandHandler.CallCount.Should().Be(1);
helloAssemblyACommandException.Should().BeNull();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Wemogy.CQRS.Commands.Abstractions;

namespace Wemogy.CQRS.UnitTests.TestApplication.Commands.TrackUserLogin;

public class TrackUserLoginCommandHandler : ICommandHandler<TrackUserLoginCommand>
{
public static int CallCount { get; private set; }

public static void ResetCallCount()
{
CallCount = 0;
}
public static Dictionary<string, int> ExecutedCount { get; } = new ();

public Task HandleAsync(TrackUserLoginCommand command)
{
CallCount++;
if (ExecutedCount.TryGetValue(command.UserId, out var count))
{
ExecutedCount[command.UserId] = count + 1;
}
else
{
ExecutedCount[command.UserId] = 1;
}

return Task.CompletedTask;
}
}

0 comments on commit 914266a

Please sign in to comment.