【发布时间】:2016-01-21 12:17:59
【问题描述】:
我有一种方法可以将数据保存在我的数据库(C# 和 Mysql)中。我正在使用 MVP 模型,所以它的作用是在 Presenter 中保存 > 在服务中保存 > 在管理器中保存 > 在 dao 中保存。我在管理器中进行数据完整性控制。
我想测试我的演示者。第一次保存测试是用不正确的值完成的,以检查我的经理是否抛出异常。
问题是我的演示者捕获了这个异常,所以我不能使用Assert.Throws<Exception>(() => {DoSave();}),因为 Assert 没有看到异常。
NUnit 中是否有一个 Assert 来检查是否在测试代码中引发了异常而没有到达测试本身?
简化,看起来像这样: (主持人)
private void SaveData()
{
try
{
//Calls service who calls manager who checks data then call DAO to save.
if (MyService.SaveData(currentData, ReferentielType.ENEG))
{
//Success
}
else
{
//Failure
}
}
//Thrown by the manager if the data are incorrect
catch (DataInvalidException ex)
{
// log exception as error
TraceHelper.TraceError(ex, "The data {0} is not correct", currentData.Code);
// send a message to user
MessageContainer.Instance.SendErrorMessage("DataInvalid", BusinessError.DataInvalid, currentData.Code);
}
}
}
还有我的测试:
[Test]
public void UpdateDataTest()
{
//Test setup
//making fake data
//When the var etatDuTest is different from 0, it means some of the data are invalid
//Instance of the presenter
MockView view = new MockView();
PresenterToTest presenter = new PresenterToTest(view);
//Testing with invalid values
//bad label
((MockView)((IView)presenter.View)).etatDuTest = 1;
Assert.Throws<DataInvalidException>(() => { ((MockView)((IView)presenter.View)).RaiseSaveDataClicked(); });
//Bad num eva
((MockView)((IView)presenter.View)).etatDuTest = 2;
Assert.Throws<DataInvalidException>(() => { ((MockView)((IView)presenter.View)).RaiseSaveDataClicked(); });
//Testing with everything OK
//Tout est bon logiquement.
((MockView)((IView)presenter.View)).etatDuTest = 0;
((MockView)((IView)presenter.View)).RaiseSaveDataClicked();
}
【问题讨论】:
-
行
MyService.SaveData(currentData, ReferentielType.ENEG)可以抛出Exception吗? -
而不是断言已经引发了异常(您将无法检查),您应该确保当您的
CoclicoInvalidException类中有CoclicoInvalidException并且TraceError和MessageContainter被正确调用。为了确保Manager正确地抛出异常,它应该有它自己的测试类,异常不会在您的业务逻辑中处理。 -
@StephenRoss 是的,我已经编写了管理器测试,但我想测试我的应用程序的另一层,以确保捕获到异常。
-
因此,一种解决方法可能是测试
MyService.SaveData(currentData, ReferentielType.ENEG)抛出ColicoInvalidException,然后测试SaveData不会抛出它。显然使用相同的参数。请注意,这并不是一个真正干净的解决方案。使用delegate可能会更干净 -
@nivolas 在这种情况下,正如我所说,您要确保使用适当的参数正确调用
TraceHelper.TraceError。并且MessageContainer.Instance.SendErrorMessage使用适当的参数正确调用。这是为了测试您是否正确处理了异常,您无需在测试中检查它是否已被抛出,因为您将验证(或断言)您的对象已被正确调用(取决于您如何模拟它们)。
标签: c# unit-testing exception nunit