I am pretty new to unit testing and NUnit, my case is that a test expecting a simple exception stopped working when I changed from the Ninject NSubstitute mocking kernel adapter to plain NSubstitute for mocking Returns
purposes.
private IRecordRepository RecordRepositorySeed()
{
var repository = Substitute.For<IRecordRepository>();
record.GetSingleAsync(r => r.Id == "1").Returns(new Task<Record>(() => new Record(...))); // sample record
return repository;
}
[Test]
[ExpectedException(typeof(NullReferenceException))]
public void UnexistantRecord()
{
var billingService = new BillingService(RecordRepositorySeed());
billingService.GenerateBill("5", "test");
}
The test dont pass as it outputs that the test is expecting the System.NullReferenceException
.
I debugged the tested method and it is actually triggering it:
//After a query on record that returns null, this snippet goes on
if (record == null)
{
//It's reaching this part
throw new NullReferenceException("record not found");
}
I tried these solutions but none of them worked for me:
NUnit unit test has ExpectedException but still failing on exception
The thing is this test was working before I changed the mock to the Substitute
syntax and I'm unable to figure out what's wrong with this approach.
I'm not sure if it is relevant but the GenerateBill
method is async void