How to write MassTransitStateMachine unit tests?

2019-07-10 00:46发布

问题:

I'm finally starting to leverage the excellent Automatonymous components within MassTransit, and I'd like to TDD my way through my new state machines.

After reading over the MT docs here (http://masstransit-project.com/MassTransit/advanced/sagas/automatonymous.html) and spending some time Googling, I found unit tests right in the MT/Automatonymous Git repo that looked like the way to go:

  • https://github.com/MassTransit/Automatonymous/blob/master/src/Automatonymous.Tests/Condition_Specs.cs#L21

In particular, the _machine.RaiseEvent(....) method seemed to be exactly what I needed to be able to drive the state machine with test-provided stimuli.

Next, I went hunting for that overload... Turns out it's an extension method that appears to live here:

https://github.com/MassTransit/Automatonymous/blob/15e9181ed05e37ff71e85b514aafc351b1edf27f/src/Automatonymous/RaiseEventExtensions.tt

That's where I've gotten stuck. I'm not sure what the right way is to make use of these extension methods for testing? *.tt is a text generation template format... Are these installed as part of the nuget package? How do I gain access to the extensions? and/or more broadly, what is the recommended way to write unit tests for state machines inheriting from MassTransitStateMachine<T>?

Follow-up question here: How to successfully drive a MassTransitStateMachine via the InMemoryTestHarness?

回答1:

The test you reference are not from MassTransit but from the Automatonymous library, which is not part of MassTransit.

What you are looking for can be probably found here:

  • State machine saga tests using the test framework. The issue is that the test framework is coupled to NUnit
  • "Classic" saga tests using the test harness. The test harness is framework-agnostic but there are no tests that combine state machines and test harness, at least in the main repo.
  • State machine saga tests using the test hardness, outside of the main repo. This is my project and I use xUnit, since I am using the test harness, it is fine. However, you can see that one test is skipped and I don't remember exactly what was the issue but there was something there I couldn't resolve. This might be related to the persistence that I test though.