NUnit NSubstitute不期待例外

时间:2015-09-21 14:35:14

标签: c# unit-testing nunit nsubstitute

我对单元测试和NUnit都很陌生,我的情况是,当我从Ninject NSubstitute模拟内核适配器更改为纯NSubstitute以模拟Returns时,期望一个简单异常的测试停止工作。

    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");
    }

测试没有通过,因为它输出的测试期望System.NullReferenceException

我调试了测试方法,它实际上是在触发它:

//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");
}

我试过这些解决方案,但没有一个能为我工作:

NUnit expected exceptions

NUnit unit test has ExpectedException but still failing on exception

在我将模拟更改为Substitute语法之前,测试工作正常,我无法弄清楚这种方法有什么问题。

我不确定它是否相关,但GenerateBill方法是async void

0 个答案:

没有答案