Created
February 25, 2013 14:40
-
-
Save andlju/5030216 to your computer and use it in GitHub Desktop.
MassTransit Saga testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestClass] | |
public class when_consuming_PassengersAdded_with_saga_in_WaitingForInformation_state : With<TransportAvailabilitySaga> | |
{ | |
private readonly Guid _passenger1Id = Guid.NewGuid(); | |
private readonly Guid _passenger2Id = Guid.NewGuid(); | |
private readonly Guid _correlationId = Guid.NewGuid(); | |
protected override IEnumerable<TransportAvailabilitySaga> Given() | |
{ | |
var saga = CreateSagaInState(_correlationId, TransportAvailabilitySaga.WaitingForInformation); | |
yield return saga; | |
} | |
protected override void When(SagaTestInstanceConfigurator<BusTestScenario, TransportAvailabilitySaga> x) | |
{ | |
x.Send(new PassengersAdded() | |
{ | |
AggregateId = _correlationId, | |
Passengers = new[] | |
{ | |
new PassengersAdded.Passenger() {PassengerId = _passenger1Id, Age = 40,}, | |
new PassengersAdded.Passenger() {PassengerId = _passenger2Id, Age = 44,}, | |
} | |
}); | |
} | |
[TestMethod] | |
public void then_saga_does_not_transfer_to_Waiting_stage() | |
{ | |
Assert.IsFalse(Test.Saga.Any(s => s.CorrelationId == _correlationId && s.CurrentState == TransportAvailabilitySaga.Waiting)); | |
} | |
[TestMethod] | |
public void then_saga_has_passengers_information_set() | |
{ | |
Assert.IsTrue(Test.Saga.Any(s => s.CorrelationId == _correlationId && s.Passengers != null)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class With<TSaga> where TSaga : SagaStateMachine<TSaga>, ISaga | |
{ | |
private SagaTest<BusTestScenario, TSaga> _test; | |
public SagaTest<BusTestScenario, TSaga> Test | |
{ | |
get { return _test; } | |
} | |
protected TSaga CreateSagaInState(Guid correlationId, State state) | |
{ | |
var saga = FastActivator<TSaga>.Create(correlationId); | |
var currentStateField = typeof(StateMachine<TSaga>).GetField("_currentState", BindingFlags.Instance | BindingFlags.NonPublic); | |
if (currentStateField == null) | |
throw new InvalidOperationException("No _currentState field found. Is this really a StateMachineSaga?"); | |
currentStateField.SetValue(saga, state); | |
return saga; | |
} | |
protected abstract IEnumerable<TSaga> Given(); | |
[TestInitialize] | |
public void Initialize() | |
{ | |
_test = TestFactory.ForSaga<TSaga>().InSingleBusScenario().New(x => | |
{ | |
var sagas = Given(); | |
var sagaRepository = new InMemorySagaRepository<TSaga>(); | |
foreach(var saga in sagas) | |
{ | |
sagaRepository.Add(saga); | |
} | |
x.UseSagaRepository(sagaRepository); | |
When(x); | |
}); | |
_test.Execute(); | |
} | |
protected abstract void When(SagaTestInstanceConfigurator<BusTestScenario, TSaga> x); | |
[TestCleanup] | |
public void Teardown() | |
{ | |
_test.Dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment