【发布时间】:2013-11-05 00:27:25
【问题描述】:
我正在尝试使用amop 编写模拟。我正在使用 Visual Studio 2008。
我有这个接口类:
struct Interface {
virtual void Activate() = 0;
};
还有这个接收指向这个Interface的指针的其他类,像这样:
struct UserOfInterface {
void execute(Interface* iface) {
iface->Activate();
}
};
所以我试着写一些这样的测试代码:
amop::TMockObject<Interface> mock;
mock.Method(&Interface::Activate).Count(1);
UserOfInterface user;
user.execute((Interface*)mock);
mock.Verifiy();
有效!到目前为止一切顺利,但我真正想要的是在 execute() 方法中的 boost::shared_ptr,所以我写了这个:
struct UserOfInterface {
void execute(boost::shared_ptr<Interface> iface) {
iface->Activate();
}
};
测试代码现在应该如何?我尝试了一些东西,例如:
amop::TMockObject<Interface> mock;
mock.Method(&Interface::Activate).Count(1);
UserOfInterface user;
boost::shared_ptr<Interface> mockAsPtr((Interface*)mock);
user.execute(mockAsPtr);
mock.Verifiy();
它可以编译,但显然会崩溃,因为在作用域的末尾,变量 'mock' 被双重破坏(因为堆栈变量 'mock' 和 shared_ptr)。
我还尝试在堆上创建“模拟”变量:
amop::TMockObject<Interface>* mock(new amop::TMockObject<Interface>);
mock->Method(&Interface::Activate).Count(1);
UserOfInterface user;
boost::shared_ptr<Interface> mockAsPtr((Interface*)*mock);
user.execute(mockAsPtr);
mock->Verifiy();
但它不起作用,不知何故进入了一个无限循环,在我遇到 boost 在 shared_ptr 试图删除对象时找不到模拟对象的析构函数之前遇到问题。
有人成功使用amop 和 boost::shared_ptr 吗?
【问题讨论】: