0
我正在使用谷歌測試來執行可能會拋出異常的類的測試。我測試的方法是:谷歌測試不處理異常
void SerialPortManager::OpenPort(PortID portNo) throw(){
try{
ports[portNo]->Open();
}
catch(exception &e){
throw;
}
}
而且我使用來測試它的測試:
TEST(SerialPortManagerTest,ExceptionCatchedOnFailedOpen) {
PortID portID = COM1;
MockSerialPort* port1 = new MockSerialPort();
EXPECT_CALL(*port1, Open()).Times(Exactly(1)).WillOnce(Throw(SerialPortOpenErrorException()));
MockSerialPort* port2 = new MockSerialPort();
MockSerialPort* port3 = new MockSerialPort();
MockSerialPortFactory portFactory;
EXPECT_CALL(portFactory, CreateSerialPort(_)).Times(3).WillOnce(
ReturnPointee(&port1)).WillOnce(ReturnPointee(&port2)).WillOnce(
ReturnPointee(&port3));
SerialPortManager* serialPortManager = new SerialPortManager(
(SerialPortFactoryInterface*) &portFactory);
EXPECT_ANY_THROW(serialPortManager->OpenPort(portID));
delete serialPortManager;
}
我預計測試將被傳遞,而是我得到:
terminate called after throwing an instance of 'SerialPortOpenErrorException'
what(): Error Opening Serial Port
¿我如何測試異常被拋出?
無論如何,我會刪除異常說明符。 –