在方法中测试异常时遇到问题。我收到C2337 ExpectedException:属性未找到错误。

    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://docs.microsoft.com/en-us/cpp/cpp/attributes?view=vs-2019

08-28 23:36