异常模块测试C++



我在方法中测试异常时遇到了问题。我收到错误 C2337 预期异常: 找不到属性。

void Railcar::AddPlace(std::string name, unsigned int number)
{
if (number > PlaceVector.size())
throw std::exception("Place not found");
if (PlaceVector[number - 1].GetNamePassenger() != " ")
throw std::exception("Place is occup");
PlaceVector[number - 1] = Place(number, name);
}

[ExpectedException("Place not found")]
TEST_METHOD(RailcarAddPlaceExseptionTest)
{
Railcar railcar(Luxury, 50, 1);
railcar.CreatePlase();
auto func = [&railcar] { railcar.AddPlace("John Titor", 51); };
Assert::ExpectException<int>(func);
}

C++中的属性与 C# 中的属性不同,因为无法定义自定义属性。

以下是可用C++属性的列表:

https://en.cppreference.com/w/cpp/language/attributes

https://learn.microsoft.com/en-us/cpp/cpp/attributes?view=vs-2019

最新更新