NSubstitute:Assert 不包含 Throws 的定义
NSubstitute: Assert does not contain definition for Throws
我正在使用 Visual Studio 附带的测试框架以及 NSubstitute 来对采用系统 ID 的方法进行单元测试,如果在数据库中找不到系统则抛出异常。 ..
public VRTSystem GetSystem(int systemID)
{
VRTSystem system = VrtSystemsRepository.GetVRTSystemByID(systemID);
if (system == null)
{
throw new Exception("System not found");
}
return system;
}
(如果这看起来很奇怪,此方法有一个特定的业务案例要求它抛出异常,因为返回一个空系统对于它的使用是不可接受的)
我想编写一个测试来检查如果系统不存在是否抛出异常。我目前有以下...
[TestMethod]
public void LicensingApplicationServiceBusinessLogic_GetSystem_SystemDoesntExist()
{
var bll = new LicensingApplicationServiceBusinessLogic();
try
{
VRTSystem systemReturned = bll.GetSystem(613);
Assert.Fail("Should have thrown an exception, but didn't.);
}
catch () { }
}
通过不模拟存储库,VrtSystemsRepository.GetVRTSystemByID()
返回的系统将为空,并抛出异常。虽然这有效,但对我来说看起来不对。我没想到在测试中需要 try/catch 块。
NSubstitute docs 有一个例子暗示我应该能够如下测试它...
[TestMethod]
public void GetSystem_SystemDoesntExist()
{
var bll = new LicensingApplicationServiceBusinessLogic();
Assert.Throws<Exception>(() => bll.GetSystem(613));
}
但是,如果我在我的测试代码中尝试这样做,我会得到 Throws
以红色突出显示的错误消息“Assert 不包含 Throws 的定义”
现在,我不确定那个页面上的示例是否涵盖了我的场景,因为测试代码指定被测试的方法抛出异常,我并不真正理解,因为我认为这个想法测试的目的是让被测试的方法单独存在,并测试在各种情况下会发生什么。但是,即使没有它,我也不明白为什么 Assert.Throws
方法不存在。
有人有什么想法吗?
编辑: DavidG 指出 Assert.Throws
可能是 NUnit 的一部分,而不是 MS 框架,这可以解释为什么无法识别它。如果是这样,我目前正在测试的方法是否正确?
正如 DavidG 所提到的,所引用的文档正在使用 NUnit 进行断言。
如果不使用该框架,您可以使用 ExpectedExceptionAttribute Class
[TestMethod]
[ExpectedException(typeof(<<Your expected exception here>>))]
public void GetSystem_SystemDoesntExist() {
var bll = new LicensingApplicationServiceBusinessLogic();
bll.GetSystem(613);
}
如果没有抛出预期的异常,它将失败。
我正在使用 Visual Studio 附带的测试框架以及 NSubstitute 来对采用系统 ID 的方法进行单元测试,如果在数据库中找不到系统则抛出异常。 ..
public VRTSystem GetSystem(int systemID)
{
VRTSystem system = VrtSystemsRepository.GetVRTSystemByID(systemID);
if (system == null)
{
throw new Exception("System not found");
}
return system;
}
(如果这看起来很奇怪,此方法有一个特定的业务案例要求它抛出异常,因为返回一个空系统对于它的使用是不可接受的)
我想编写一个测试来检查如果系统不存在是否抛出异常。我目前有以下...
[TestMethod]
public void LicensingApplicationServiceBusinessLogic_GetSystem_SystemDoesntExist()
{
var bll = new LicensingApplicationServiceBusinessLogic();
try
{
VRTSystem systemReturned = bll.GetSystem(613);
Assert.Fail("Should have thrown an exception, but didn't.);
}
catch () { }
}
通过不模拟存储库,VrtSystemsRepository.GetVRTSystemByID()
返回的系统将为空,并抛出异常。虽然这有效,但对我来说看起来不对。我没想到在测试中需要 try/catch 块。
NSubstitute docs 有一个例子暗示我应该能够如下测试它...
[TestMethod]
public void GetSystem_SystemDoesntExist()
{
var bll = new LicensingApplicationServiceBusinessLogic();
Assert.Throws<Exception>(() => bll.GetSystem(613));
}
但是,如果我在我的测试代码中尝试这样做,我会得到 Throws
以红色突出显示的错误消息“Assert 不包含 Throws 的定义”
现在,我不确定那个页面上的示例是否涵盖了我的场景,因为测试代码指定被测试的方法抛出异常,我并不真正理解,因为我认为这个想法测试的目的是让被测试的方法单独存在,并测试在各种情况下会发生什么。但是,即使没有它,我也不明白为什么 Assert.Throws
方法不存在。
有人有什么想法吗?
编辑: DavidG 指出 Assert.Throws
可能是 NUnit 的一部分,而不是 MS 框架,这可以解释为什么无法识别它。如果是这样,我目前正在测试的方法是否正确?
正如 DavidG 所提到的,所引用的文档正在使用 NUnit 进行断言。
如果不使用该框架,您可以使用 ExpectedExceptionAttribute Class
[TestMethod]
[ExpectedException(typeof(<<Your expected exception here>>))]
public void GetSystem_SystemDoesntExist() {
var bll = new LicensingApplicationServiceBusinessLogic();
bll.GetSystem(613);
}
如果没有抛出预期的异常,它将失败。