如何测试属性(通常返回浮点值)会在从C ++-CLI调用的NUnit测试中引发异常?我尝试了以下感觉它应该工作:
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)'
答案 0 :(得分:2)
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);
答案 1 :(得分:1)
您可以使用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);
}