Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如何在NUnit测试中测试属性是否引发异常?_.net_C++ Cli_Nunit - Fatal编程技术网

.net 如何在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

如何在从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)'

您可以使用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);