Skip to content

Commit

Permalink
Added example of Snapshot tests for catching broken contract in events
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardudycz committed May 17, 2024
1 parent 9842d4c commit ef8d994
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Verify.Xunit" Version="24.2.0" />
<PackageReference Include="xunit" Version="2.8.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.0">
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
ShoppingCartId: Guid_1,
ClientId: anonymised,
ConfirmedAt: DateTimeOffset_1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
ShoppingCartId: Guid_1,
ConfirmedAt: DateTimeOffset_1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Runtime.CompilerServices;

namespace EventsVersioning.Tests.SnapshotTesting;

public class EventsSnapshotTests
{
private record ShoppingCartConfirmed(
Guid ShoppingCartId,
string? ClientId,
DateTimeOffset ConfirmedAt
);

[Fact]
public Task ShoppingCartConfirmed_WithCompleteData_IsCompatible()
{
var @event = new ShoppingCartConfirmed(Guid.NewGuid(), "Oskar Dudycz", DateTimeOffset.UtcNow);
return Verify(@event);
}

[Fact]
public Task ShoppingCartConfirmed_WithOnlyRequiredData_IsCompatible()
{
var @event = new ShoppingCartConfirmed(Guid.NewGuid(), null, DateTimeOffset.UtcNow);
return Verify(@event);
}
}
// note this is optional, if you really need to
// This is just showing that you can
public static class StaticSettingsUsage
{
[ModuleInitializer]
public static void Initialize() =>
VerifierSettings.AddScrubber(text => text.Replace("Oskar Dudycz", "anonymised"));
}

0 comments on commit ef8d994

Please sign in to comment.