Frage

How do I test that a property (which normally returns a float value) throws an exception in an NUnit test called from C++-CLI? I've tried the following which feels like it ought to work:

System::Type^ type = System::NotImplementedException::typeid;
NUnit::Framework::TestDelegate^ delegateToTest = gcnew NUnit::Framework::TestDelegate(this, &(MyClass::MyProperty::get));
Assert::Throws(type, delegateToTest);

...but that gives me:

error C3352: 'float MyClass::MyProperty::get(void)' : the specified function does not match the delegate type 'void (void)'
War es hilfreich?

Lösung

Assert::Throws will only work with a method that returns void. You're attempting to use it with a method that returns a float.

The simple solution is to wrap the property read in a method, and assert that the wrapping method throws the exception.

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);

Andere Tipps

You can use the Assert::Throws method. And you must create a method that matches this delegate

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);
}

See: Exception Asserts (NUnit 2.5)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top