Reputation: 135
I'm trying to mock Masstransit's GetRetryAttempt() method to return some values using NSubstitute but doesn't seems to be working. Not getting any errors but GetRetryAttempt() returns always 0 instead of 2.
Test
var context = Substitute.For<ConsumeContext<FakeCommand>>();
var consumeRetryContext = Substitute.For<ConsumeRetryContext>();
consumeRetryContext.RetryAttempt.Returns(2);
context.TryGetPayload(out consumeRetryContext).Returns(true);
context.GetRetryAttempt().Returns(consumeRetryContext.RetryAttempt);
Application code
public async Task Consume(ConsumeContext<T> context)
{
......
var retryAttempt = context.GetRetryAttempt();
}
Upvotes: 0
Views: 216
Reputation: 13640
You didn't mock the retry context correctly. This works
var context = Substitute.For<ConsumeContext<FakeCommand>>();
var consumeRetryContext = Substitute.For<ConsumeRetryContext>();
consumeRetryContext.RetryAttempt.Returns(2);
context
.TryGetPayload(out Arg.Any<ConsumeRetryContext>())
.Returns(result =>
{
result[0] = consumeRetryContext;
return true;
});
Upvotes: 0