验证器的断言失败
assertion fails for validator
我创建了一个类和一个验证器,所以我想对验证器使用断言进行一些测试。这就是我说的:
类药
class Medicine{
private:
int ID;
string name;
public:
Medicine(){
this->ID=0;
this->name="";
}
Medicine(int ID, string name, float concentration, int quantity){
this->ID=ID;
this->name=name;
}
~Medicine(){};
//inline get methods
int getID(){
return ID;
}
string& getName(){
return name;
}
//inline set methods
void setID(int newID){
this->ID = newID;
}
void setName(string newName){
this->name = newName;
}
};
exceptions.h
#include <string>
using namespace std;
#include <string>
using namespace std;
class MyException
{
public:
MyException(string msg):message(msg){}
const string& getMessage() const {return message;}
private:
string message;
};
class ValidatorException: public MyException
{
public:
ValidatorException(string msg): MyException(msg){}
};
class RepositoryException: public MyException
{
public:
RepositoryException(string msg): MyException(msg){}
};
medicineValidator
#include "exceptions.h"
#include "medicine.h"
class MedicineValidator{
public:
void validate(Medicine& m) throw (ValidatorException);
};
#include "medicineValidator.h"
void MedicineValidator::validate(Medicine& m) throw (ValidatorException){
string message="";
if(m.getID()<1){
message+="The ID should be positive and >0!";
}
if(m.getName()==""){
message+="The name field is empty, and it shouldn't!";
}
if(m.getConcentration()<1){
message+="The concentration should be positive(>0)!";
}
if(m.getQuantity()<1){
message+="The quantity should be positive(>0)!";
}
}
测试验证器的算法:
void testValidator(){
Medicine* m = new Medicine(1,"para",30,40);
MedicineValidator* medValidator = new MedicineValidator();
medValidator->validate(*m);
m->setID(-2); //the ID should not be < 1 so it should catch the exception but the test fails
try{
medValidator->validate(*m);
assert(false);
}
catch(ValidatorException& ex){
assert(true);
}
delete m;
}
我做错了什么?我就是看不出错误……也许你可以。:)当我运行程序时,这就是我得到的:"断言失败:false,文件..src/utils/../domain/testMedicine.h,第32行
这个应用程序请求运行时以一种不寻常的方式终止它。请联系应用程序的支持团队获取更多信息。"
不要在validate方法中抛出异常消息
和assert(false)中止程序执行
相关文章:
- 尝试使用 std::vector<std::thread时出现静态断言失败错误>
- uint_not_usable_without_attribute在业力规则中使用数字生成器时静态断言失败
- 从 exe 文件 (Visual Studio ) 启动时调试断言失败
- 断言"id < 0"在Qt ActiveX中失败
- 在 CppUnit 中测试中止断言失败
- 使用扫描的调试断言失败
- MS 本机单元测试 - 断言::线程失败不起作用
- 为什么我的Qt程序在断言失败后继续运行?
- 图片不显示,关闭时出错 --> 调试断言失败!表达式:is_block_type_valid(标头>_block_use)
- 访问提升:shared_ptr 主范围外崩溃,断言失败:px != 0.指针的正确用法是什么?
- C++ 调试断言失败 - 矢量下标超出视觉工作室的范围
- 错误:断言失败 (src.type() == CV_8UC1) 在阈值中
- Boost Beast 异步服务器失败,断言失败:(id_ != T::id) 在多个 aync 调用中
- 使用imwrite OpenCV时断言失败
- 调试断言失败的缓冲区!=nullptr
- 使用 ofstream 写入文本文件时断言失败
- OpenCV 错误:相机校准:断言在matrix_wrap.cpp失败
- 提升继续恢复断言失败
- 获取有关调试断言失败的错误:表达式:"(_Ptr_user &(_BIG_ALLOCATION_ALIGNMENT -1)) == 0" &&0
- 打破gdb中失败断言的正确方法是什么