MassTransit / Sample-Twitch

Sample created in the live-coding video series
Apache License 2.0
80 stars 49 forks source link

Question: Running tests results in failure #1

Open shahabganji opened 4 years ago

shahabganji commented 4 years ago

Dear @phatboyg

Thanks for such an amazing content around MassTransit. I am trying to write the code as you teach on MassTransit concepts here.

I run dotnet test on this repository and it results in failing tests:

That being said, I am trying to write the sample hand-in-hand with your education stuff, hence created a repository, however the above-mentioned tests do not fail on my repository while they are almost identical to those in Sample-Twitch repo, but another test fails!! ( Should_cancel_order_when_account_closed ).

That repository is almost identical to the one here, and I am wondering why those tests are failing? is there something missed in between about the InMemoryHarness?

BTW, when I remove the .Activity(x => x.OfType<CustomerAccountClosedActivity>()) from here in the OrderStateMachine state definitions the test pass!!

I will be grateful and highly appreciate if you could invest some time to answer my question :pray:

Again, many thanks for sharing your valuable knowledge with all of us.

phatboyg commented 4 years ago

Do you have the CustomerAccountClosedActivity registered in your container, so that the state machine can resolve it?

shahabganji commented 4 years ago

@phatboyg Yes, the app works fine when dotnet run.

I think if the saga activities NOT being registered we will get a DI error indicating it cannot resolve type T, for instance, CustomerAccountClosedActivity.

here I've registered both activities used in the saga. However, I don't know how does that relates to InMemoryTestHarness does it also use DI?

phatboyg commented 4 years ago

The in-memory test harness doesn't have any magic behind it, and state machines where the activities are specifying using that style need to have some type of factory that can create them. I'm not sure there is a testing harness provision for that, since there isn't a way to specify the factory beyond adding it to the consume context during the saga execution.

I'd have to think about it. It's definitely a limitation of the test harness.

shahabganji commented 4 years ago

state machines where the activities are specifying using that style need to have some type of factory that can create them.

That looks rational. however, In this case, even when the activities have no deps, with default ctor, the test is still failing.


Anyway, thanks for the time and support; still don't know why tests fail in one project and not in the other?! And there is also a situation in which a test fails when run with all the others but when runs alone, it's green. The behavior sounds strange to me. 😩

phatboyg commented 4 years ago

So, it does use the default constructor activity factory if there isn't on available on the payload context.

https://github.com/MassTransit/MassTransit/blob/develop/src/MassTransit/Automatonymous/Activities/StateMachineActivityFactoryPayloadExtensions.cs#L10

So if your activity has a public default constructor, it should be created as part of the test.