Provides testing helpers for Rebus.
rebus-org / rebus.testhelpers Goto Github PK
View Code? Open in Web Editor NEW:bus: Test helpers for Rebus (i.e. fake bus, saga fixture, etc.)
Home Page: https://mookid.dk/category/rebus
License: Other
:bus: Test helpers for Rebus (i.e. fake bus, saga fixture, etc.)
Home Page: https://mookid.dk/category/rebus
License: Other
Provides testing helpers for Rebus.
We ran into an issue using Rebus.TestHelpers.SagaFixture
in combination with saga data that relies on a custom ISagaSerializer
.
The Clone
method of InMemorySagaStorage
uses JsonConvert.SerializeObject
and DeserializeObject
instead of a configured ISagaSerializer
.
We found this surprising for two reasons:
Newtonsoft.Json.JsonConverter
configured in the JsonSerializerSettings
). From the perspective of our tests, the saga store would silently revert to default values for the fields that it couldn't deserialize.SagaFixture
can succeed while the real application fails with subtle and hard to track down serialization-deserialization round-tripping issues.For our particular case, we have worked around the issue by accessing InMemorySagaStorage._serializerSettings
via reflection to add JsonConverter
that we need. This is obviously not a great solution because it relies on a very private implementation detail of SagaFixture
:(
Hi there ππ½ it's my first time writing an issue on GitHub! Thanks for the open space and I hope I'm doing it right π
So some of my DTOs have looping references and it's not a problem as I configured Rebus to use NewtonSoft.Json.
I have upgraded to Rebus 7.0 and, therefore to Rebus.TextHelpers 8.0. I've read that Rebus now uses System.Text.Json
as default Json serializer, therefore FakeBus as well. The problem I have now is that System.Text.Json can't handle my DTOs. I looked on the Wiki but I could not find a way to set the JsonSerializer of FakeBus.
I just want to restate that the application is running fine with the new Rebus, my only problem is with Unit Tests.
Do you have any suggestion?
Here I've pasted some code snippets.
public class ChangeParentCommand {
public ChangeParentCommand(Guid processingId) {
ProcessingId = processingId;
}
public Guid ProcessingId { get; set; }
[JsonProperty(IsReference = true,
ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
ItemReferenceLoopHandling = ReferenceLoopHandling.Serialize,
TypeNameHandling = TypeNameHandling.All
)]
public ICollection<ParentDto> Parents { get; set; } = new List<ParentDto>();
}
[JsonObject(IsReference = true, ItemTypeNameHandling = TypeNameHandling.All)]
public class Parent {
[JsonProperty(IsReference = true,
ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
ItemReferenceLoopHandling = ReferenceLoopHandling.Serialize,
TypeNameHandling = TypeNameHandling.All
)]
public ICollection<ChildDto> Children { get; set; } = new List<ChildDto>();
}
[JsonObject(IsReference = true, ItemTypeNameHandling = TypeNameHandling.All)]
public class Child {
[JsonProperty(IsReference = true,
ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
ItemReferenceLoopHandling = ReferenceLoopHandling.Serialize,
TypeNameHandling = TypeNameHandling.All
)]
public ICollection<ParentDto> Parents { get; set; } = new List<ParentDto>();
}
// on my unit tests:
[Fact]
public void HandleChangeParentCommand() {
var message = new ChangeParentCommand(Guid.NewGuid()) {
Parents = someData.ToList(),
};
var fakeBus = new FakeBus();
using var fixture = SagaFixture.For(() => new ChangeParentSaga(fakeBus));
fixture.Add(new ChangeParentSagaData {
ProcessingStepTrackingId = message.SagaCorrelationId,
Step = "Some Step",
}
);
fixture.Deliver(message); // error on sending
var data = fixture.Data.OfType<ChangeParentSagaData>();
data.Should().NotBeEmpty();
}
# error thrown
System.Text.Json.JsonException
A possible object cycle was detected. This can either be due to a cycle or
if the object depth is larger than the maximum allowed depth of 64. Consider
using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles.
(what the headline says)
Hi
It seems that you have an error in FakeRoutingApi.Defer method.
The current code:
var messageSentToDestination = _factory.CreateEventGeneric(
typeof(MessageSentToDestination<>),
explicitlyRoutedMessage.GetType(),
destinationAddress,
delay,
explicitlyRoutedMessage,
optionalHeaders
);
There should be typeof(MessageDeferredToDestination<>) instead of typeof(MessageSentToDestination<>).
I noticed that my shiny saga test broke while updating to Rebus 8. I have a saga test where i test an Ifailed handler (the regular handler is not part of the saga) and i see an handler not found exception :(
Testing an ifailed where the regular handler is part of the saga does work tho. With testing i mean using the deliverFailed method.
This worked with Rebus 7.
Hello,
while trying to unit test the saga conflict resolution I encountered a strange behavior. Could you please help me to clarify?
This behavior is also reproducible with the unit test provided in this repository, therefore no custom code is needed.
TestSagaFixture_ResolveConflicts.CanSimulateConflict()
The saga ConflictSagaHandler
provides a ResolveConflict
method but this method is never called when calling the mentioned test.
Saga flow is undisturbed and all the assertions pass.
From what I gather the ResolveConflict
method should be called, or am I missing something?
I can understand why the FakeMessageContext
doesn't contain a valid IncomingStepContext
, but that unfortunately means that MessageContextExtensions.GetCancellationToken()
doesn't work with the fake context. Because that's an extension method, it's also kind of difficult to get around this by mocking the message context.
Do you have any suggestions for testing handlers that use the cancellation token?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. πππ
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google β€οΈ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.