1

My question is similar to question about DI for NserviceBus Handler for testing (Handler). As a solution, you can use constructor injection by using the following syntax:

Test.Handler<YourMessageHandler>(bus => new YourMessageHandler(dep1, dep2))

I couldn't find a way to use the same approach for Saga testing. There is a support for property injecting, which would look something like this:

var saga = Test.Saga<MySaga>()
            .WithExternalDependencies(DependenciesSetUp);
private void DependenciesSetUp(MySaga saga)
    {
        saga.M2IntegrationService = M2IntegrationService.Object;
        saga.ProcessLogService = ProcessLogService.Object;
        saga.Log = Log.Object;
    }

However, this approach requires making my dependencies public properties. And I want to try to avoid it.

Is there a way to use construction dependency injection for Saga testing?

Community
  • 1
  • 1
Tonven
  • 608
  • 9
  • 27

2 Answers2

1

You can work around this like:

Have a saga that has a constructor with parameters (in addition to a default empty constructor, which is required).

This is how your test can look like:

Test.Initialize();
var injected = new InjectedDependency() {Id = Guid.NewGuid(), SomeText = "Text"};
var testingSaga = new MySaga(injected);
var saga = Test.Saga(testingSaga);
saga.WhenReceivesMessageFrom("enter code here")

Will this work for you?

Leandros
  • 16,805
  • 9
  • 69
  • 108
Sean Farmar
  • 2,254
  • 13
  • 10
1

Yes it is also supported:

        var saga = new MySaga(new MyFirstDep(), new MySecondDep());

        Test.Saga(saga)
            .ExpectSend<ProcessOrder>(m => m.Total == 500)
            .ExpectTimeoutToBeSetIn<SubmitOrder>((state, span) => span == TimeSpan.FromDays(7))
            .When(s => s.Handle(new SubmitOrder
            {
                Total = 500
            }));
John Simons
  • 4,288
  • 23
  • 41