I'm receiving the following Exception:
NSubstitute.Exceptions.NotASubstituteException: NSubstitute extension methods like .Received() can only be called on objects created using Substitute.For() and related methods.
...when calling chargeService.When():
public class WhenUserTopsUpAndStripeRejectsPaymentRequest
{
[Theory, AutoNSubstituteData]
public void it_should_throw_AutoTopUpFailedException(
[Frozen] StripeChargeService chargeService,
[Frozen] IRepository repository,
TopUpUserAccountBalance message,
TopUpUserAccountBalanceHandler sut) <== has dependency on StripeChargeService
{
repository.Find(Arg.Any<ById<User>>())
.Returns(new User
{
AutoTopUpEnabled = true,
AccountBalance = -15
});
=====> chargeService.When(s => s.Create(Arg.Any<StripeChargeCreateOptions>()))
.DoNotCallBase();
Assert.Throws<AutoTopUpFailedException>(() => sut.Handle(message));
}
}
Now, I can of course get around this by doing as the Exception suggests and manually create the StripeChargeService, and then manually create and inject all my dependencies into my SUT, but I'd rather have less code and let AutoFixture do the work.
public class AndStripeRejectsPaymentRequest
{
[Theory, AutoNSubstituteData]
public void it_should_throw_AutoTopUpFailedException(
IMediator mediator,
IBillingConfig config,
[Frozen] IRepository repository,
TopUpDriverAccountBalance message)
{
var chargeService = Substitute.ForPartsOf<StripeChargeService>("");
repository.Find(Arg.Any<ById<Driver, TopUpDriverAccountBalanceHandler.DriverProjection>>())
.Returns(new TopUpDriverAccountBalanceHandler.DriverProjection
{
AutoTopUpEnabled = true,
AccountBalance = -15
});
chargeService.When(s => s.Create(Arg.Any<StripeChargeCreateOptions>()))
.DoNotCallBase();
// Manually build SUT, with params declared above.
var sut = new TopUpDriverAccountBalanceHandler(mediator, repository, config, chargeService);
Assert.Throws<AutoTopUpFailedException>(() => sut.Handle(message));
}
}
I thought that by using the AutoNSubstituteCustomization()
in the AutoFixture.AutoNSubstitute NuGet package, auto-mocked parameters would be created using NSubstitute. What am I doing wrong?