.net 如何在NUnit测试中测试属性是否引发异常?
如何在从C++-CLI调用的NUnit测试中测试属性(通常返回浮点值)是否引发异常?我试过以下方法,感觉应该有效:.net 如何在NUnit测试中测试属性是否引发异常?,.net,c++-cli,nunit,.net,C++ Cli,Nunit,如何在从C++-CLI调用的NUnit测试中测试属性(通常返回浮点值)是否引发异常?我试过以下方法,感觉应该有效: System::Type^ type = System::NotImplementedException::typeid; NUnit::Framework::TestDelegate^ delegateToTest = gcnew NUnit::Framework::TestDelegate(this, &(MyClass::MyProperty::get)); Asse
System::Type^ type = System::NotImplementedException::typeid;
NUnit::Framework::TestDelegate^ delegateToTest = gcnew NUnit::Framework::TestDelegate(this, &(MyClass::MyProperty::get));
Assert::Throws(type, delegateToTest);
…但这给了我:
error C3352: 'float MyClass::MyProperty::get(void)' : the specified function does not match the delegate type 'void (void)'
您可以使用Assert::Throws方法。您必须创建一个与此委托匹配的方法
void MethodThatThrows()
{
MyClass::MyProperty::get;
}
void Test()
{
System::Type^ type = System::NotImplementedException::typeid;
NUnit::Framework::TestDelegate^ delegateToTest = gcnew NUnit::Framework::TestDelegate(this, MethodThatThrows);
Assert::Throws(type, delegateToTest);
}
请参阅:您可以使用Assert::Throws方法。您必须创建一个与此委托匹配的方法
void MethodThatThrows()
{
MyClass::MyProperty::get;
}
void Test()
{
System::Type^ type = System::NotImplementedException::typeid;
NUnit::Framework::TestDelegate^ delegateToTest = gcnew NUnit::Framework::TestDelegate(this, MethodThatThrows);
Assert::Throws(type, delegateToTest);
}
请参阅:Assert::Throws将仅与返回void的方法一起使用。您正试图将其与返回浮点的方法一起使用 简单的解决方案是将读取的属性包装到方法中,并断言包装方法引发异常
void ReadMyProperty()
{
float ignored = this.MyProperty;
}
System::Type^ type = System::NotImplementedException::typeid;
NUnit::Framework::TestDelegate^ delegateToTest =
gcnew NUnit::Framework::TestDelegate(this, &(MyClass::ReadMyProperty));
Assert::Throws(type, delegateToTest);
Assert::Throws只适用于返回void的方法。您正试图将其与返回浮点的方法一起使用 简单的解决方案是将读取的属性包装到方法中,并断言包装方法引发异常
void ReadMyProperty()
{
float ignored = this.MyProperty;
}
System::Type^ type = System::NotImplementedException::typeid;
NUnit::Framework::TestDelegate^ delegateToTest =
gcnew NUnit::Framework::TestDelegate(this, &(MyClass::ReadMyProperty));
Assert::Throws(type, delegateToTest);