单元测试不可复制的对象
Unit test noncopyable object
我有一个不可复制的怪物基类,我还有一个IView类。 我有一个从两个怪物和IView继承的地精职业, 我有一个控制器,它将指向 IView 的指针作为参数。 基本上我想检查霍布妖精是否爆炸了。
我正在使用 gmock/gtest
我不断得到
Actual function call count doesn't match EXPECT_CALL(h, Explode())...
Expected: to be called at least once
Actual: never called - unsatisfied and active
当我使用模拟对象时。我错过了什么?
怪物基地
#ifndef MONSTER_H
#define MONSTER_H
#include <string>
// interface for all monsters
class monster {
public:
virtual ~monster();
// forbid copying
monster(monster const &) = delete;
monster & operator=(monster const &) = delete;
void receive_damage(double damage);
void interact_with_chainsaw();
std::string name() const;
protected:
// allow construction for child classes only
monster();
private:
virtual void do_receive_damage(double damage) = 0;
virtual void do_interact_with_chainsaw() = 0;
virtual std::string do_name() const = 0;
};
#endif // MONSTER_H
四、四
#ifndef IVIEW_H
#define IVIEW_H
class IView
{
public:
virtual void Explode() = 0;
virtual ~IView(){}
};
#endif // IVIEW_H
妖怪
#ifndef HOBGOBLIN_H
#define HOBGOBLIN_H
#include "monster.h"
#include "iview.h"
class hobgoblin : public monster, public IView
{
public:
hobgoblin();
void Explode();
virtual ~hobgoblin();
private:
void do_receive_damage(double damage) final;
void do_interact_with_chainsaw() final;
std::string do_name() const final;
double health_;
};
#endif // HOBGOBLIN_H
#include "hobgoblin.h"
#include <QDebug>
hobgoblin::hobgoblin() :
health_(100.0)
{
}
hobgoblin::~hobgoblin()
{
}
void hobgoblin::Explode()
{
health_ = 0;
qDebug() << "Health is 0";
}
void hobgoblin::do_receive_damage(double damage)
{
health_ -= damage;
}
void hobgoblin::do_interact_with_chainsaw()
{
// imagine horrible, gory things here such as
// having to deal with a singleton
}
std::string hobgoblin::do_name() const
{
static std::string const name("Furry hobgoblin of nitwittery +5");
return name;
}
控制器
#ifndef CONTROLLER_H
#define CONTROLLER_H
#include "iview.h"
class Controller
{
public:
Controller(IView *view);
void Explode();
~Controller();
private:
IView *m_View;
};
#endif // CONTROLLER_H
#include "controller.h"
#include <QDebug>
Controller::Controller(IView *view):
m_View(view)
{
}
void Controller::Explode()
{
m_View->Explode();
}
Controller::~Controller()
{
}
单元测试
class mockmonster : public IView
{
public:
MOCK_METHOD0(Explode,void());
virtual ~mockmonster(){}
};
TEST(MockMonster,Explode)
{
// this is not calling explode as expected.
mockmonster h;
Controller c(&h);
c.Explode();
}
TEST(HobGoblin,Explode)
{
// this calls explode fine
hobgoblin h;
Controller c(&h);
c.Explode();
}
那么,你的Explode
函数不应该是虚拟的吗?
从外观上看,你的mockmonster
正在遮蔽IView
的功能。由于Controller
是指向IView
的指针,而Explode
是非虚拟的,它将调用IView
的版本。
作为旁注,我怀疑您的任何一个类是否不可复制在这里很重要。使用 gmock 时,不可复制的类在设置期望/断言时是有问题的(即,您希望使用特定对象调用函数 - 该对象必须由 gmock 在内部复制,这可能会失败(。
相关文章:
- 简单可复制与可简单复制
- 在什么条件下使用 std::memcpy 在对象之间复制是安全的?
- 我可以从列表中获取对象并复制它们,但如何删除我复制的对象?
- 是否可以将不可复制的成员用作使对象不可复制的替代方法?
- 如果 iostream 对象不可复制,为什么以下代码是合法的?
- 为什么我可以使用 memcpy 将一个对象变量复制到另一个对象变量
- reinterpret_cast,只读访问,简单的可复制类型,会出什么问题?
- 对于参加可复制和可移动类的访问者来说,应该有多少过载?
- 可变参数宏:无法通过"..."传递非平凡可复制类型的对象
- 为什么 std::atomic<std::string> 会给出微不足道的可复制错误?
- 我可以隐式地创建一个琐碎的可复制类型吗
- C :对象上的可复制视图
- 错误:无法通过'...'传递非平凡可复制类型的对象'class boost::filesystem::path'
- 使用realloc可以安全地重新分配琐碎的可复制对象的存储吗
- "constructing"一个带有memcpy的可复制对象
- 错误:无法通过`..`传递非普通可复制类型的对象
- 无法传递非普通可复制类型“const class mysqlpp::String”的对象
- 将std::memcpy用于非平凡可复制类型的对象
- 如何返回一个不可移动(但可复制)的对象
- 正在复制通常在c++ 14中定义的可复制对象