c++ - 如何模拟 QML 组件

标签 c++ unit-testing mocking qml qt5

实际上我正在尝试在 QML component which embeds C++ objects 上运行一些测试.不幸的是,我在执行测试时遇到了一些错误。 QML 文件无法识别 C++ 对象。这也是有意义的,因为 C++ 对象是在 main.cpp 文件中设置的。

我的问题是:如何模拟上下文属性来执行 QML 测试?或者换句话说,我如何通过混合 Qt/QML 代码进行单元测试?

最佳答案

据我了解,你遇到了和我一样的问题。前段时间我写了这个模拟:https://bitbucket.org/troyane/qml-cpp-template (您可以根据自己的目的免费使用该代码)。

看看main.cpp,你可以看到两种做事的方式:

// 1 case:
// Register type and create object at QML side
qmlRegisterType<CppClass>("CppClassModule", 1, 0, "CppClass");
QQmlApplicationEngine engine(QUrl("qrc:///qml/main.qml"));
qDebug() << "Qt version: " << qVersion();
// 2 case:
// Create object here and "move it up" to QML side
// engine.rootContext()->setContextProperty("cppClass", new CppClass);

祝你好运!

关于c++ - 如何模拟 QML 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23060717/

相关文章:

c++ - SFML 只绘制一些 Sprite

c++ - C++中的对象序列化

unit-testing - 模拟命令并根据调用模拟的次数获得不同的结果

java - 模拟具有 setListener(...) 的依赖项

c++ - 将 ostream 复制到 std::string

c++ - 内存映射文件 - 如何在其他进程中插入一条数据

unit-testing - py.test : dump stuck background threads at the end of the tests

c# - 带有泛型的 NUnit 测试用例

c# - IoC 预期接口(interface)的命名约定

python - 如何在另一个因 moto 的模拟而不一定存在的函数中模拟对象的函数调用